Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unparse to Formatter extender #2780

Merged
merged 2 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Extend/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Formatter implements ExtenderInterface, LifecycleInterface
{
private $configurationCallbacks = [];
private $parsingCallbacks = [];
private $unparsingCallbacks = [];
private $renderingCallbacks = [];

/**
Expand Down Expand Up @@ -58,6 +59,28 @@ public function parse($callback)
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.
Expand Down Expand Up @@ -91,6 +114,10 @@ public function extend(Container $container, Extension $extension = null)
$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));
}
Expand Down
14 changes: 13 additions & 1 deletion src/Formatter/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Formatter

protected $parsingCallbacks = [];

protected $unparsingCallbacks = [];

protected $renderingCallbacks = [];

/**
Expand Down Expand Up @@ -52,6 +54,11 @@ public function addParsingCallback($callback)
$this->parsingCallbacks[] = $callback;
}

public function addUnparsingCallback($callback)
{
$this->unparsingCallbacks[] = $callback;
}

public function addRenderingCallback($callback)
{
$this->renderingCallbacks[] = $callback;
Expand Down Expand Up @@ -98,10 +105,15 @@ public function render($xml, $context = null, ServerRequestInterface $request =
* 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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Post/CommentPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function restore()
*/
public function getContentAttribute($value)
{
return static::$formatter->unparse($value);
return static::$formatter->unparse($value, $this);
}

/**
Expand Down