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

Skip to content

Commit d103aa6

Browse files
committed
[Serializer] Add method getSupportedTypes to NormalizerInterface and DenormalizerInterface
1 parent dbb475d commit d103aa6

File tree

57 files changed

+241
-232
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+241
-232
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ CHANGELOG
55
---
66

77
* Add `XmlEncoder::SAVE_OPTIONS` context option
8-
* Add `SupportedTypesMethodInterface` for normalizers and denormalizers that can provide supported types outside from calls to their `supports*()` methods
9-
* Deprecate `CacheableSupportsMethodInterface` in favor of implementing `SupportedTypesMethodInterface`
8+
* Add method `getSupportedTypes(?string $format)` to `NormalizerInterface` and `DenormalizerInterface`
9+
* Deprecate `CacheableSupportsMethodInterface` in favor of the new `getSupportedTypes(?string $format)` methods
1010

1111
6.2
1212
---

src/Symfony/Component/Serializer/Debug/TraceableNormalizer.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
1818
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
1919
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20-
use Symfony\Component\Serializer\Normalizer\SupportedTypesMethodInterface;
2120
use Symfony\Component\Serializer\SerializerAwareInterface;
2221
use Symfony\Component\Serializer\SerializerInterface;
2322

@@ -28,21 +27,22 @@
2827
*
2928
* @internal
3029
*/
31-
class TraceableNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, NormalizerAwareInterface, DenormalizerAwareInterface, SupportedTypesMethodInterface, CacheableSupportsMethodInterface
30+
class TraceableNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, NormalizerAwareInterface, DenormalizerAwareInterface, CacheableSupportsMethodInterface
3231
{
3332
public function __construct(
3433
private NormalizerInterface|DenormalizerInterface $normalizer,
3534
private SerializerDataCollector $dataCollector,
3635
) {
3736
}
3837

39-
public function getSupportedTypes(): ?array
38+
public function getSupportedTypes(?string $format): ?array
4039
{
41-
if ($this->normalizer instanceof SupportedTypesMethodInterface) {
42-
return $this->normalizer->getSupportedTypes();
40+
// @deprecated remove condition in 7.0
41+
if (!method_exists($this->normalizer, 'getSupportedTypes')) {
42+
return null;
4343
}
4444

45-
return null;
45+
return $this->normalizer->getSupportedTypes($format);
4646
}
4747

4848
public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
150150
}
151151
}
152152

153+
public function getSupportedTypes(?string $format): ?array
154+
{
155+
return null;
156+
}
157+
153158
public function hasCacheableSupportsMethod(): bool
154159
{
155160
return false;

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,11 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
135135
$this->objectClassResolver = $objectClassResolver;
136136
}
137137

138-
/**
139-
* @param array $context
140-
*/
138+
public function getSupportedTypes(?string $format): ?array
139+
{
140+
return null;
141+
}
142+
141143
public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */)
142144
{
143145
return \is_object($data) && !$data instanceof \Traversable;
@@ -290,16 +292,12 @@ abstract protected function extractAttributes(object $object, string $format = n
290292

291293
/**
292294
* Gets the attribute value.
293-
*
294-
* @return mixed
295295
*/
296296
abstract protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = []);
297297

298-
/**
299-
* @param array $context
300-
*/
301298
public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */)
302299
{
300+
// @deprecated return true in 7.0
303301
return class_exists($type) || (interface_exists($type, false) && $this->classDiscriminatorResolver && null !== $this->classDiscriminatorResolver->getMappingForClass($type));
304302
}
305303

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ class ArrayDenormalizer implements ContextAwareDenormalizerInterface, Denormaliz
2727
{
2828
use DenormalizerAwareTrait;
2929

30+
public function getSupportedTypes(?string $format): ?array
31+
{
32+
// @deprecated remove condition in 7.0
33+
if (!method_exists($this->denormalizer, 'getSupportedTypes')) {
34+
return null;
35+
}
36+
37+
return $this->denormalizer->getSupportedTypes($format);
38+
}
39+
3040
/**
3141
* @throws NotNormalizableValueException
3242
*/
@@ -65,6 +75,8 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
6575
throw new BadMethodCallException(sprintf('The nested denormalizer needs to be set to allow "%s()" to be used.', __METHOD__));
6676
}
6777

