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

Skip to content

Commit 5ae5f04

Browse files
committed
fix: a check, and add tests
1 parent 80e74fc commit 5ae5f04

33 files changed

Lines changed: 169 additions & 31 deletions

src/Elasticsearch/Serializer/ItemNormalizer.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
1717
use Symfony\Component\Serializer\Exception\LogicException;
18+
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
1819
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
1920
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2021
use Symfony\Component\Serializer\SerializerAwareInterface;
@@ -39,8 +40,10 @@ public function __construct(private readonly NormalizerInterface $decorated)
3940
*/
4041
public function hasCacheableSupportsMethod(): bool
4142
{
42-
if (!$this->decorated instanceof CacheableSupportsMethodInterface) {
43-
throw new LogicException(sprintf('The decorated normalizer must be an instance of "%s".', CacheableSupportsMethodInterface::class));
43+
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
44+
45+
if (!$this->decorated instanceof BaseCacheableSupportsMethodInterface) {
46+
throw new LogicException(sprintf('The decorated normalizer must be an instance of "%s".', BaseCacheableSupportsMethodInterface::class));
4447
}
4548

4649
return $this->decorated->hasCacheableSupportsMethod();
@@ -96,11 +99,11 @@ public function getSupportedTypes($format): array
9699
if (!method_exists($this->decorated, 'getSupportedTypes')) {
97100
return [
98101
DocumentNormalizer::FORMAT => null,
99-
'*' => $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
102+
'*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
100103
];
101104
}
102105

103-
return DocumentNormalizer::FORMAT !== $format ? $this->decorated->getSupportedTypes($format) : [];
106+
return DocumentNormalizer::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : [];
104107
}
105108

106109
/**

src/GraphQl/Serializer/ObjectNormalizer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use ApiPlatform\Metadata\Util\ClassInfoTrait;
1919
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
2020
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
21+
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
2122
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2223

2324
/**
@@ -49,7 +50,7 @@ public function getSupportedTypes($format): array
4950
// @deprecated remove condition when support for symfony versions under 6.3 is dropped
5051
if (!method_exists($this->decorated, 'getSupportedTypes')) {
5152
return [
52-
'*' => $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
53+
'*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
5354
];
5455
}
5556

@@ -63,7 +64,7 @@ public function hasCacheableSupportsMethod(): bool
6364
{
6465
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
6566

66-
return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
67+
return $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
6768
}
6869

6970
/**

src/Hal/Serializer/ObjectNormalizer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Api\IriConverterInterface;
1717
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
1818
use Symfony\Component\Serializer\Exception\LogicException;
19+
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
1920
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
2021
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2122

@@ -44,7 +45,7 @@ public function getSupportedTypes($format): array
4445
// @deprecated remove condition when support for symfony versions under 6.3 is dropped
4546
if (!method_exists($this->decorated, 'getSupportedTypes')) {
4647
return [
47-
'*' => $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
48+
'*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
4849
];
4950
}
5051

@@ -58,7 +59,7 @@ public function hasCacheableSupportsMethod(): bool
5859
{
5960
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
6061

61-
return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
62+
return $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
6263
}
6364

6465
/**

src/Hydra/Serializer/CollectionFiltersNormalizer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Psr\Container\ContainerInterface;
2323
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
2424
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
25+
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
2526
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
2627
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2728

@@ -54,7 +55,7 @@ public function getSupportedTypes($format): array
5455
{
5556
// @deprecated remove condition when support for symfony versions under 6.3 is dropped
5657
if (!method_exists($this->collectionNormalizer, 'getSupportedTypes')) {
57-
return ['*' => $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod()];
58+
return ['*' => $this->collectionNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod()];
5859
}
5960

6061
return $this->collectionNormalizer->getSupportedTypes($format);
@@ -64,7 +65,7 @@ public function hasCacheableSupportsMethod(): bool
6465
{
6566
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
6667

67-
return $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
68+
return $this->collectionNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
6869
}
6970

7071
/**

src/Hydra/Serializer/PartialCollectionViewNormalizer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\PropertyAccess\PropertyAccess;
2323
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
2424
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
25+
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
2526
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
2627
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2728

@@ -111,7 +112,7 @@ public function getSupportedTypes($format): array
111112
// @deprecated remove condition when support for symfony versions under 6.3 is dropped
112113
if (!method_exists($this->collectionNormalizer, 'getSupportedTypes')) {
113114
return [
114-
'*' => $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod(),
115+
'*' => $this->collectionNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod(),
115116
];
116117
}
117118

@@ -122,7 +123,7 @@ public function hasCacheableSupportsMethod(): bool
122123
{
123124
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
124125

125-
return $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
126+
return $this->collectionNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
126127
}
127128

128129
/**

src/JsonApi/Serializer/ConstraintViolationListNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function supportsNormalization(mixed $data, string $format = null, array
6161

6262
public function getSupportedTypes($format): array
6363
{
64-
return (self::FORMAT === $format) ? [ConstraintViolationListInterface::class => true] : [];
64+
return self::FORMAT === $format ? [ConstraintViolationListInterface::class => true] : [];
6565
}
6666

6767
public function hasCacheableSupportsMethod(): bool

src/JsonApi/Serializer/ObjectNormalizer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
1919
use ApiPlatform\Metadata\Util\ClassInfoTrait;
2020
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
21+
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
2122
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2223

2324
/**
@@ -47,7 +48,7 @@ public function getSupportedTypes($format): array
4748
// @deprecated remove condition when support for symfony versions under 6.3 is dropped
4849
if (!method_exists($this->decorated, 'getSupportedTypes')) {
4950
return [
50-
'*' => $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
51+
'*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
5152
];
5253
}
5354

@@ -58,7 +59,7 @@ public function hasCacheableSupportsMethod(): bool
5859
{
5960
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
6061

61-
return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
62+
return $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
6263
}
6364

6465
/**

src/JsonLd/Serializer/ObjectNormalizer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use ApiPlatform\Exception\InvalidArgumentException;
1818
use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
1919
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
20+
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
2021
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2122

2223
/**
@@ -46,7 +47,7 @@ public function getSupportedTypes($format): array
4647
// @deprecated remove condition when support for symfony versions under 6.3 is dropped
4748
if (!method_exists($this->decorated, 'getSupportedTypes')) {
4849
return [
49-
'*' => $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
50+
'*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
5051
];
5152
}
5253

@@ -57,7 +58,7 @@ public function hasCacheableSupportsMethod(): bool
5758
{
5859
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
5960

60-
return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
61+
return $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
6162
}
6263

6364
/**

src/OpenApi/Serializer/ApiGatewayNormalizer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
namespace ApiPlatform\OpenApi\Serializer;
1515

16-
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
1716
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
17+
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
1818
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1919

2020
/**
@@ -124,7 +124,7 @@ public function getSupportedTypes($format): array
124124
{
125125
// @deprecated remove condition when support for symfony versions under 6.3 is dropped
126126
if (!method_exists($this->documentationNormalizer, 'getSupportedTypes')) {
127-
return ['*' => $this->documentationNormalizer instanceof CacheableSupportsMethodInterface && $this->documentationNormalizer->hasCacheableSupportsMethod()];
127+
return ['*' => $this->documentationNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->documentationNormalizer->hasCacheableSupportsMethod()];
128128
}
129129

130130
return $this->documentationNormalizer->getSupportedTypes($format);
@@ -137,7 +137,7 @@ public function hasCacheableSupportsMethod(): bool
137137
{
138138
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
139139

140-
return $this->documentationNormalizer instanceof CacheableSupportsMethodInterface && $this->documentationNormalizer->hasCacheableSupportsMethod();
140+
return $this->documentationNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->documentationNormalizer->hasCacheableSupportsMethod();
141141
}
142142

143143
private function isLocalRef(string $ref): bool
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\OpenApi\Serializer;
15+
16+
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
17+
use Symfony\Component\Serializer\Serializer;
18+
19+
if (method_exists(Serializer::class, 'getSupportedTypes')) {
20+
/**
21+
* Backward compatibility layer for getSupportedTypes().
22+
*
23+
* @internal
24+
*
25+
* @author Kévin Dunglas <[email protected]>
26+
*
27+
* @todo remove this interface when dropping support for Serializer < 6.3
28+
*/
29+
interface CacheableSupportsMethodInterface
30+
{
31+
public function getSupportedTypes(?string $format): array;
32+
}
33+
} else {
34+
/**
35+
* Backward compatibility layer for NormalizerInterface::getSupportedTypes().
36+
*
37+
* @internal
38+
*
39+
* @author Kévin Dunglas <[email protected]>
40+
*
41+
* @todo remove this interface when dropping support for Serializer < 6.3
42+
*/
43+
interface CacheableSupportsMethodInterface extends BaseCacheableSupportsMethodInterface
44+
{
45+
}
46+
}

0 commit comments

Comments
 (0)