Skip to content

Commit 5da9851

Browse files
committed
feat: add Normalizer/Configurator/ConvertDateTime
1 parent f84e786 commit 5da9851

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CuyZ\Valinor\Normalizer\Configurator;
6+
7+
use CuyZ\Valinor\NormalizerBuilder;
8+
use DateTimeInterface;
9+
10+
/**
11+
* Convert a DateTimeInterface to a string using the given format.
12+
*
13+
* ```
14+
* use CuyZ\Valinor\NormalizerBuilder;
15+
* use CuyZ\Valinor\Normalizer\Configurator\ConvertDateTime;
16+
* use CuyZ\Valinor\Normalizer\Format;
17+
*
18+
* $userAsArray = (new NormalizerBuilder())
19+
* ->configureWith(new ConvertDateTime(\DateTimeInterface::ATOM))
20+
* ->normalizer(Format::array())
21+
* ->normalize($user);
22+
* // ['createdAt' => '2000-01-01T00:00:00+00:00', 'name' => 'John Doe']
23+
* ```
24+
*
25+
* @api
26+
*/
27+
final readonly class ConvertDateTime implements NormalizerBuilderConfigurator
28+
{
29+
/**
30+
* @param non-empty-string $format
31+
*/
32+
public function __construct(private string $format) {}
33+
34+
public function configureNormalizerBuilder(NormalizerBuilder $builder): NormalizerBuilder
35+
{
36+
return $builder->registerTransformer(
37+
/** @return non-empty-string */
38+
function (DateTimeInterface $date): string {
39+
return $date->format($this->format);
40+
}
41+
);
42+
}
43+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace CuyZ\Valinor\Tests\Integration\Normalizer\Configurator;
4+
5+
use CuyZ\Valinor\Normalizer\Configurator\ConvertDateTime;
6+
use CuyZ\Valinor\Normalizer\Format;
7+
use CuyZ\Valinor\Tests\Integration\IntegrationTestCase;
8+
use DateTimeInterface;
9+
use DateTimeZone;
10+
use PHPUnit\Framework\Attributes\DataProvider;
11+
use Safe\DateTimeImmutable;
12+
13+
class ConvertDateTimeTest extends IntegrationTestCase
14+
{
15+
/**
16+
* @param array<string, non-empty-string> $expectedValue
17+
* @param non-empty-string $format
18+
*/
19+
#[DataProvider('date_time_data_provider')]
20+
public function test_date_time_converts_values(array $expectedValue, string $format, object $object): void
21+
{
22+
$result = $this->normalizerBuilder()
23+
->configureWith(new ConvertDateTime($format))
24+
->normalizer(Format::array())
25+
->normalize($object);
26+
27+
self::assertSame($expectedValue, $result);
28+
}
29+
30+
/**
31+
* @return iterable<string, array{array, non-empty-string, object}>
32+
*/
33+
public static function date_time_data_provider(): iterable
34+
{
35+
$object = new class () {
36+
public DateTimeInterface $someDateTime;
37+
};
38+
$object->someDateTime = new DateTimeImmutable('1955-11-05T21:10:14', new DateTimeZone('UTC'));
39+
40+
yield 'to ATOM format' => [['someDateTime' => '1955-11-05T21:10:14+00:00'], DateTimeInterface::ATOM, $object];
41+
yield 'to RFC1036 format' => [['someDateTime' => 'Sat, 05 Nov 55 21:10:14 +0000'], DateTimeInterface::RFC1036, $object];
42+
yield 'to custom format' => [['someDateTime' => '05.11.1955 21:10:14'], 'd.m.Y H:i:s', $object];
43+
}
44+
}

0 commit comments

Comments
 (0)