Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion src/Modifiers/FormatterModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ public function modify(mixed $value, string|null $pipe): string

$formatter = $this->tryToCreateFormatter($name, $arguments);

if ($formatter === null || !is_scalar($value)) {
if ($formatter === null) {
return $this->nextModifier->modify($value, $pipe);
}

if (!is_scalar($value)) {
return $this->nextModifier->modify($value, null);
}

return $formatter->format((string) $value);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Helper/TestingModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@

final class TestingModifier implements Modifier
{
public string|null $lastPipe = null;

public function __construct(private string|null $customResult = null)
{
}

public function modify(mixed $value, string|null $pipe): string
{
$this->lastPipe = $pipe;

return $this->customResult ?? ($pipe ? sprintf('%s(%s)', $pipe, print_r($value, true)) : print_r($value, true));
}
}
49 changes: 49 additions & 0 deletions tests/Unit/Modifiers/FormatterModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,36 @@
#[CoversClass(FormatterModifier::class)]
final class FormatterModifierTest extends TestCase
{
#[Test]
#[DataProvider('providerForNonScalarValues')]
public function itShouldDelegateWithNullPipeForNonScalarValues(
mixed $value,
string $pipe,
): void {
$nextModifier = new TestingModifier('fallback');
$modifier = new FormatterModifier($nextModifier);

$result = $modifier->modify($value, $pipe);

self::assertSame('fallback', $result);
self::assertNull($nextModifier->lastPipe);
}

#[Test]
#[DataProvider('providerForInvalidFormatters')]
public function itShouldDelegateWithOriginalPipeForInvalidFormatters(
mixed $value,
string $pipe,
): void {
$nextModifier = new TestingModifier('fallback');
$modifier = new FormatterModifier($nextModifier);

$result = $modifier->modify($value, $pipe);

self::assertSame('fallback', $result);
self::assertSame($pipe, $nextModifier->lastPipe);
}

#[Test]
public function itShouldDelegateWhenPipeIsNull(): void
{
Expand Down Expand Up @@ -261,4 +291,23 @@ public function itShouldHandleEmptyArguments(): void

self::assertSame($expected, $actual);
}

/** @return array<string, array{0: mixed, 1: string}> */
public static function providerForNonScalarValues(): array
{
return [
'array value delegates with null pipe' => [['array'], 'date:Y/m/d'],
'object value delegates with null pipe' => [(object) ['key' => 'value'], 'number:2'],
'null value delegates with null pipe' => [null, 'mask:1-3'],
];
}

/** @return array<string, array{0: mixed, 1: string}> */
public static function providerForInvalidFormatters(): array
{
return [
'unknown formatter delegates with original pipe' => ['test value', 'unknown:formatter'],
'invalid number formatter delegates with original pipe' => ['test', 'number:invalid-decimal'],
];
}
}