Skip to content

Commit c9951e5

Browse files
Implement multiple pipes support in PlaceholderFormatter
- Modified PlaceholderFormatter to split pipe string by unescaped | characters - Each modifier is applied sequentially in order - Added test cases for multiple pipes functionality - Supports escaped pipes (\|) as arguments within modifiers Co-authored-by: henriquemoody <154023+henriquemoody@users.noreply.github.com>
1 parent 9d6d7d2 commit c9951e5

3 files changed

Lines changed: 101 additions & 9 deletions

File tree

composer.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/PlaceholderFormatter.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use function array_key_exists;
2020
use function preg_replace_callback;
21+
use function preg_split;
2122

2223
final readonly class PlaceholderFormatter implements Formatter
2324
{
@@ -67,6 +68,20 @@ private function processPlaceholder(array $matches, array $parameters): string
6768
return $placeholder;
6869
}
6970

70-
return $this->modifier->modify($parameters[$name], $pipe);
71+
if ($pipe === null) {
72+
return $this->modifier->modify($parameters[$name], null);
73+
}
74+
75+
// Split by unescaped pipes to support multiple modifiers
76+
$pipes = preg_split('/(?<!\\\\)\|/', $pipe) ?: [];
77+
78+
// Apply each modifier in sequence
79+
$value = $parameters[$name];
80+
foreach ($pipes as $currentPipe) {
81+
$result = $this->modifier->modify($value, $currentPipe);
82+
$value = $result;
83+
}
84+
85+
return $value;
7186
}
7287
}

tests/Unit/PlaceholderFormatterTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,4 +671,81 @@ public static function providerForEscapedPipes(): array
671671
],
672672
];
673673
}
674+
675+
/** @param array<string, mixed> $parameters */
676+
#[Test]
677+
#[DataProvider('providerForMultiplePipes')]
678+
public function itShouldHandleMultiplePipesInSequence(
679+
array $parameters,
680+
string $template,
681+
string $expected,
682+
): void {
683+
$formatter = new PlaceholderFormatter($parameters);
684+
$actual = $formatter->format($template);
685+
686+
self::assertSame($expected, $actual);
687+
}
688+
689+
/** @return array<string, array{0: array<string, mixed>, 1: string, 2: string}> */
690+
public static function providerForMultiplePipes(): array
691+
{
692+
return [
693+
'trans then quote' => [
694+
['name' => 'hello'],
695+
'{{name|trans|quote}}',
696+
'`hello`',
697+
],
698+
'quote then trans (demonstrates order matters)' => [
699+
['name' => 'hello'],
700+
'{{name|quote|trans}}',
701+
'`hello`',
702+
],
703+
'pattern then mask' => [
704+
['phone' => '1234567890'],
705+
'{{phone|pattern:(###) ###-####|mask:6-12}}',
706+
'(123) ******90',
707+
],
708+
'number then mask' => [
709+
['value' => '12345'],
710+
'{{value|number:2|mask:1-5}}',
711+
'*****45',
712+
],
713+
'three pipes: pattern, number, mask' => [
714+
['value' => '12345'],
715+
'{{value|pattern:###.##|number:2|mask:1-4}}',
716+
'****45',
717+
],
718+
];
719+
}
720+
721+
/** @param array<string, mixed> $parameters */
722+
#[Test]
723+
#[DataProvider('providerForMultiplePipesWithEscaping')]
724+
public function itShouldHandleMultiplePipesWithEscapedCharacters(
725+
array $parameters,
726+
string $template,
727+
string $expected,
728+
): void {
729+
$formatter = new PlaceholderFormatter($parameters);
730+
$actual = $formatter->format($template);
731+
732+
self::assertSame($expected, $actual);
733+
}
734+
735+
/** @return array<string, array{0: array<string, mixed>, 1: string, 2: string}> */
736+
public static function providerForMultiplePipesWithEscaping(): array
737+
{
738+
return [
739+
'pattern with escaped pipe then mask' => [
740+
['value' => '123456'],
741+
'{{value|pattern:###\|###|mask:1-3}}',
742+
'***|456',
743+
],
744+
'pattern with escaped colon then pattern with escaped pipe' => [
745+
['value' => '12345678'],
746+
'{{value|pattern:####\:####|pattern:####\|####}}',
747+
'1234|5678',
748+
],
749+
];
750+
}
674751
}

0 commit comments

Comments
 (0)