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\Core\Api; |
||||
15 | |||||
16 | use ApiPlatform\Core\Exception\InvalidArgumentException; |
||||
17 | use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
||||
18 | |||||
19 | /** |
||||
20 | * {@inheritdoc} |
||||
21 | * |
||||
22 | * @author Anthony GRASSIOT <[email protected]> |
||||
23 | * |
||||
24 | * @deprecated since API Platform 2.5, use the "formats" attribute instead |
||||
25 | */ |
||||
26 | final class FormatsProvider implements FormatsProviderInterface, OperationAwareFormatsProviderInterface |
||||
0 ignored issues
–
show
The interface
ApiPlatform\Core\Api\FormatsProviderInterface has been deprecated: since API Platform 2.5, use the "formats" attribute instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This interface has been deprecated. The supplier of the interface has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.
Loading history...
|
|||||
27 | { |
||||
28 | private $configuredFormats; |
||||
29 | private $resourceMetadataFactory; |
||||
30 | |||||
31 | public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, array $configuredFormats) |
||||
32 | { |
||||
33 | @trigger_error(sprintf('The "%s" class is deprecated since API Platform 2.5, use the "formats" attribute instead.', __CLASS__), E_USER_DEPRECATED); |
||||
34 | |||||
35 | $this->resourceMetadataFactory = $resourceMetadataFactory; |
||||
36 | $this->configuredFormats = $configuredFormats; |
||||
37 | } |
||||
38 | |||||
39 | /** |
||||
40 | * {@inheritdoc} |
||||
41 | * |
||||
42 | * @throws InvalidArgumentException |
||||
43 | */ |
||||
44 | public function getFormatsFromAttributes(array $attributes): array |
||||
45 | { |
||||
46 | if (!$attributes || !isset($attributes['resource_class'])) { |
||||
0 ignored issues
–
show
The expression
$attributes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using
Loading history...
|
|||||
47 | return $this->configuredFormats; |
||||
48 | } |
||||
49 | |||||
50 | $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']); |
||||
51 | |||||
52 | if (!$formats = $resourceMetadata->getOperationAttribute($attributes, 'formats', [], true)) { |
||||
53 | return $this->configuredFormats; |
||||
54 | } |
||||
55 | |||||
56 | if (!\is_array($formats)) { |
||||
57 | throw new InvalidArgumentException(sprintf("The 'formats' attributes must be an array, %s given for resource class '%s'.", \gettype($formats), $attributes['resource_class'])); |
||||
58 | } |
||||
59 | |||||
60 | return $this->getOperationFormats($formats); |
||||
61 | } |
||||
62 | |||||
63 | /** |
||||
64 | * {@inheritdoc} |
||||
65 | * |
||||
66 | * @throws InvalidArgumentException |
||||
67 | */ |
||||
68 | public function getFormatsFromOperation(string $resourceClass, string $operationName, string $operationType): array |
||||
69 | { |
||||
70 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||||
71 | |||||
72 | if (!$formats = $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'formats', [], true)) { |
||||
73 | return $this->configuredFormats; |
||||
74 | } |
||||
75 | |||||
76 | if (!\is_array($formats)) { |
||||
77 | throw new InvalidArgumentException(sprintf("The 'formats' attributes must be an array, %s given for resource class '%s'.", \gettype($formats), $resourceClass)); |
||||
78 | } |
||||
79 | |||||
80 | return $this->getOperationFormats($formats); |
||||
81 | } |
||||
82 | |||||
83 | /** |
||||
84 | * Filter and populate the acceptable formats. |
||||
85 | * |
||||
86 | * @throws InvalidArgumentException |
||||
87 | */ |
||||
88 | private function getOperationFormats(array $annotationFormats): array |
||||
89 | { |
||||
90 | $resourceFormats = []; |
||||
91 | foreach ($annotationFormats as $format => $value) { |
||||
92 | if (!is_numeric($format)) { |
||||
93 | $resourceFormats[$format] = (array) $value; |
||||
94 | continue; |
||||
95 | } |
||||
96 | if (!\is_string($value)) { |
||||
97 | throw new InvalidArgumentException(sprintf("The 'formats' attributes value must be a string when trying to include an already configured format, %s given.", \gettype($value))); |
||||
98 | } |
||||
99 | if (\array_key_exists($value, $this->configuredFormats)) { |
||||
100 | $resourceFormats[$value] = $this->configuredFormats[$value]; |
||||
101 | continue; |
||||
102 | } |
||||
103 | |||||
104 | throw new InvalidArgumentException(sprintf("You either need to add the format '%s' to your project configuration or declare a mime type for it in your annotation.", $value)); |
||||
105 | } |
||||
106 | |||||
107 | return $resourceFormats; |
||||
108 | } |
||||
109 | } |
||||
110 |
This interface has been deprecated. The supplier of the interface has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.