Initial import from aternosorg/codex-minecraft
Some checks failed
Tests / Run tests on PHP v8.4 (push) Failing after 32s
Tests / Run tests on PHP v8.5 (push) Failing after 2s

This commit is contained in:
2026-04-30 09:56:57 -05:00
commit 7c7fe5ca80
94 changed files with 7003 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
<?php
namespace Aternos\Codex\Printer;
use Aternos\Codex\Log\LineInterface;
/**
* Class DefaultPrinter
*
* @package Aternos\Codex\Printer
*/
class DefaultPrinter extends Printer
{
/**
* Print a line
*
* @param LineInterface $line
* @return string
*/
protected function printLine(LineInterface $line): string
{
return $line->getText() . PHP_EOL;
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Aternos\Codex\Printer;
use Aternos\Codex\Log\LineInterface;
/**
* Class ModifiableDefaultPrinter
*
* @package Aternos\Codex\Printer
*/
class ModifiableDefaultPrinter extends ModifiablePrinter
{
/**
* Print a line
*
* @param LineInterface $line
* @return string
*/
protected function printLine(LineInterface $line): string
{
return $this->runModifications($line->getText()) . PHP_EOL;
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Aternos\Codex\Printer;
/**
* Class ModifiablePrinter
*
* @package Aternos\Codex\Printer
*/
abstract class ModifiablePrinter extends Printer implements ModifiablePrinterInterface
{
/**
* @var ModificationInterface[]
*/
protected array $modifications = [];
/**
* Set all modifications replacing the current modifications
*
* @param ModificationInterface[] $modifications
* @return $this
*/
public function setModifications(array $modifications): static
{
$this->modifications = [];
foreach ($modifications as $modification) {
$this->addModification($modification);
}
return $this;
}
/**
* Add a modification
*
* @param ModificationInterface $modification
* @return $this
*/
public function addModification(ModificationInterface $modification): static
{
$this->modifications[] = $modification;
return $this;
}
/**
* Run the set modifications for a string
*
* @param string $text
* @return string
*/
protected function runModifications(string $text): string
{
foreach ($this->modifications as $modification) {
$text = $modification->modify($text);
}
return $text;
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Aternos\Codex\Printer;
/**
* Interface ModifiablePrinterInterface
*
* @package Aternos\Codex\Printer
*/
interface ModifiablePrinterInterface extends PrinterInterface
{
/**
* Set all modifications replacing the current modifications
*
* @param ModificationInterface[] $modifications
* @return $this
*/
public function setModifications(array $modifications): static;
/**
* Add a modification
*
* @param ModificationInterface $modification
* @return $this
*/
public function addModification(ModificationInterface $modification): static;
}

View File

@@ -0,0 +1,13 @@
<?php
namespace Aternos\Codex\Printer;
/**
* Class Modification
*
* @package Aternos\Codex\Printer
*/
abstract class Modification implements ModificationInterface
{
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Aternos\Codex\Printer;
/**
* Interface ModificationInterface
*
* @package Aternos\Codex\Printer
*/
interface ModificationInterface
{
/**
* Modify the given string and return it
*
* @param string $text
* @return string
*/
public function modify(string $text): string;
}

View File

@@ -0,0 +1,60 @@
<?php
namespace Aternos\Codex\Printer;
/**
* Class PatternModification
*
* @package Aternos\Codex\Printer
*/
class PatternModification extends Modification
{
/**
* @param string $pattern
* @param string $replacement
*/
public function __construct(
protected string $pattern,
protected string $replacement)
{
}
/**
* Set the pattern
*
* See http://php.net/manual/de/function.preg-replace.php
*
* @param string $pattern
* @return $this
*/
public function setPattern(string $pattern): PatternModification
{
$this->pattern = $pattern;
return $this;
}
/**
* Set the replacement string
*
* See http://php.net/manual/de/function.preg-replace.php
*
* @param string $replacement
* @return $this
*/
public function setReplacement(string $replacement): PatternModification
{
$this->replacement = $replacement;
return $this;
}
/**
* Modify the given string and return it
*
* @param string $text
* @return string
*/
public function modify(string $text): string
{
return preg_replace($this->pattern, $this->replacement, $text);
}
}

99
src/Printer/Printer.php Normal file
View File

@@ -0,0 +1,99 @@
<?php
namespace Aternos\Codex\Printer;
use Aternos\Codex\Log\EntryInterface;
use Aternos\Codex\Log\LineInterface;
use Aternos\Codex\Log\LogInterface;
/**
* Class Printer
*
* @package Aternos\Codex\Printer
*/
abstract class Printer implements PrinterInterface
{
protected ?LogInterface $log = null;
protected ?EntryInterface $entry = null;
/**
* Set the log
*
* @param LogInterface $log
* @return $this
*/
public function setLog(LogInterface $log): static
{
$this->log = $log;
return $this;
}
/**
* Set the entry
*
* @param EntryInterface $entry
* @return $this
*/
public function setEntry(EntryInterface $entry): static
{
$this->entry = $entry;
return $this;
}
/**
* Print the log
*
* @return string
*/
public function print(): string
{
if ($this->entry) {
return $this->printEntry();
} else {
return $this->printLog();
}
}
/**
* Print a log
*
* @return string
*/
protected function printLog(): string
{
$return = "";
foreach ($this->log as $entry) {
$return .= $this->printEntry($entry);
}
return $return;
}
/**
* Print an entry
*
* @param EntryInterface|null $entry
* @return string
*/
protected function printEntry(?EntryInterface $entry = null): string
{
if ($entry === null) {
$entry = $this->entry;
}
$return = "";
foreach ($entry as $line) {
$return .= $this->printLine($line);
}
return $return;
}
/**
* Print a line
*
* @param LineInterface $line
* @return string
*/
abstract protected function printLine(LineInterface $line): string;
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Aternos\Codex\Printer;
use Aternos\Codex\Log\EntryInterface;
use Aternos\Codex\Log\LogInterface;
/**
* Interface PrinterInterface
*
* @package Aternos\Codex\Printer
*/
interface PrinterInterface
{
/**
* Set the log
*
* @param LogInterface $log
* @return $this
*/
public function setLog(LogInterface $log): static;
/**
* Set the entry
*
* @param EntryInterface $entry
* @return $this
*/
public function setEntry(EntryInterface $entry): static;
/**
* Print the log
*
* @return string
*/
public function print(): string;
}