Initial import from aternosorg/codex-minecraft
This commit is contained in:
24
src/Printer/DefaultPrinter.php
Normal file
24
src/Printer/DefaultPrinter.php
Normal 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;
|
||||
}
|
||||
}
|
||||
24
src/Printer/ModifiableDefaultPrinter.php
Normal file
24
src/Printer/ModifiableDefaultPrinter.php
Normal 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;
|
||||
}
|
||||
}
|
||||
59
src/Printer/ModifiablePrinter.php
Normal file
59
src/Printer/ModifiablePrinter.php
Normal 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;
|
||||
}
|
||||
}
|
||||
27
src/Printer/ModifiablePrinterInterface.php
Normal file
27
src/Printer/ModifiablePrinterInterface.php
Normal 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;
|
||||
}
|
||||
13
src/Printer/Modification.php
Normal file
13
src/Printer/Modification.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Aternos\Codex\Printer;
|
||||
|
||||
/**
|
||||
* Class Modification
|
||||
*
|
||||
* @package Aternos\Codex\Printer
|
||||
*/
|
||||
abstract class Modification implements ModificationInterface
|
||||
{
|
||||
|
||||
}
|
||||
19
src/Printer/ModificationInterface.php
Normal file
19
src/Printer/ModificationInterface.php
Normal 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;
|
||||
}
|
||||
60
src/Printer/PatternModification.php
Normal file
60
src/Printer/PatternModification.php
Normal 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
99
src/Printer/Printer.php
Normal 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;
|
||||
}
|
||||
37
src/Printer/PrinterInterface.php
Normal file
37
src/Printer/PrinterInterface.php
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user