Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e5e925e

Browse files
norkunasfabpot
authored andcommitted
[Serializer] Add DateTimeNormalizer::CAST_KEY context option
1 parent bf3b54b commit e5e925e

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
7+
* Add `DateTimeNormalizer::CAST_KEY` context option
8+
49
7.0
510
---
611

src/Symfony/Component/Serializer/Context/Normalizer/DateTimeNormalizerContextBuilder.php

+8
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,12 @@ public function withTimezone(\DateTimeZone|string|null $timezone): static
6161

6262
return $this->with(DateTimeNormalizer::TIMEZONE_KEY, $timezone);
6363
}
64+
65+
/**
66+
* @param 'int'|'float'|null $cast
67+
*/
68+
public function withCast(?string $cast): static
69+
{
70+
return $this->with(DateTimeNormalizer::CAST_KEY, $cast);
71+
}
6472
}

src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ final class DateTimeNormalizer implements NormalizerInterface, DenormalizerInter
2525
{
2626
public const FORMAT_KEY = 'datetime_format';
2727
public const TIMEZONE_KEY = 'datetime_timezone';
28+
public const CAST_KEY = 'datetime_cast';
2829

2930
private array $defaultContext = [
3031
self::FORMAT_KEY => \DateTimeInterface::RFC3339,
3132
self::TIMEZONE_KEY => null,
33+
self::CAST_KEY => null,
3234
];
3335

3436
private const SUPPORTED_TYPES = [
@@ -59,7 +61,7 @@ public function getSupportedTypes(?string $format): array
5961
/**
6062
* @throws InvalidArgumentException
6163
*/
62-
public function normalize(mixed $object, ?string $format = null, array $context = []): string
64+
public function normalize(mixed $object, ?string $format = null, array $context = []): int|float|string
6365
{
6466
if (!$object instanceof \DateTimeInterface) {
6567
throw new InvalidArgumentException('The object must implement the "\DateTimeInterface".');
@@ -73,7 +75,11 @@ public function normalize(mixed $object, ?string $format = null, array $context
7375
$object = $object->setTimezone($timezone);
7476
}
7577

76-
return $object->format($dateTimeFormat);
78+
return match ($context[self::CAST_KEY] ?? $this->defaultContext[self::CAST_KEY] ?? false) {
79+
'int' => (int) $object->format($dateTimeFormat),
80+
'float' => (float) $object->format($dateTimeFormat),
81+
default => $object->format($dateTimeFormat),
82+
};
7783
}
7884

7985
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool

src/Symfony/Component/Serializer/Tests/Context/Normalizer/DateTimeNormalizerContextBuilderTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function testWithers(array $values)
3838
$context = $this->contextBuilder
3939
->withFormat($values[DateTimeNormalizer::FORMAT_KEY])
4040
->withTimezone($values[DateTimeNormalizer::TIMEZONE_KEY])
41+
->withCast($values[DateTimeNormalizer::CAST_KEY])
4142
->toArray();
4243

4344
$this->assertEquals($values, $context);
@@ -51,11 +52,13 @@ public static function withersDataProvider(): iterable
5152
yield 'With values' => [[
5253
DateTimeNormalizer::FORMAT_KEY => 'format',
5354
DateTimeNormalizer::TIMEZONE_KEY => new \DateTimeZone('GMT'),
55+
DateTimeNormalizer::CAST_KEY => 'int',
5456
]];
5557

5658
yield 'With null values' => [[
5759
DateTimeNormalizer::FORMAT_KEY => null,
5860
DateTimeNormalizer::TIMEZONE_KEY => null,
61+
DateTimeNormalizer::CAST_KEY => null,
5962
]];
6063
}
6164

src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php

+66
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,72 @@ public static function normalizeUsingTimeZonePassedInContextAndExpectedFormatWit
154154
];
155155
}
156156

157+
/**
158+
* @dataProvider provideNormalizeUsingCastCases
159+
*/
160+
public function testNormalizeUsingCastPassedInConstructor(\DateTimeInterface $value, string $format, ?string $cast, string|int|float $expectedResult)
161+
{
162+
$normalizer = new DateTimeNormalizer([DateTimeNormalizer::CAST_KEY => $cast]);
163+
164+
$this->assertSame($normalizer->normalize($value, null, [DateTimeNormalizer::FORMAT_KEY => $format]), $expectedResult);
165+
}
166+
167+
/**
168+
* @dataProvider provideNormalizeUsingCastCases
169+
*/
170+
public function testNormalizeUsingCastPassedInContext(\DateTimeInterface $value, string $format, ?string $cast, string|int|float $expectedResult)
171+
{
172+
$this->assertSame($this->normalizer->normalize($value, null, [DateTimeNormalizer::FORMAT_KEY => $format, DateTimeNormalizer::CAST_KEY => $cast]), $expectedResult);
173+
}
174+
175+
/**
176+
* @return iterable<array{0: \DateTimeImmutable, 1: non-empty-string, 2: 'int'|'float'|null, 3: 'int'|'float'|'string'}>
177+
*/
178+
public static function provideNormalizeUsingCastCases(): iterable
179+
{
180+
yield [
181+
\DateTimeImmutable::createFromFormat('U', '1703071202'),
182+
'Y',
183+
null,
184+
'2023',
185+
];
186+
187+
yield [
188+
\DateTimeImmutable::createFromFormat('U', '1703071202'),
189+
'Y',
190+
'int',
191+
2023,
192+
];
193+
194+
yield [
195+
\DateTimeImmutable::createFromFormat('U', '1703071202'),
196+
'Ymd',
197+
'int',
198+
20231220,
199+
];
200+
201+
yield [
202+
\DateTimeImmutable::createFromFormat('U', '1703071202'),
203+
'Y',
204+
'int',
205+
2023,
206+
];
207+
208+
yield [
209+
\DateTimeImmutable::createFromFormat('U.v', '1703071202.388'),
210+
'U.v',
211+
'float',
212+
1703071202.388,
213+
];
214+
215+
yield [
216+
\DateTimeImmutable::createFromFormat('U.u', '1703071202.388811'),
217+
'U.u',
218+
'float',
219+
1703071202.388811,
220+
];
221+
}
222+
157223
public function testNormalizeInvalidObjectThrowsException()
158224
{
159225
$this->expectException(InvalidArgumentException::class);

0 commit comments

Comments
 (0)