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

Appearance settings

Commit 6c33afc

Browse filesBrowse files
committed
add ScalarDenormalizer and move the logic
1 parent 634d168 commit 6c33afc
Copy full SHA for 6c33afc

File tree

3 files changed

+90
-27
lines changed
Filter options

3 files changed

+90
-27
lines changed
+54Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Normalizer;
13+
14+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
16+
17+
final class ScalarDenormalizer implements DenormalizerInterface, CacheableSupportsMethodInterface
18+
{
19+
private const SCALAR_TYPES = [
20+
'int' => true,
21+
'bool' => true,
22+
'float' => true,
23+
'string' => true,
24+
];
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function denormalize($data, string $type, string $format = null, array $context = [])
30+
{
31+
if (!isset(self::SCALAR_TYPES[get_debug_type($data)])) {
32+
throw new InvalidArgumentException(sprintf('Data expected to be of one of the types in "%s" ("%s" given).', implode(', ', array_keys(self::SCALAR_TYPES)), get_debug_type($data)));
33+
}
34+
35+
if (!('is_'.$type)($data)) {
36+
throw new NotNormalizableValueException(sprintf('Data expected to be of type "%s" ("%s" given).', $type, get_debug_type($data)));
37+
}
38+
39+
return $data;
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function supportsDenormalization($data, string $type, string $format = null)
46+
{
47+
return isset(self::SCALAR_TYPES[$type]);
48+
}
49+
50+
public function hasCacheableSupportsMethod(): bool
51+
{
52+
return true;
53+
}
54+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Serializer.php
+2-17Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,6 @@
4747
*/
4848
class Serializer implements SerializerInterface, ContextAwareNormalizerInterface, ContextAwareDenormalizerInterface, ContextAwareEncoderInterface, ContextAwareDecoderInterface
4949
{
50-
private const SCALAR_TYPES = [
51-
'int' => true,
52-
'bool' => true,
53-
'float' => true,
54-
'string' => true,
55-
];
56-
5750
/**
5851
* @var Encoder\ChainEncoder
5952
*/
@@ -179,7 +172,7 @@ public function normalize($data, string $format = null, array $context = [])
179172
throw new NotNormalizableValueException(sprintf('Could not normalize object of type "%s", no supporting normalizer found.', get_debug_type($data)));
180173
}
181174

182-
throw new NotNormalizableValueException('An unexpected value could not be normalized: '.(!\is_resource($data) ? var_export($data, true) : sprintf('%s resource', get_resource_type($data))));
175+
throw new NotNormalizableValueException('An unexpected value could not be normalized: '.(!\is_resource($data) ? var_export($data, true) : sprintf('"%s" resource', get_resource_type($data))));
183176
}
184177

185178
/**
@@ -189,14 +182,6 @@ public function normalize($data, string $format = null, array $context = [])
189182
*/
190183
public function denormalize($data, string $type, string $format = null, array $context = [])
191184
{
192-
if (isset(self::SCALAR_TYPES[$type])) {
193-
if (!('is_'.$type)($data)) {
194-
throw new NotNormalizableValueException(sprintf('Data expected to be of type "%s" ("%s" given).', $type, get_debug_type($data)));
195-
}
196-
197-
return $data;
198-
}
199-
200185
if (!$this->normalizers) {
201186
throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
202187
}
@@ -221,7 +206,7 @@ public function supportsNormalization($data, string $format = null, array $conte
221206
*/
222207
public function supportsDenormalization($data, string $type, string $format = null, array $context = [])
223208
{
224-
return isset(self::SCALAR_TYPES[$type]) || null !== $this->getDenormalizer($data, $type, $format, $context);
209+
return null !== $this->getDenormalizer($data, $type, $format, $context);
225210
}
226211

227212
/**

‎src/Symfony/Component/Serializer/Tests/SerializerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/SerializerTest.php
+34-10Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
1919
use Symfony\Component\Serializer\Encoder\JsonEncoder;
2020
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
21-
use Symfony\Component\Serializer\Exception\LogicException;
2221
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
2322
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
2423
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
@@ -37,6 +36,7 @@
3736
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
3837
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
3938
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
39+
use Symfony\Component\Serializer\Normalizer\ScalarDenormalizer;
4040
use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;
4141
use Symfony\Component\Serializer\Serializer;
4242
use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummy;
@@ -558,7 +558,7 @@ public function testNormalizeScalarArray()
558558

559559
public function testDeserializeScalar()
560560
{
561-
$serializer = new Serializer([], ['json' => new JsonEncoder()]);
561+
$serializer = new Serializer([new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
562562

563563
$this->assertSame(42, $serializer->deserialize('42', 'int', 'json'));
564564
$this->assertTrue($serializer->deserialize('true', 'bool', 'json'));
@@ -570,35 +570,38 @@ public function testDeserializeScalar()
570570

571571
public function testDeserializeLegacyScalarType()
572572
{
573-
$this->expectException(LogicException::class);
574-
$serializer = new Serializer([], ['json' => new JsonEncoder()]);
573+
$this->expectException(NotNormalizableValueException::class);
574+
$serializer = new Serializer([new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
575575
$serializer->deserialize('42', 'integer', 'json');
576576
}
577577

578578
public function testDeserializeScalarTypeToCustomType()
579579
{
580-
$this->expectException(LogicException::class);
581-
$serializer = new Serializer([], ['json' => new JsonEncoder()]);
580+
$this->expectException(NotNormalizableValueException::class);
581+
$serializer = new Serializer([new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
582582
$serializer->deserialize('"something"', Foo::class, 'json');
583583
}
584584

585585
public function testDeserializeNonscalarTypeToScalar()
586586
{
587-
$this->expectException(NotNormalizableValueException::class);
588-
$serializer = new Serializer([], ['json' => new JsonEncoder()]);
587+
$this->expectException(InvalidArgumentException::class);
588+
$serializer = new Serializer([new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
589589
$serializer->deserialize('{"foo":true}', 'string', 'json');
590590
}
591591

592592
public function testDeserializeInconsistentScalarType()
593593
{
594594
$this->expectException(NotNormalizableValueException::class);
595-
$serializer = new Serializer([], ['json' => new JsonEncoder()]);
595+
$serializer = new Serializer([new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
596596
$serializer->deserialize('"42"', 'int', 'json');
597597
}
598598

599599
public function testDeserializeScalarArray()
600600
{
601-
$serializer = new Serializer([new ArrayDenormalizer()], ['json' => new JsonEncoder()]);
601+
$serializer = new Serializer([
602+
new ScalarDenormalizer(),
603+
new ArrayDenormalizer(),
604+
], ['json' => new JsonEncoder()]);
602605

603606
$this->assertSame([42], $serializer->deserialize('[42]', 'int[]', 'json'));
604607
$this->assertSame([true, false], $serializer->deserialize('[true,false]', 'bool[]', 'json'));
@@ -613,6 +616,27 @@ public function testDeserializeInconsistentScalarArray()
613616
$serializer->deserialize('["42"]', 'int[]', 'json');
614617
}
615618

619+
public function testDeserializeAndUnwrapScalarType()
620+
{
621+
$data = ['wrapper' => 7];
622+
623+
$serializer = new Serializer([
624+
new UnwrappingDenormalizer(new PropertyAccessor()),
625+
new ScalarDenormalizer(),
626+
new ObjectNormalizer(),
627+
new ArrayDenormalizer(),
628+
], ['json' => new JsonEncoder()]);
629+
630+
$result = $serializer->deserialize(
631+
json_encode($data),
632+
'int',
633+
'json',
634+
[UnwrappingDenormalizer::UNWRAP_PATH => '[wrapper]'],
635+
);
636+
637+
$this->assertEquals(7, $result);
638+
}
639+
616640
private function serializerWithClassDiscriminator()
617641
{
618642
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.