Add unparse to Formatter extender (#2780)

This commit is contained in:
Sami Mazouz 2021-04-14 11:34:49 +01:00 committed by GitHub
parent 67e452dda5
commit 72e29a24b7
4 changed files with 79 additions and 2 deletions

View File

@ -18,6 +18,7 @@ class Formatter implements ExtenderInterface, LifecycleInterface
{
private $configurationCallbacks = [];
private $parsingCallbacks = [];
private $unparsingCallbacks = [];
private $renderingCallbacks = [];
/**
@ -58,6 +59,28 @@ class Formatter implements ExtenderInterface, LifecycleInterface
return $this;
}
/**
* Prepare the system for unparsing. This can be used to modify the text that was parsed.
* Please note that the parsed text must be returned, regardless of whether it's changed.
*
* @param callable|string $callback
*
* The callback can be a closure or invokable class, and should accept:
* - mixed $context
* - string $xml: The parsed text.
*
* The callback should return:
* - string $xml: The text to be unparsed.
*
* @return self
*/
public function unparse($callback)
{
$this->unparsingCallbacks[] = $callback;
return $this;
}
/**
* Prepare the system for rendering. This can be used to modify the xml that will be rendered, or to modify the renderer.
* Please note that the xml to be rendered must be returned, regardless of whether it's changed.
@ -91,6 +114,10 @@ class Formatter implements ExtenderInterface, LifecycleInterface
$formatter->addParsingCallback(ContainerUtil::wrapCallback($callback, $container));
}
foreach ($this->unparsingCallbacks as $callback) {
$formatter->addUnparsingCallback(ContainerUtil::wrapCallback($callback, $container));
}
foreach ($this->renderingCallbacks as $callback) {
$formatter->addRenderingCallback(ContainerUtil::wrapCallback($callback, $container));
}

View File

@ -20,6 +20,8 @@ class Formatter
protected $parsingCallbacks = [];
protected $unparsingCallbacks = [];
protected $renderingCallbacks = [];
/**
@ -52,6 +54,11 @@ class Formatter
$this->parsingCallbacks[] = $callback;
}
public function addUnparsingCallback($callback)
{
$this->unparsingCallbacks[] = $callback;
}
public function addRenderingCallback($callback)
{
$this->renderingCallbacks[] = $callback;
@ -98,10 +105,15 @@ class Formatter
* Unparse XML.
*
* @param string $xml
* @param mixed $context
* @return string
*/
public function unparse($xml)
public function unparse($xml, $context = null)
{
foreach ($this->unparsingCallbacks as $callback) {
$xml = $callback($context, $xml);
}
return Unparser::unparse($xml);
}

View File

@ -128,7 +128,7 @@ class CommentPost extends Post
*/
public function getContentAttribute($value)
{
return static::$formatter->unparse($value);
return static::$formatter->unparse($value, $this);
}
/**

View File

@ -89,6 +89,36 @@ class FormatterTest extends TestCase
$this->assertEquals('<t>ReplacedText&lt;a&gt;</t>', $this->getFormatter()->parse('Text<a>'));
}
/**
* @test
*/
public function custom_formatter_unparsing_doesnt_work_by_default()
{
$this->assertEquals('Text<a>', $this->getFormatter()->unparse('<t>Text&lt;a&gt;</t>'));
}
/**
* @test
*/
public function custom_formatter_unparsing_works_if_added_with_closure()
{
$this->extend((new Extend\Formatter)->unparse(function ($context, $xml) {
return '<t>ReplacedText&lt;a&gt;</t>';
}));
$this->assertEquals('ReplacedText<a>', $this->getFormatter()->unparse('<t>Text&lt;a&gt;</t>'));
}
/**
* @test
*/
public function custom_formatter_unparsing_works_if_added_with_invokable_class()
{
$this->extend((new Extend\Formatter)->unparse(InvokableUnparsing::class));
$this->assertEquals('ReplacedText<a>', $this->getFormatter()->unparse('<t>Text&lt;a&gt;</t>'));
}
/**
* @test
*/
@ -136,6 +166,14 @@ class InvokableParsing
}
}
class InvokableUnparsing
{
public function __invoke($context, $xml)
{
return '<t>ReplacedText&lt;a&gt;</t>';
}
}
class InvokableRendering
{
public function __invoke($renderer, $context, $xml, $request)