-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Aternos\Codex\Printer; | ||
|
||
/** | ||
* Class ModifiableDefaultPrinter | ||
* | ||
* @package Aternos\Codex\Printer | ||
*/ | ||
class ModifiableDefaultPrinter extends ModifiablePrinter | ||
{ | ||
/** | ||
* Print the log | ||
* | ||
* @return string | ||
*/ | ||
public function print(): string | ||
{ | ||
$return = ""; | ||
foreach ($this->log as $entry) { | ||
foreach ($entry as $line) { | ||
$return .= $this->runModifications($line->getText()) . PHP_EOL; | ||
} | ||
} | ||
|
||
return $return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Aternos\Codex\Printer; | ||
|
||
/** | ||
* Class ModifiablePrinter | ||
* | ||
* @package Aternos\Codex\Printer | ||
*/ | ||
abstract class ModifiablePrinter extends Printer implements ModifiablePrinterInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $modificationClasses; | ||
|
||
/** | ||
* Set all modification classes replacing the current classes | ||
* | ||
* @param array $modificationClasses | ||
* @return $this | ||
*/ | ||
public function setModificationClasses(array $modificationClasses) | ||
{ | ||
$this->modificationClasses = []; | ||
foreach ($modificationClasses as $modificationClass) { | ||
$this->addModificationClass($modificationClass); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Add a modification class | ||
* | ||
* @param string $modificationClass | ||
* @return $this | ||
*/ | ||
public function addModificationClass(string $modificationClass) | ||
{ | ||
if (!is_subclass_of($modificationClass, ModificationInterface::class)) { | ||
throw new \InvalidArgumentException("Class " . $modificationClass . " does not implement " . ModificationInterface::class . "."); | ||
} | ||
|
||
$this->modificationClasses[] = $modificationClass; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Run the set modifications for a string | ||
* | ||
* @param string $text | ||
* @return string | ||
*/ | ||
protected function runModifications(string $text) | ||
{ | ||
foreach ($this->modificationClasses as $modificationClass) { | ||
/** @var ModificationInterface $modification */ | ||
$modification = new $modificationClass(); | ||
$text = $modification->modify($text); | ||
} | ||
|
||
return $text; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Aternos\Codex\Printer; | ||
|
||
/** | ||
* Interface ModifiablePrinterInterface | ||
* | ||
* @package Aternos\Codex\Printer | ||
*/ | ||
interface ModifiablePrinterInterface extends PrinterInterface | ||
{ | ||
/** | ||
* Set all modification classes replacing the current classes | ||
* | ||
* @param array $modificationClasses | ||
* @return $this | ||
*/ | ||
public function setModificationClasses(array $modificationClasses); | ||
|
||
/** | ||
* Add a modification class | ||
* | ||
* @param string $modificationClass | ||
* @return $this | ||
*/ | ||
public function addModificationClass(string $modificationClass); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Aternos\Codex\Printer; | ||
|
||
/** | ||
* Class Modification | ||
* | ||
* @package Aternos\Codex\Printer | ||
*/ | ||
abstract class Modification implements ModificationInterface | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
use Aternos\Codex\Printer\Modification; | ||
|
||
/** | ||
* Class TestModification | ||
*/ | ||
class TestModification extends Modification | ||
{ | ||
/** | ||
* Modify the given string and return it | ||
* | ||
* @param string $text | ||
* @return string | ||
*/ | ||
public function modify(string $text): string | ||
{ | ||
return str_replace("foo", "bar", $text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
require_once __DIR__ . "/../../src/Printer/TestModification.php"; | ||
|
||
use Aternos\Codex\Log\File\StringLogFile; | ||
use Aternos\Codex\Log\Log; | ||
use Aternos\Codex\Printer\ModifiableDefaultPrinter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ModifiableDefaultPrinterTest extends TestCase | ||
{ | ||
|
||
public function testPrint() | ||
{ | ||
$logFile = new StringLogFile("This is foo!"); | ||
$log = new Log(); | ||
$log->setLogFile($logFile); | ||
$log->parse(); | ||
|
||
$printer = new ModifiableDefaultPrinter(); | ||
$printer->addModificationClass(TestModification::class); | ||
$printer->setLog($log); | ||
$this->assertEquals("This is bar!", trim($printer->print())); | ||
} | ||
} |