forked from Respect/Validation
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a stringifier for "listed" values
This can be extremely useful when dealing with custom templates.
- Loading branch information
1 parent
4379f66
commit a8ae57f
Showing
6 changed files
with
164 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Message\Placeholder; | ||
|
||
final class Listed | ||
{ | ||
/** @param array<int, mixed> $values */ | ||
public function __construct( | ||
public readonly array $values, | ||
public readonly string $lastGlue | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
namespace Respect\Validation\Message\Stringifier; | ||
|
||
use Respect\Stringifier\Stringifier; | ||
use Respect\Validation\Message\Placeholder\Listed; | ||
|
||
use function array_map; | ||
use function array_pop; | ||
use function count; | ||
use function implode; | ||
use function sprintf; | ||
|
||
final class ListedStringifier implements Stringifier | ||
{ | ||
public function __construct( | ||
private readonly Stringifier $stringifier | ||
) { | ||
} | ||
|
||
public function stringify(mixed $raw, int $depth): ?string | ||
{ | ||
if (!$raw instanceof Listed) { | ||
return null; | ||
} | ||
|
||
if (count($raw->values) === 0) { | ||
return null; | ||
} | ||
|
||
$strings = array_map(fn ($value) => $this->stringifier->stringify($value, $depth + 1), $raw->values); | ||
if (count($strings) < 3) { | ||
return implode(sprintf(' %s ', $raw->lastGlue), $strings); | ||
} | ||
$lastString = array_pop($strings); | ||
|
||
return sprintf('%s, %s %s', implode(', ', $strings), $raw->lastGlue, $lastString); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Message\Stringifier; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Respect\Stringifier\Stringifiers\JsonEncodableStringifier; | ||
use Respect\Validation\Message\Placeholder\Listed; | ||
use Respect\Validation\Test\TestCase; | ||
|
||
#[CoversClass(ListedStringifier::class)] | ||
final class ListedStringifierTest extends TestCase | ||
{ | ||
#[Test] | ||
#[DataProvider('providerForAnyValues')] | ||
public function itShouldNotStringifyWhenValueIsNotAnInstanceOfListed(mixed $value): void | ||
{ | ||
$quoter = new JsonEncodableStringifier(); | ||
$stringifier = new ListedStringifier($quoter); | ||
|
||
self::assertNull($stringifier->stringify($value, 0)); | ||
} | ||
|
||
#[Test] | ||
public function itShouldNotStringifyEmptyListed(): void | ||
{ | ||
$stringifier = new ListedStringifier(new JsonEncodableStringifier()); | ||
|
||
self::assertNull($stringifier->stringify(new Listed([], '-'), 0)); | ||
} | ||
|
||
#[Test] | ||
#[DataProvider('providerForListed')] | ||
public function itShouldStringifyWhenValueIsAnInstanceOfListed(Listed $listed, string $expected): void | ||
{ | ||
$stringifier = new ListedStringifier(new JsonEncodableStringifier()); | ||
|
||
$actual = $stringifier->stringify($listed, 0); | ||
|
||
self::assertSame($expected, $actual); | ||
} | ||
|
||
/** @return array<string, array{Listed, string}> */ | ||
public static function providerForListed(): array | ||
{ | ||
return [ | ||
'1 item' => [new Listed([1], 'and'), '1'], | ||
'2 items' => [new Listed([1, 2], 'and'), '1 and 2'], | ||
'3 items' => [new Listed([1, 2, 3], 'or'), '1, 2, or 3'], | ||
]; | ||
} | ||
} |