78+
// @deprecated return true in 7.0
79+
6880
return str_ends_with($type, '[]')
6981
&& $this->denormalizer->supportsDenormalization($data, substr($type, 0, -2), $format, $context);
7082
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
*
2121
* @author Alexandre Daubois <[email protected]>
2222
*/
23-
final class BackedEnumNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface, SupportedTypesMethodInterface
23+
final class BackedEnumNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
2424
{
25-
public function getSupportedTypes(): array
25+
public function getSupportedTypes(?string $format): array
2626
{
2727
return [
2828
\BackedEnum::class => true,
@@ -40,6 +40,7 @@ public function normalize(mixed $object, string $format = null, array $context =
4040

4141
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
4242
{
43+
// @deprecated return true in 7.0
4344
return $data instanceof \BackedEnum;
4445
}
4546

@@ -65,12 +66,13 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
6566

6667
public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
6768
{
69+
// @deprecated return true in 7.0
6870
return is_subclass_of($type, \BackedEnum::class);
6971
}
7072

7173
public function hasCacheableSupportsMethod(): bool
7274
{
73-
trigger_deprecation('symfony/serializer', '6.3', 'CacheableSupportsMethodInterface::hasCacheableSupportsMethod() is deprecated, use "getSupportedTypes()" return value instead.');
75+
trigger_deprecation('symfony/serializer', '6.3', 'CacheableSupportsMethodInterface::hasCacheableSupportsMethod() is deprecated, use "getSupportedTypes(?string $format)" return value instead.');
7476

7577
return true;
7678
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @author Kévin Dunglas <[email protected]>
2222
*
23-
* @deprecated since Symfony 6.3, implement "getSupportedTypes()" instead
23+
* @deprecated since Symfony 6.3, implement "getSupportedTypes(?string $format)" instead
2424
*/
2525
interface CacheableSupportsMethodInterface
2626
{

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @author Grégoire Pineau <[email protected]>
2323
* @author Kévin Dunglas <[email protected]>
2424
*/
25-
class ConstraintViolationListNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface, SupportedTypesMethodInterface
25+
class ConstraintViolationListNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
2626
{
2727
public const INSTANCE = 'instance';
2828
public const STATUS = 'status';
@@ -39,7 +39,7 @@ public function __construct(array $defaultContext = [], NameConverterInterface $
3939
$this->nameConverter = $nameConverter;
4040
}
4141

42-
public function getSupportedTypes(): ?array
42+
public function getSupportedTypes(?string $format): ?array
4343
{
4444
return [
4545
ConstraintViolationListInterface::class => __CLASS__ == static::class,
@@ -108,17 +108,15 @@ public function normalize(mixed $object, string $format = null, array $context =
108108
return $result + ['violations' => $violations];
109109
}
110110

111-
/**
112-
* @param array $context
113-
*/
114111
public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */): bool
115112
{
113+
// @deprecated return true in 7.0
116114
return $data instanceof ConstraintViolationListInterface;
117115
}
118116

119117
public function hasCacheableSupportsMethod(): bool
120118
{
121-
trigger_deprecation('symfony/serializer', '6.3', 'CacheableSupportsMethodInterface::hasCacheableSupportsMethod() is deprecated, use "getSupportedTypes()" return value instead.');
119+
trigger_deprecation('symfony/serializer', '6.3', 'CacheableSupportsMethodInterface::hasCacheableSupportsMethod() is deprecated, use "getSupportedTypes(?string $format)" return value instead.');
122120

123121
return __CLASS__ === static::class;
124122
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
/**
1818
* @author Jordi Boggiano <[email protected]>
1919
*/
20-
class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface, SupportedTypesMethodInterface
20+
class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
2121
{
2222
use ObjectToPopulateTrait;
2323
use SerializerAwareTrait;
2424

25-
public function getSupportedTypes(): array
25+
public function getSupportedTypes(?string $format): array
2626
{
2727
return [
2828
NormalizableInterface::class => __CLASS__ === static::class,
@@ -45,31 +45,31 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
4545
/**
4646
* Checks if the given class implements the NormalizableInterface.
4747
*
48-
* @param mixed $data Data to normalize
49-
* @param string $format The format being (de-)serialized from or into
50-
* @param array $context
48+
* @param mixed $data Data to normalize
49+
* @param string $format The format being (de-)serialized from or into
5150
*/
5251
public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */): bool
5352
{
53+
// @deprecated return true in 7.0
5454
return $data instanceof NormalizableInterface;
5555
}
5656

5757
/**
5858
* Checks if the given class implements the DenormalizableInterface.
5959
*
60-
* @param mixed $data Data to denormalize from
61-
* @param string $type The class to which the data should be denormalized
62-
* @param string $format The format being deserialized from
63-
* @param array $context
60+
* @param mixed $data Data to denormalize from
61+
* @param string $type The class to which the data should be denormalized
62+
* @param string $format The format being deserialized from
6463
*/
6564
public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */): bool
6665
{
66+
// @deprecated return true in 7.0
6767
return is_subclass_of($type, DenormalizableInterface::class);
6868
}
6969

7070
public function hasCacheableSupportsMethod(): bool
7171
{
72-
trigger_deprecation('symfony/serializer', '6.3', 'CacheableSupportsMethodInterface::hasCacheableSupportsMethod() is deprecated, use "getSupportedTypes()" return value instead.');
72+
trigger_deprecation('symfony/serializer', '6.3', 'CacheableSupportsMethodInterface::hasCacheableSupportsMethod() is deprecated, use "getSupportedTypes(?string $format)" return value instead.');
7373

7474
return __CLASS__ === static::class;
7575
}

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @author Kévin Dunglas <[email protected]>
2525
*/
26-
class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface, SupportedTypesMethodInterface
26+
class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
2727
{
2828
private const SUPPORTED_TYPES = [
2929
\SplFileInfo::class => true,
@@ -45,7 +45,7 @@ public function __construct(MimeTypeGuesserInterface $mimeTypeGuesser = null)
4545
$this->mimeTypeGuesser = $mimeTypeGuesser;
4646
}
4747

48-
public function getSupportedTypes(): array
48+
public function getSupportedTypes(?string $format): array
4949
{
5050
return self::SUPPORTED_TYPES;
5151
}
@@ -73,11 +73,9 @@ public function normalize(mixed $object, string $format = null, array $context =
7373
return sprintf('data:%s;base64,%s', $mimeType, base64_encode($data));
7474
}
7575

76-
/**
77-
* @param array $context
78-
*/
7976
public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */): bool
8077
{
78+
// @deprecated return true in 7.0
8179
return $data instanceof \SplFileInfo;
8280
}
8381

@@ -115,17 +113,15 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
115113
throw new InvalidArgumentException(sprintf('The class parameter "%s" is not supported. It must be one of "SplFileInfo", "SplFileObject" or "Symfony\Component\HttpFoundation\File\File".', $type));
116114
}
117115

118-
/**
119-
* @param array $context
120-
*/
121116
public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */): bool
122117
{
118+
// @deprecated return true in 7.0
123119
return isset(self::SUPPORTED_TYPES[$type]);
124120
}
125121

126122
public function hasCacheableSupportsMethod(): bool
127123
{
128-
trigger_deprecation('symfony/serializer', '6.3', 'CacheableSupportsMethodInterface::hasCacheableSupportsMethod() is deprecated, use "getSupportedTypes()" return value instead.');
124+
trigger_deprecation('symfony/serializer', '6.3', 'CacheableSupportsMethodInterface::hasCacheableSupportsMethod() is deprecated, use "getSupportedTypes(?string $format)" return value instead.');
129125

130126
return __CLASS__ === static::class;
131127
}

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @author Jérôme Parmentier <[email protected]>
2222
*/
23-
class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface, SupportedTypesMethodInterface
23+
class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
2424
{
2525
public const FORMAT_KEY = 'dateinterval_format';
2626

@@ -33,7 +33,7 @@ public function __construct(array $defaultContext = [])
3333
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
3434
}
3535

36-
public function getSupportedTypes(): array
36+
public function getSupportedTypes(?string $format): array
3737
{
3838
return [
3939
\DateInterval::class => __CLASS__ === static::class,
@@ -52,17 +52,15 @@ public function normalize(mixed $object, string $format = null, array $context =
5252
return $object->format($context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY]);
5353
}
5454

55-
/**
56-
* @param array $context
57-
*/
5855
public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */): bool
5956
{
57+
// @deprecated return true in 7.0
6058
return $data instanceof \DateInterval;
6159
}
6260

6361
public function hasCacheableSupportsMethod(): bool
6462
{
65-
trigger_deprecation('symfony/serializer', '6.3', 'CacheableSupportsMethodInterface::hasCacheableSupportsMethod() is deprecated, use "getSupportedTypes()" return value instead.');
63+
trigger_deprecation('symfony/serializer', '6.3', 'CacheableSupportsMethodInterface::hasCacheableSupportsMethod() is deprecated, use "getSupportedTypes(?string $format)" return value instead.');
6664

6765
return __CLASS__ === static::class;
6866
}
@@ -117,11 +115,9 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
117115
}
118116
}
119117

120-
/**
121-
* @param array $context
122-
*/
123118
public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */): bool
124119
{
120+
// @deprecated return true in 7.0
125121
return \DateInterval::class === $type;
126122
}
127123

0 commit comments

Comments
 (0)