Completed
Push — master ( ac8ec4...1a57ac )
by Kévin
04:37 queued 11s
created

src/Hal/Serializer/ItemNormalizer.php (1 issue)

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\Hal\Serializer;
15
16
use ApiPlatform\Core\Serializer\AbstractItemNormalizer;
17
use ApiPlatform\Core\Serializer\CacheKeyTrait;
18
use ApiPlatform\Core\Serializer\ContextTrait;
19
use ApiPlatform\Core\Util\ClassInfoTrait;
20
use Symfony\Component\Serializer\Exception\LogicException;
21
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
22
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
23
24
/**
25
 * Converts between objects and array including HAL metadata.
26
 *
27
 * @author Kévin Dunglas <[email protected]>
28
 */
29
final class ItemNormalizer extends AbstractItemNormalizer
30
{
31
    use CacheKeyTrait;
32
    use ClassInfoTrait;
33
    use ContextTrait;
34
35
    public const FORMAT = 'jsonhal';
36
37
    private $componentsCache = [];
38
    private $attributesMetadataCache = [];
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function supportsNormalization($data, $format = null): bool
44
    {
45
        return self::FORMAT === $format && parent::supportsNormalization($data, $format);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function normalize($object, $format = null, array $context = [])
52
    {
53
        if (null !== $this->getOutputClass($this->getObjectClass($object), $context)) {
54
            return parent::normalize($object, $format, $context);
55
        }
56
57
        if (!isset($context['cache_key'])) {
58
            $context['cache_key'] = $this->getCacheKey($format, $context);
59
        }
60
61
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null);
62
        $context = $this->initContext($resourceClass, $context);
63
        $iri = $this->iriConverter->getIriFromItem($object);
64
        $context['iri'] = $iri;
65
        $context['api_normalize'] = true;
66
67
        $data = parent::normalize($object, $format, $context);
68
        if (!\is_array($data)) {
69
            return $data;
70
        }
71
72
        $metadata = [
73
            '_links' => [
74
                'self' => [
75
                    'href' => $iri,
76
                ],
77
            ],
78
        ];
79
        $components = $this->getComponents($object, $format, $context);
80
        $metadata = $this->populateRelation($metadata, $object, $format, $context, $components, 'links');
81
        $metadata = $this->populateRelation($metadata, $object, $format, $context, $components, 'embedded');
82
83
        return $metadata + $data;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function supportsDenormalization($data, $type, $format = null): bool
90
    {
91
        // prevent the use of lower priority normalizers (e.g. serializer.normalizer.object) for this format
92
        return self::FORMAT === $format;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     *
98
     * @throws LogicException
99
     */
100
    public function denormalize($data, $class, $format = null, array $context = [])
101
    {
102
        throw new LogicException(sprintf('%s is a read-only format.', self::FORMAT));
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    protected function getAttributes($object, $format = null, array $context): array
109
    {
110
        return $this->getComponents($object, $format, $context)['states'];
111
    }
112
113
    /**
114
     * Gets HAL components of the resource: states, links and embedded.
115
     *
116
     * @param object $object
117
     */
118
    private function getComponents($object, ?string $format, array $context): array
119
    {
120
        $cacheKey = $this->getObjectClass($object).'-'.$context['cache_key'];
121
122
        if (isset($this->componentsCache[$cacheKey])) {
123
            return $this->componentsCache[$cacheKey];
124
        }
125
126
        $attributes = parent::getAttributes($object, $format, $context);
127
        $options = $this->getFactoryOptions($context);
128
129
        $components = [
130
            'states' => [],
131
            'links' => [],
132
            'embedded' => [],
133
        ];
134
135
        foreach ($attributes as $attribute) {
136
            $propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $options);
137
138
            $type = $propertyMetadata->getType();
139
            $isOne = $isMany = false;
140
141
            if (null !== $type) {
142
                if ($type->isCollection()) {
143
                    $valueType = $type->getCollectionValueType();
144
                    $isMany = null !== $valueType && ($className = $valueType->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
145
                } else {
146
                    $className = $type->getClassName();
147
                    $isOne = $className && $this->resourceClassResolver->isResourceClass($className);
148
                }
149
            }
150
151
            if (!$isOne && !$isMany) {
152
                $components['states'][] = $attribute;
153
                continue;
154
            }
155
156
            $relation = ['name' => $attribute, 'cardinality' => $isOne ? 'one' : 'many'];
157
            if ($propertyMetadata->isReadableLink()) {
158
                $components['embedded'][] = $relation;
159
            }
160
161
            $components['links'][] = $relation;
162
        }
163
164
        if (false !== $context['cache_key']) {
165
            $this->componentsCache[$cacheKey] = $components;
166
        }
167
168
        return $components;
169
    }
170
171
    /**
172
     * Populates _links and _embedded keys.
173
     *
174
     * @param object $object
175
     */
176
    private function populateRelation(array $data, $object, ?string $format, array $context, array $components, string $type): array
177
    {
178
        $class = $this->getObjectClass($object);
179
180
        $attributesMetadata = \array_key_exists($class, $this->attributesMetadataCache) ?
181
            $this->attributesMetadataCache[$class] :
182
            $this->attributesMetadataCache[$class] = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null;
183
184
        $key = '_'.$type;
185
        foreach ($components[$type] as $relation) {
186
            if (null !== $attributesMetadata && $this->isMaxDepthReached($attributesMetadata, $class, $relation['name'], $context)) {
187
                continue;
188
            }
189
190
            $attributeValue = $this->getAttributeValue($object, $relation['name'], $format, $context);
191
            if (empty($attributeValue)) {
192
                continue;
193
            }
194
195
            $relationName = $relation['name'];
196
            if ($this->nameConverter) {
197
                $relationName = $this->nameConverter->normalize($relationName, $class, $format, $context);
0 ignored issues
show
The call to Symfony\Component\Serial...rInterface::normalize() has too many arguments starting with $class. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

197
                /** @scrutinizer ignore-call */ 
198
                $relationName = $this->nameConverter->normalize($relationName, $class, $format, $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
198
            }
199
200
            if ('one' === $relation['cardinality']) {
201
                if ('links' === $type) {
202
                    $data[$key][$relationName]['href'] = $this->getRelationIri($attributeValue);
203
                    continue;
204
                }
205
206
                $data[$key][$relationName] = $attributeValue;
207
                continue;
208
            }
209
210
            // many
211
            $data[$key][$relationName] = [];
212
            foreach ($attributeValue as $rel) {
213
                if ('links' === $type) {
214
                    $rel = ['href' => $this->getRelationIri($rel)];
215
                }
216
217
                $data[$key][$relationName][] = $rel;
218
            }
219
        }
220
221
        return $data;
222
    }
223
224
    /**
225
     * Gets the IRI of the given relation.
226
     *
227
     * @throws UnexpectedValueException
228
     */
229
    private function getRelationIri($rel): string
230
    {
231
        if (!(\is_array($rel) || \is_string($rel))) {
232
            throw new UnexpectedValueException('Expected relation to be an IRI or array');
233
        }
234
235
        return \is_string($rel) ? $rel : $rel['_links']['self']['href'];
236
    }
237
238
    /**
239
     * Is the max depth reached for the given attribute?
240
     *
241
     * @param AttributeMetadataInterface[] $attributesMetadata
242
     */
243
    private function isMaxDepthReached(array $attributesMetadata, string $class, string $attribute, array &$context): bool
244
    {
245
        if (
246
            !($context[self::ENABLE_MAX_DEPTH] ?? false) ||
247
            !isset($attributesMetadata[$attribute]) ||
248
            null === $maxDepth = $attributesMetadata[$attribute]->getMaxDepth()
249
        ) {
250
            return false;
251
        }
252
253
        $key = sprintf(self::DEPTH_KEY_PATTERN, $class, $attribute);
254
        if (!isset($context[$key])) {
255
            $context[$key] = 1;
256
257
            return false;
258
        }
259
260
        if ($context[$key] === $maxDepth) {
261
            return true;
262
        }
263
264
        ++$context[$key];
265
266
        return false;
267
    }
268
}
269