Completed
Push — master ( d53149...bf867e )
by Antoine
20s queued 11s
created

ItemNormalizer::normalize()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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

198
                /** @scrutinizer ignore-call */ 
199
                $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...
199
            }
200
201
            if ('one' === $relation['cardinality']) {
202
                if ('links' === $type) {
203
                    $data[$key][$relationName]['href'] = $this->getRelationIri($attributeValue);
204
                    continue;
205
                }
206
207
                $data[$key][$relationName] = $attributeValue;
208
                continue;
209
            }
210
211
            // many
212
            $data[$key][$relationName] = [];
213
            foreach ($attributeValue as $rel) {
214
                if ('links' === $type) {
215
                    $rel = ['href' => $this->getRelationIri($rel)];
216
                }
217
218
                $data[$key][$relationName][] = $rel;
219
            }
220
        }
221
222
        return $data;
223
    }
224
225
    /**
226
     * Gets the IRI of the given relation.
227
     *
228
     * @param array|string $rel
229
     */
230
    private function getRelationIri($rel): string
231
    {
232
        return $rel['_links']['self']['href'] ?? $rel;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $rel['_links']['self']['href'] ?? $rel could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
233
    }
234
235
    /**
236
     * Gets the cache key to use.
237
     *
238
     * @return bool|string
239
     */
240
    private function getHalCacheKey(string $format = null, array $context)
241
    {
242
        try {
243
            return md5($format.serialize($context));
244
        } catch (\Exception $exception) {
245
            // The context cannot be serialized, skip the cache
246
            return false;
247
        }
248
    }
249
250
    /**
251
     * Is the max depth reached for the given attribute?
252
     *
253
     * @param AttributeMetadataInterface[] $attributesMetadata
254
     */
255
    private function isMaxDepthReached(array $attributesMetadata, string $class, string $attribute, array &$context): bool
256
    {
257
        if (
258
            !($context[self::ENABLE_MAX_DEPTH] ?? false) ||
259
            !isset($attributesMetadata[$attribute]) ||
260
            null === $maxDepth = $attributesMetadata[$attribute]->getMaxDepth()
261
        ) {
262
            return false;
263
        }
264
265
        $key = sprintf(self::DEPTH_KEY_PATTERN, $class, $attribute);
266
        if (!isset($context[$key])) {
267
            $context[$key] = 1;
268
269
            return false;
270
        }
271
272
        if ($context[$key] === $maxDepth) {
273
            return true;
274
        }
275
276
        ++$context[$key];
277
278
        return false;
279
    }
280
}
281