Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit54d7b75

Browse files
committed
[Serializer] Support specifying format for DateTimeNormalizer::denormalize
1 parent69e8654 commit54d7b75

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@ public function supportsNormalization($data, $format = null)
6868
*/
6969
publicfunctiondenormalize($data,$class,$format =null,array$context =array())
7070
{
71+
$dateTimeFormat =isset($context[self::FORMAT_KEY]) ?$context[self::FORMAT_KEY] :null;
72+
73+
if (null !==$dateTimeFormat) {
74+
$object = \DateTime::class ===$class ? \DateTime::createFromFormat($dateTimeFormat,$data) : \DateTimeImmutable::createFromFormat($dateTimeFormat,$data);
75+
76+
if (false !==$object) {
77+
return$object;
78+
}
79+
80+
$dateTimeErrors = \DateTime::class ===$class ? \DateTime::getLastErrors() : \DateTimeImmutable::getLastErrors();
81+
82+
thrownewUnexpectedValueException(sprintf(
83+
'Parsing datetime string "%s" using format "%s" resulted in %d errors:'."\n".'%s',
84+
$data,
85+
$dateTimeFormat,
86+
$dateTimeErrors['error_count'],
87+
implode("\n",$this->formatDateTimeErrors($dateTimeErrors['errors']))
88+
));
89+
}
90+
7191
try {
7292
return \DateTime::class ===$class ?new \DateTime($data) :new \DateTimeImmutable($data);
7393
}catch (\Exception$e) {
@@ -88,4 +108,22 @@ public function supportsDenormalization($data, $type, $format = null)
88108

89109
returnisset($supportedTypes[$type]);
90110
}
111+
112+
/**
113+
* Formats datetime errors.
114+
*
115+
* @param array $errors
116+
*
117+
* @return string[]
118+
*/
119+
privatefunctionformatDateTimeErrors(array$errors)
120+
{
121+
$formattedErrors =array();
122+
123+
foreach ($errorsas$pos =>$message) {
124+
$formattedErrors[] =sprintf('at position %d: %s',$pos,$message);
125+
}
126+
127+
return$formattedErrors;
128+
}
91129
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public function testNormalize()
4141
$this->assertEquals('2016-01-01T00:00:00+00:00',$this->normalizer->normalize(new \DateTimeImmutable('2016/01/01',new \DateTimeZone('UTC'))));
4242
}
4343

44-
publicfunctiontestContextFormat()
44+
publicfunctiontestNormalizeUsingFormatPassedInContext()
4545
{
4646
$this->assertEquals('2016',$this->normalizer->normalize(new \DateTime('2016/01/01'),null,array(DateTimeNormalizer::FORMAT_KEY =>'Y')));
4747
}
4848

49-
publicfunctiontestConstructorFormat()
49+
publicfunctiontestNormalizeUsingFormatPassedInConstructor()
5050
{
5151
$this->assertEquals('16', (newDateTimeNormalizer('y'))->normalize(new \DateTime('2016/01/01',new \DateTimeZone('UTC'))));
5252
}
@@ -55,7 +55,7 @@ public function testConstructorFormat()
5555
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
5656
* @expectedExceptionMessage The object must implement the "\DateTimeInterface".
5757
*/
58-
publicfunctiontestInvalidDataThrowException()
58+
publicfunctiontestNormalizeInvalidObjectThrowsException()
5959
{
6060
$this->normalizer->normalize(new \stdClass());
6161
}
@@ -75,10 +75,17 @@ public function testDenormalize()
7575
$this->assertEquals(new \DateTime('2016/01/01',new \DateTimeZone('UTC')),$this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTime::class));
7676
}
7777

78+
publicfunctiontestDenormalizeUsingFormatPassedInContext()
79+
{
80+
$this->assertEquals(new \DateTimeImmutable('2016/01/01',new \DateTimeZone('UTC')),$this->normalizer->denormalize('2016.01.01', \DateTimeInterface::class,null,array(DateTimeNormalizer::FORMAT_KEY =>'Y.m.d|')));
81+
$this->assertEquals(new \DateTimeImmutable('2016/01/01',new \DateTimeZone('UTC')),$this->normalizer->denormalize('2016.01.01', \DateTimeImmutable::class,null,array(DateTimeNormalizer::FORMAT_KEY =>'Y.m.d|')));
82+
$this->assertEquals(new \DateTime('2016/01/01',new \DateTimeZone('UTC')),$this->normalizer->denormalize('2016.01.01', \DateTime::class,null,array(DateTimeNormalizer::FORMAT_KEY =>'Y.m.d|')));
83+
}
84+
7885
/**
7986
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
8087
*/
81-
publicfunctiontestInvalidDateThrowException()
88+
publicfunctiontestDenormalizeInvalidDataThrowsException()
8289
{
8390
$this->normalizer->denormalize('invalid date', \DateTimeInterface::class);
8491
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp