Skip to content

Commit

Permalink
[FEATURE] Formally support arrays in v:format.replace
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder committed Sep 6, 2023
1 parent 0328e43 commit e96921e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
37 changes: 27 additions & 10 deletions Classes/ViewHelpers/Format/ReplaceViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,27 @@

/**
* Replaces $substring in $content with $replacement.
*
* Supports array as input substring/replacements and content.
*
* When input substring/replacement is an array, both must be
* the same length and must contain only strings.
*
* When input content is an array, the search/replace is done
* on every value in the input content array and the return
* value will be an array of equal size as the input content
* array but with all values search/replaced. All values in the
* input content array must be strings.
*/
class ReplaceViewHelper extends AbstractViewHelper
{
use CompileWithContentArgumentAndRenderStatic;

public function initializeArguments(): void
{
$this->registerArgument('content', 'string', 'Content in which to perform replacement');
$this->registerArgument('substring', 'string', 'Substring to replace', true);
$this->registerArgument('replacement', 'string', 'Replacement to insert', false, '');
$this->registerArgument('content', 'string', 'Content in which to perform replacement. Array supported.');
$this->registerArgument('substring', 'string', 'Substring to replace. Array supported.', true);
$this->registerArgument('replacement', 'string', 'Replacement to insert. Array supported.', false, '');
$this->registerArgument(
'returnCount',
'bool',
Expand All @@ -34,19 +45,25 @@ public function initializeArguments(): void
}

/**
* @return string|int
* @return array|string|int
*/
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
) {
$content = (string) $renderChildrenClosure();
/** @var string $substring */
$substring = (string) $arguments['substring'];
/** @var string $replacement */
$replacement = (string) $arguments['replacement'];
/** @var int|null $count */
$content = $renderChildrenClosure();
/** @var string|array $content */
$content = is_scalar($content) || $content === null ? (string) $content : (array) $content;

$substring = $arguments['substring'];
/** @var string|array $substring */
$substring = is_scalar($substring) ? (string) $substring : (array) $substring;

$replacement = $arguments['replacement'];
/** @var string|array $replacement */
$replacement = is_scalar($replacement) ? (string) $replacement : (array) $replacement;

$count = 0;
$caseSensitive = (boolean) $arguments['caseSensitive'];
$function = $caseSensitive ? 'str_replace' : 'str_ireplace';
Expand Down
11 changes: 11 additions & 0 deletions Tests/Unit/ViewHelpers/Format/ReplaceViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ public function testCanReplace(): void
$this->assertSame('bar', $test);
}

public function testCanReplaceWithArrays(): void
{
$arguments = [
'content' => ['foobar', 'foobaz', 'fizbaz'],
'substring' => ['foo', 'baz'],
'replacement' => ['x', 'x'],
];
$test = $this->executeViewHelper($arguments);
$this->assertSame(['xbar', 'xx', 'fizx'], $test);
}

public function testReturnsCountWhenAsked(): void
{
$arguments = [
Expand Down

0 comments on commit e96921e

Please sign in to comment.