• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 11590717871

30 Oct 2024 09:50AM UTC coverage: 62.063% (-0.04%) from 62.1%
11590717871

push

github

web-flow
fix(serializer): fetch type on normalization error when possible (#6761)

0 of 12 new or added lines in 1 file covered. (0.0%)

1 existing line in 1 file now uncovered.

11450 of 18449 relevant lines covered (62.06%)

67.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

48.62
/src/Serializer/AbstractItemNormalizer.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Serializer;
15

16
use ApiPlatform\Api\IriConverterInterface as LegacyIriConverterInterface;
17
use ApiPlatform\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
18
use ApiPlatform\Metadata\ApiProperty;
19
use ApiPlatform\Metadata\CollectionOperationInterface;
20
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
21
use ApiPlatform\Metadata\Exception\ItemNotFoundException;
22
use ApiPlatform\Metadata\IriConverterInterface;
23
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
24
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
25
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
26
use ApiPlatform\Metadata\ResourceAccessCheckerInterface;
27
use ApiPlatform\Metadata\ResourceClassResolverInterface;
28
use ApiPlatform\Metadata\UrlGeneratorInterface;
29
use ApiPlatform\Metadata\Util\ClassInfoTrait;
30
use ApiPlatform\Metadata\Util\CloneTrait;
31
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
32
use Symfony\Component\PropertyAccess\PropertyAccess;
33
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
34
use Symfony\Component\PropertyInfo\Type;
35
use Symfony\Component\Serializer\Encoder\CsvEncoder;
36
use Symfony\Component\Serializer\Encoder\XmlEncoder;
37
use Symfony\Component\Serializer\Exception\LogicException;
38
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
39
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
40
use Symfony\Component\Serializer\Exception\RuntimeException;
41
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
42
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
43
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
44
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
45
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
46
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
47
use Symfony\Component\Serializer\Serializer;
48

49
/**
50
 * Base item normalizer.
51
 *
52
 * @author Kévin Dunglas <dunglas@gmail.com>
53
 */
54
abstract class AbstractItemNormalizer extends AbstractObjectNormalizer
55
{
56
    use ClassInfoTrait;
57
    use CloneTrait;
58
    use ContextTrait;
59
    use InputOutputMetadataTrait;
60
    use OperationContextTrait;
61

62
    protected PropertyAccessorInterface $propertyAccessor;
63
    protected array $localCache = [];
64
    protected array $localFactoryOptionsCache = [];
65
    protected ?ResourceAccessCheckerInterface $resourceAccessChecker;
66

67
    public function __construct(protected PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, protected PropertyMetadataFactoryInterface $propertyMetadataFactory, protected LegacyIriConverterInterface|IriConverterInterface $iriConverter, protected LegacyResourceClassResolverInterface|ResourceClassResolverInterface $resourceClassResolver, ?PropertyAccessorInterface $propertyAccessor = null, ?NameConverterInterface $nameConverter = null, ?ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, ?ResourceAccessCheckerInterface $resourceAccessChecker = null, protected ?TagCollectorInterface $tagCollector = null)
68
    {
69
        if (!isset($defaultContext['circular_reference_handler'])) {
515✔
70
            $defaultContext['circular_reference_handler'] = fn ($object): ?string => $this->iriConverter->getIriFromResource($object);
515✔
71
        }
72

73
        parent::__construct($classMetadataFactory, $nameConverter, null, null, \Closure::fromCallable($this->getObjectClass(...)), $defaultContext);
515✔
74
        $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
515✔
75
        $this->resourceAccessChecker = $resourceAccessChecker;
515✔
76
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
515✔
77
    }
78

79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
83
    {
84
        if (!\is_object($data) || is_iterable($data)) {
415✔
85
            return false;
80✔
86
        }
87

88
        $class = $context['force_resource_class'] ?? $this->getObjectClass($data);
391✔
89
        if (($context['output']['class'] ?? null) === $class) {
391✔
90
            return true;
16✔
91
        }
92

93
        return $this->resourceClassResolver->isResourceClass($class);
383✔
94
    }
95

96
    public function getSupportedTypes(?string $format): array
97
    {
98
        return [
427✔
99
            'object' => true,
427✔
100
        ];
427✔
101
    }
102

103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function hasCacheableSupportsMethod(): bool
107
    {
108
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
109
            trigger_deprecation(
×
110
                'api-platform/core',
×
111
                '3.1',
×
112
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
113
                __METHOD__
×
114
            );
×
115
        }
116

117
        return true;
×
118
    }
119

120
    /**
121
     * {@inheritdoc}
122
     *
123
     * @throws LogicException
124
     */
125
    public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
126
    {
127
        $resourceClass = $context['force_resource_class'] ?? $this->getObjectClass($object);
403✔
128
        if ($outputClass = $this->getOutputClass($context)) {
403✔
129
            if (!$this->serializer instanceof NormalizerInterface) {
16✔
130
                throw new LogicException('Cannot normalize the output because the injected serializer is not a normalizer');
×
131
            }
132

133
            unset($context['output'], $context['operation'], $context['operation_name']);
16✔
134
            $context['resource_class'] = $outputClass;
16✔
135
            $context['api_sub_level'] = true;
16✔
136
            $context[self::ALLOW_EXTRA_ATTRIBUTES] = false;
16✔
137

138
            return $this->serializer->normalize($object, $format, $context);
16✔
139
        }
140

141
        // Never remove this, with `application/json` we don't use our AbstractCollectionNormalizer and we need
142
        // to remove the collection operation from our context or we'll introduce security issues
143
        if (isset($context['operation']) && $context['operation'] instanceof CollectionOperationInterface) {
403✔
144
            unset($context['operation_name'], $context['operation'], $context['iri']);
8✔
145
        }
146

147
        if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
403✔
148
            $context = $this->initContext($resourceClass, $context);
387✔
149
        }
150

151
        $context['api_normalize'] = true;
403✔
152
        $iri = $context['iri'] ??= $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_URL, $context['operation'] ?? null, $context);
403✔
153

154
        /*
155
         * When true, converts the normalized data array of a resource into an
156
         * IRI, if the normalized data array is empty.
157
         *
158
         * This is useful when traversing from a non-resource towards an attribute
159
         * which is a resource, as we do not have the benefit of {@see ApiProperty::isReadableLink}.
160
         *
161
         * It must not be propagated to resources, as {@see ApiProperty::isReadableLink}
162
         * should take effect.
163
         */
164
        $emptyResourceAsIri = $context['api_empty_resource_as_iri'] ?? false;
403✔
165
        unset($context['api_empty_resource_as_iri']);
403✔
166

167
        if (!$this->tagCollector && isset($context['resources'])) {
403✔
168
            $context['resources'][$iri] = $iri;
×
169
        }
170

171
        $context['object'] = $object;
403✔
172
        $context['format'] = $format;
403✔
173

174
        $data = parent::normalize($object, $format, $context);
403✔
175

176
        $context['data'] = $data;
403✔
177
        unset($context['property_metadata'], $context['api_attribute']);
403✔
178

179
        if ($emptyResourceAsIri && \is_array($data) && 0 === \count($data)) {
403✔
180
            $context['data'] = $iri;
×
181

182
            if ($this->tagCollector) {
×
183
                $this->tagCollector->collect($context);
×
184
            }
185

186
            return $iri;
×
187
        }
188

189
        if ($this->tagCollector) {
403✔
190
            $this->tagCollector->collect($context);
348✔
191
        }
192

193
        return $data;
403✔
194
    }
195

196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
200
    {
201
        if (($context['input']['class'] ?? null) === $type) {
12✔
202
            return true;
×
203
        }
204

205
        return $this->localCache[$type] ?? $this->localCache[$type] = $this->resourceClassResolver->isResourceClass($type);
12✔
206
    }
207

208
    /**
209
     * {@inheritdoc}
210
     */
211
    public function denormalize(mixed $data, string $class, ?string $format = null, array $context = []): mixed
212
    {
213
        $resourceClass = $class;
12✔
214

215
        if ($inputClass = $this->getInputClass($context)) {
12✔
216
            if (!$this->serializer instanceof DenormalizerInterface) {
×
217
                throw new LogicException('Cannot denormalize the input because the injected serializer is not a denormalizer');
×
218
            }
219

220
            unset($context['input'], $context['operation'], $context['operation_name'], $context['uri_variables']);
×
221
            $context['resource_class'] = $inputClass;
×
222

223
            try {
224
                return $this->serializer->denormalize($data, $inputClass, $format, $context);
×
225
            } catch (NotNormalizableValueException $e) {
×
226
                throw new UnexpectedValueException('The input data is misformatted.', $e->getCode(), $e);
×
227
            }
228
        }
229

230
        if (null === $objectToPopulate = $this->extractObjectToPopulate($resourceClass, $context, static::OBJECT_TO_POPULATE)) {
12✔
231
            $normalizedData = \is_scalar($data) ? [$data] : $this->prepareForDenormalization($data);
12✔
232
            $class = $this->getClassDiscriminatorResolvedClass($normalizedData, $class, $context);
12✔
233
        }
234

235
        $context['api_denormalize'] = true;
12✔
236

237
        if ($this->resourceClassResolver->isResourceClass($class)) {
12✔
238
            $resourceClass = $this->resourceClassResolver->getResourceClass($objectToPopulate, $class);
12✔
239
            $context['resource_class'] = $resourceClass;
12✔
240
        }
241

242
        if (\is_string($data)) {
12✔
243
            try {
244
                return $this->iriConverter->getResourceFromIri($data, $context + ['fetch_data' => true]);
×
245
            } catch (ItemNotFoundException $e) {
×
246
                throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
×
247
            } catch (InvalidArgumentException $e) {
×
248
                throw new UnexpectedValueException(\sprintf('Invalid IRI "%s".', $data), $e->getCode(), $e);
×
249
            }
250
        }
251

252
        if (!\is_array($data)) {
12✔
253
            throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type of the "%s" resource must be "array" (nested document) or "string" (IRI), "%s" given.', $resourceClass, \gettype($data)), $data, [Type::BUILTIN_TYPE_ARRAY, Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null);
×
254
        }
255

256
        $previousObject = $this->clone($objectToPopulate);
12✔
257
        $object = parent::denormalize($data, $class, $format, $context);
12✔
258

259
        if (!$this->resourceClassResolver->isResourceClass($class)) {
12✔
260
            return $object;
×
261
        }
262

263
        // Bypass the post-denormalize attribute revert logic if the object could not be
264
        // cloned since we cannot possibly revert any changes made to it.
265
        if (null !== $objectToPopulate && null === $previousObject) {
12✔
266
            return $object;
×
267
        }
268

269
        $options = $this->getFactoryOptions($context);
12✔
270
        $propertyNames = iterator_to_array($this->propertyNameCollectionFactory->create($resourceClass, $options));
12✔
271

272
        // Revert attributes that aren't allowed to be changed after a post-denormalize check
273
        foreach (array_keys($data) as $attribute) {
12✔
274
            $attribute = $this->nameConverter ? $this->nameConverter->denormalize((string) $attribute) : $attribute;
12✔
275
            if (!\in_array($attribute, $propertyNames, true)) {
12✔
276
                continue;
×
277
            }
278

279
            if (!$this->canAccessAttributePostDenormalize($object, $previousObject, $attribute, $context)) {
12✔
280
                if (null !== $previousObject) {
×
281
                    $this->setValue($object, $attribute, $this->propertyAccessor->getValue($previousObject, $attribute));
×
282
                } else {
283
                    $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $attribute, $options);
×
284
                    $this->setValue($object, $attribute, $propertyMetadata->getDefault());
×
285
                }
286
            }
287
        }
288

289
        return $object;
12✔
290
    }
291

292
    /**
293
     * Method copy-pasted from symfony/serializer.
294
     * Remove it after symfony/serializer version update @see https://github.com/symfony/symfony/pull/28263.
295
     *
296
     * {@inheritdoc}
297
     *
298
     * @internal
299
     */
300
    protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, ?string $format = null): object
301
    {
302
        if (null !== $object = $this->extractObjectToPopulate($class, $context, static::OBJECT_TO_POPULATE)) {
12✔
303
            unset($context[static::OBJECT_TO_POPULATE]);
×
304

305
            return $object;
×
306
        }
307

308
        $class = $this->getClassDiscriminatorResolvedClass($data, $class, $context);
12✔
309
        $reflectionClass = new \ReflectionClass($class);
12✔
310

311
        $constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes);
12✔
312
        if ($constructor) {
12✔
313
            $constructorParameters = $constructor->getParameters();
12✔
314

315
            $params = [];
12✔
316
            $missingConstructorArguments = [];
12✔
317
            foreach ($constructorParameters as $constructorParameter) {
12✔
318
                $paramName = $constructorParameter->name;
×
319
                $key = $this->nameConverter ? $this->nameConverter->normalize($paramName, $class, $format, $context) : $paramName;
×
320

321
                $allowed = false === $allowedAttributes || (\is_array($allowedAttributes) && \in_array($paramName, $allowedAttributes, true));
×
322
                $ignored = !$this->isAllowedAttribute($class, $paramName, $format, $context);
×
323
                if ($constructorParameter->isVariadic()) {
×
324
                    if ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) {
×
325
                        if (!\is_array($data[$paramName])) {
×
326
                            throw new RuntimeException(\sprintf('Cannot create an instance of %s from serialized data because the variadic parameter %s can only accept an array.', $class, $constructorParameter->name));
×
327
                        }
328

329
                        $params[] = $data[$paramName];
×
330
                    }
331
                } elseif ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) {
×
332
                    $constructorContext = $context;
×
333
                    $constructorContext['deserialization_path'] = $context['deserialization_path'] ?? $key;
×
334
                    try {
335
                        $params[] = $this->createConstructorArgument($data[$key], $key, $constructorParameter, $constructorContext, $format);
×
336
                    } catch (NotNormalizableValueException $exception) {
×
337
                        if (!isset($context['not_normalizable_value_exceptions'])) {
×
338
                            throw $exception;
×
339
                        }
340
                        $context['not_normalizable_value_exceptions'][] = $exception;
×
341
                    }
342

343
                    // Don't run set for a parameter passed to the constructor
344
                    unset($data[$key]);
×
345
                } elseif (isset($context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key])) {
×
346
                    $params[] = $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
×
347
                } elseif ($constructorParameter->isDefaultValueAvailable()) {
×
348
                    $params[] = $constructorParameter->getDefaultValue();
×
349
                } else {
350
                    if (!isset($context['not_normalizable_value_exceptions'])) {
×
351
                        $missingConstructorArguments[] = $constructorParameter->name;
×
352
                    }
353

NEW
354
                    $attributeContext = $this->getAttributeDenormalizationContext($class, $paramName, $context);
×
NEW
355
                    $constructorParameterType = 'unknown';
×
NEW
356
                    $reflectionType = $constructorParameter->getType();
×
NEW
357
                    if ($reflectionType instanceof \ReflectionNamedType) {
×
NEW
358
                        $constructorParameterType = $reflectionType->getName();
×
359
                    }
360

NEW
361
                    $exception = NotNormalizableValueException::createForUnexpectedDataType(
×
NEW
362
                        \sprintf('Failed to create object because the class misses the "%s" property.', $constructorParameter->name),
×
NEW
363
                        null,
×
NEW
364
                        [$constructorParameterType],
×
NEW
365
                        $attributeContext['deserialization_path'] ?? null,
×
NEW
366
                        true
×
NEW
367
                    );
×
UNCOV
368
                    $context['not_normalizable_value_exceptions'][] = $exception;
×
369
                }
370
            }
371

372
            if ($missingConstructorArguments) {
12✔
373
                throw new MissingConstructorArgumentsException(\sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires the following parameters to be present : "$%s".', $class, implode('", "$', $missingConstructorArguments)), 0, null, $missingConstructorArguments, $class);
×
374
            }
375

376
            if (\count($context['not_normalizable_value_exceptions'] ?? []) > 0) {
12✔
377
                return $reflectionClass->newInstanceWithoutConstructor();
×
378
            }
379

380
            if ($constructor->isConstructor()) {
12✔
381
                return $reflectionClass->newInstanceArgs($params);
12✔
382
            }
383

384
            return $constructor->invokeArgs(null, $params);
×
385
        }
386

387
        return new $class();
×
388
    }
389

390
    protected function getClassDiscriminatorResolvedClass(array $data, string $class, array $context = []): string
391
    {
392
        if (null === $this->classDiscriminatorResolver || (null === $mapping = $this->classDiscriminatorResolver->getMappingForClass($class))) {
12✔
393
            return $class;
12✔
394
        }
395

396
        if (!isset($data[$mapping->getTypeProperty()])) {
×
397
            throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Type property "%s" not found for the abstract object "%s".', $mapping->getTypeProperty(), $class), null, ['string'], isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty());
×
398
        }
399

400
        $type = $data[$mapping->getTypeProperty()];
×
401
        if (null === ($mappedClass = $mapping->getClassForType($type))) {
×
402
            throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type "%s" is not a valid value.', $type), $type, ['string'], isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty(), true);
×
403
        }
404

405
        return $mappedClass;
×
406
    }
407

408
    protected function createConstructorArgument($parameterData, string $key, \ReflectionParameter $constructorParameter, array &$context, ?string $format = null): mixed
409
    {
410
        return $this->createAndValidateAttributeValue($constructorParameter->name, $parameterData, $format, $context);
×
411
    }
412

413
    /**
414
     * {@inheritdoc}
415
     *
416
     * Unused in this context.
417
     *
418
     * @return string[]
419
     */
420
    protected function extractAttributes($object, $format = null, array $context = []): array
421
    {
422
        return [];
×
423
    }
424

425
    /**
426
     * {@inheritdoc}
427
     */
428
    protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool
429
    {
430
        if (!$this->resourceClassResolver->isResourceClass($context['resource_class'])) {
403✔
431
            return parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);
16✔
432
        }
433

434
        $resourceClass = $this->resourceClassResolver->getResourceClass(null, $context['resource_class']); // fix for abstract classes and interfaces
387✔
435
        $options = $this->getFactoryOptions($context);
387✔
436
        $propertyNames = $this->propertyNameCollectionFactory->create($resourceClass, $options);
387✔
437

438
        $allowedAttributes = [];
387✔
439
        foreach ($propertyNames as $propertyName) {
387✔
440
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName, $options);
371✔
441

442
            if (
443
                $this->isAllowedAttribute($classOrObject, $propertyName, null, $context)
371✔
444
                && (isset($context['api_normalize']) && $propertyMetadata->isReadable()
371✔
445
                    || isset($context['api_denormalize']) && ($propertyMetadata->isWritable() || !\is_object($classOrObject) && $propertyMetadata->isInitializable())
371✔
446
                )
447
            ) {
448
                $allowedAttributes[] = $propertyName;
359✔
449
            }
450
        }
451

452
        return $allowedAttributes;
387✔
453
    }
454

455
    /**
456
     * {@inheritdoc}
457
     */
458
    protected function isAllowedAttribute(object|string $classOrObject, string $attribute, ?string $format = null, array $context = []): bool
459
    {
460
        if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) {
387✔
461
            return false;
12✔
462
        }
463

464
        return $this->canAccessAttribute(\is_object($classOrObject) ? $classOrObject : null, $attribute, $context);
387✔
465
    }
466

467
    /**
468
     * Check if access to the attribute is granted.
469
     */
470
    protected function canAccessAttribute(?object $object, string $attribute, array $context = []): bool
471
    {
472
        if (!$this->resourceClassResolver->isResourceClass($context['resource_class'])) {
387✔
473
            return true;
16✔
474
        }
475

476
        $options = $this->getFactoryOptions($context);
371✔
477
        $propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $options);
371✔
478
        $security = $propertyMetadata->getSecurity();
371✔
479
        if (null !== $this->resourceAccessChecker && $security) {
371✔
480
            return $this->resourceAccessChecker->isGranted($context['resource_class'], $security, [
×
481
                'object' => $object,
×
482
                'property' => $attribute,
×
483
            ]);
×
484
        }
485

486
        return true;
371✔
487
    }
488

489
    /**
490
     * Check if access to the attribute is granted.
491
     */
492
    protected function canAccessAttributePostDenormalize(?object $object, ?object $previousObject, string $attribute, array $context = []): bool
493
    {
494
        $options = $this->getFactoryOptions($context);
12✔
495
        $propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $options);
12✔
496
        $security = $propertyMetadata->getSecurityPostDenormalize();
12✔
497
        if ($this->resourceAccessChecker && $security) {
12✔
498
            return $this->resourceAccessChecker->isGranted($context['resource_class'], $security, [
×
499
                'object' => $object,
×
500
                'previous_object' => $previousObject,
×
501
                'property' => $attribute,
×
502
            ]);
×
503
        }
504

505
        return true;
12✔
506
    }
507

508
    /**
509
     * {@inheritdoc}
510
     */
511
    protected function setAttributeValue(object $object, string $attribute, mixed $value, ?string $format = null, array $context = []): void
512
    {
513
        try {
514
            $this->setValue($object, $attribute, $this->createAttributeValue($attribute, $value, $format, $context));
12✔
515
        } catch (NotNormalizableValueException $exception) {
×
516
            // Only throw if collecting denormalization errors is disabled.
517
            if (!isset($context['not_normalizable_value_exceptions'])) {
×
518
                throw $exception;
×
519
            }
520
        }
521
    }
522

523
    /**
524
     * Validates the type of the value. Allows using integers as floats for JSON formats.
525
     *
526
     * @throws NotNormalizableValueException
527
     */
528
    protected function validateType(string $attribute, Type $type, mixed $value, ?string $format = null, array $context = []): void
529
    {
530
        $builtinType = $type->getBuiltinType();
12✔
531
        if (Type::BUILTIN_TYPE_FLOAT === $builtinType && null !== $format && str_contains($format, 'json')) {
12✔
532
            $isValid = \is_float($value) || \is_int($value);
×
533
        } else {
534
            $isValid = \call_user_func('is_'.$builtinType, $value);
12✔
535
        }
536

537
        if (!$isValid) {
12✔
538
            throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type of the "%s" attribute must be "%s", "%s" given.', $attribute, $builtinType, \gettype($value)), $value, [$builtinType], $context['deserialization_path'] ?? null);
×
539
        }
540
    }
541

542
    /**
543
     * Denormalizes a collection of objects.
544
     *
545
     * @throws NotNormalizableValueException
546
     */
547
    protected function denormalizeCollection(string $attribute, ApiProperty $propertyMetadata, Type $type, string $className, mixed $value, ?string $format, array $context): array
548
    {
549
        if (!\is_array($value)) {
×
550
            throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type of the "%s" attribute must be "array", "%s" given.', $attribute, \gettype($value)), $value, [Type::BUILTIN_TYPE_ARRAY], $context['deserialization_path'] ?? null);
×
551
        }
552

553
        $values = [];
×
554
        $childContext = $this->createChildContext($this->createOperationContext($context, $className), $attribute, $format);
×
555
        $collectionKeyTypes = $type->getCollectionKeyTypes();
×
556
        foreach ($value as $index => $obj) {
×
557
            $currentChildContext = $childContext;
×
558
            if (isset($childContext['deserialization_path'])) {
×
559
                $currentChildContext['deserialization_path'] = "{$childContext['deserialization_path']}[{$index}]";
×
560
            }
561

562
            // no typehint provided on collection key
563
            if (!$collectionKeyTypes) {
×
564
                $values[$index] = $this->denormalizeRelation($attribute, $propertyMetadata, $className, $obj, $format, $currentChildContext);
×
565
                continue;
×
566
            }
567

568
            // validate collection key typehint
569
            foreach ($collectionKeyTypes as $collectionKeyType) {
×
570
                $collectionKeyBuiltinType = $collectionKeyType->getBuiltinType();
×
571
                if (!\call_user_func('is_'.$collectionKeyBuiltinType, $index)) {
×
572
                    continue;
×
573
                }
574

575
                $values[$index] = $this->denormalizeRelation($attribute, $propertyMetadata, $className, $obj, $format, $currentChildContext);
×
576
                continue 2;
×
577
            }
578
            throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type of the key "%s" must be "%s", "%s" given.', $index, $collectionKeyTypes[0]->getBuiltinType(), \gettype($index)), $index, [$collectionKeyTypes[0]->getBuiltinType()], ($context['deserialization_path'] ?? false) ? \sprintf('key(%s)', $context['deserialization_path']) : null, true);
×
579
        }
580

581
        return $values;
×
582
    }
583

584
    /**
585
     * Denormalizes a relation.
586
     *
587
     * @throws LogicException
588
     * @throws UnexpectedValueException
589
     * @throws NotNormalizableValueException
590
     */
591
    protected function denormalizeRelation(string $attributeName, ApiProperty $propertyMetadata, string $className, mixed $value, ?string $format, array $context): ?object
592
    {
593
        if (\is_string($value)) {
×
594
            try {
595
                return $this->iriConverter->getResourceFromIri($value, $context + ['fetch_data' => true]);
×
596
            } catch (ItemNotFoundException $e) {
×
597
                if (!isset($context['not_normalizable_value_exceptions'])) {
×
598
                    throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
×
599
                }
600
                $context['not_normalizable_value_exceptions'][] = NotNormalizableValueException::createForUnexpectedDataType(
×
601
                    $e->getMessage(),
×
602
                    $value,
×
603
                    [$className],
×
604
                    $context['deserialization_path'] ?? null,
×
605
                    true,
×
606
                    $e->getCode(),
×
607
                    $e
×
608
                );
×
609

610
                return null;
×
611
            } catch (InvalidArgumentException $e) {
×
612
                if (!isset($context['not_normalizable_value_exceptions'])) {
×
613
                    throw new UnexpectedValueException(\sprintf('Invalid IRI "%s".', $value), $e->getCode(), $e);
×
614
                }
615
                $context['not_normalizable_value_exceptions'][] = NotNormalizableValueException::createForUnexpectedDataType(
×
616
                    $e->getMessage(),
×
617
                    $value,
×
618
                    [$className],
×
619
                    $context['deserialization_path'] ?? null,
×
620
                    true,
×
621
                    $e->getCode(),
×
622
                    $e
×
623
                );
×
624

625
                return null;
×
626
            }
627
        }
628

629
        if ($propertyMetadata->isWritableLink()) {
×
630
            $context['api_allow_update'] = true;
×
631

632
            if (!$this->serializer instanceof DenormalizerInterface) {
×
633
                throw new LogicException(\sprintf('The injected serializer must be an instance of "%s".', DenormalizerInterface::class));
×
634
            }
635

636
            $item = $this->serializer->denormalize($value, $className, $format, $context);
×
637
            if (!\is_object($item) && null !== $item) {
×
638
                throw new \UnexpectedValueException('Expected item to be an object or null.');
×
639
            }
640

641
            return $item;
×
642
        }
643

644
        if (!\is_array($value)) {
×
645
            throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type of the "%s" attribute must be "array" (nested document) or "string" (IRI), "%s" given.', $attributeName, \gettype($value)), $value, [Type::BUILTIN_TYPE_ARRAY, Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, true);
×
646
        }
647

648
        throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Nested documents for attribute "%s" are not allowed. Use IRIs instead.', $attributeName), $value, [Type::BUILTIN_TYPE_ARRAY, Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, true);
×
649
    }
650

651
    /**
652
     * Gets the options for the property name collection / property metadata factories.
653
     */
654
    protected function getFactoryOptions(array $context): array
655
    {
656
        $options = [];
403✔
657
        if (isset($context[self::GROUPS])) {
403✔
658
            /* @see https://github.com/symfony/symfony/blob/v4.2.6/src/Symfony/Component/PropertyInfo/Extractor/SerializerExtractor.php */
659
            $options['serializer_groups'] = (array) $context[self::GROUPS];
220✔
660
        }
661

662
        $operationCacheKey = ($context['resource_class'] ?? '').($context['operation_name'] ?? '').($context['root_operation_name'] ?? '');
403✔
663
        $suffix = ($context['api_normalize'] ?? '') ? 'n' : '';
403✔
664
        if ($operationCacheKey && isset($this->localFactoryOptionsCache[$operationCacheKey.$suffix])) {
403✔
665
            return $options + $this->localFactoryOptionsCache[$operationCacheKey.$suffix];
387✔
666
        }
667

668
        // This is a hot spot
669
        if (isset($context['resource_class'])) {
403✔
670
            // Note that the groups need to be read on the root operation
671
            if ($operation = ($context['root_operation'] ?? null)) {
403✔
672
                $options['normalization_groups'] = $operation->getNormalizationContext()['groups'] ?? null;
108✔
673
                $options['denormalization_groups'] = $operation->getDenormalizationContext()['groups'] ?? null;
108✔
674
                $options['operation_name'] = $operation->getName();
108✔
675
            }
676
        }
677

678
        return $options + $this->localFactoryOptionsCache[$operationCacheKey.$suffix] = $options;
403✔
679
    }
680

681
    /**
682
     * {@inheritdoc}
683
     *
684
     * @throws UnexpectedValueException
685
     */
686
    protected function getAttributeValue(object $object, string $attribute, ?string $format = null, array $context = []): mixed
687
    {
688
        $context['api_attribute'] = $attribute;
375✔
689
        $context['property_metadata'] = $propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $this->getFactoryOptions($context));
375✔
690

691
        if ($context['api_denormalize'] ?? false) {
375✔
692
            return $this->propertyAccessor->getValue($object, $attribute);
×
693
        }
694

695
        $types = $propertyMetadata->getBuiltinTypes() ?? [];
375✔
696

697
        foreach ($types as $type) {
375✔
698
            if (
699
                $type->isCollection()
371✔
700
                && ($collectionValueType = $type->getCollectionValueTypes()[0] ?? null)
371✔
701
                && ($className = $collectionValueType->getClassName())
371✔
702
                && $this->resourceClassResolver->isResourceClass($className)
371✔
703
            ) {
704
                $childContext = $this->createChildContext($this->createOperationContext($context, $className), $attribute, $format);
24✔
705

706
                // @see ApiPlatform\Hal\Serializer\ItemNormalizer:getComponents logic for intentional duplicate content
707
                // @see ApiPlatform\JsonApi\Serializer\ItemNormalizer:getComponents logic for intentional duplicate content
708
                if ('jsonld' === $format && $itemUriTemplate = $propertyMetadata->getUriTemplate()) {
24✔
709
                    $operation = $this->resourceMetadataCollectionFactory->create($className)->getOperation(
×
710
                        operationName: $itemUriTemplate,
×
711
                        forceCollection: true,
×
712
                        httpOperation: true
×
713
                    );
×
714

715
                    return $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $operation, $childContext);
×
716
                }
717

718
                $attributeValue = $this->propertyAccessor->getValue($object, $attribute);
24✔
719

720
                if (!is_iterable($attributeValue)) {
24✔
721
                    throw new UnexpectedValueException('Unexpected non-iterable value for to-many relation.');
×
722
                }
723

724
                $resourceClass = $this->resourceClassResolver->getResourceClass($attributeValue, $className);
24✔
725

726
                $data = $this->normalizeCollectionOfRelations($propertyMetadata, $attributeValue, $resourceClass, $format, $childContext);
24✔
727
                $context['data'] = $data;
24✔
728
                $context['type'] = $type;
24✔
729

730
                if ($this->tagCollector) {
24✔
731
                    $this->tagCollector->collect($context);
24✔
732
                }
733

734
                return $data;
24✔
735
            }
736

737
            if (
738
                ($className = $type->getClassName())
371✔
739
                && $this->resourceClassResolver->isResourceClass($className)
371✔
740
            ) {
741
                $childContext = $this->createChildContext($this->createOperationContext($context, $className), $attribute, $format);
24✔
742
                unset($childContext['iri'], $childContext['uri_variables'], $childContext['item_uri_template']);
24✔
743

744
                if ('jsonld' === $format && $uriTemplate = $propertyMetadata->getUriTemplate()) {
24✔
745
                    $operation = $this->resourceMetadataCollectionFactory->create($className)->getOperation(
×
746
                        operationName: $uriTemplate,
×
747
                        httpOperation: true
×
748
                    );
×
749

750
                    return $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $operation, $childContext);
×
751
                }
752

753
                $attributeValue = $this->propertyAccessor->getValue($object, $attribute);
24✔
754

755
                if (!\is_object($attributeValue) && null !== $attributeValue) {
24✔
756
                    throw new UnexpectedValueException('Unexpected non-object value for to-one relation.');
×
757
                }
758

759
                $resourceClass = $this->resourceClassResolver->getResourceClass($attributeValue, $className);
24✔
760

761
                $data = $this->normalizeRelation($propertyMetadata, $attributeValue, $resourceClass, $format, $childContext);
24✔
762
                $context['data'] = $data;
24✔
763
                $context['type'] = $type;
24✔
764

765
                if ($this->tagCollector) {
24✔
766
                    $this->tagCollector->collect($context);
12✔
767
                }
768

769
                return $data;
24✔
770
            }
771

772
            if (!$this->serializer instanceof NormalizerInterface) {
371✔
773
                throw new LogicException(\sprintf('The injected serializer must be an instance of "%s".', NormalizerInterface::class));
×
774
            }
775

776
            unset(
371✔
777
                $context['resource_class'],
371✔
778
                $context['force_resource_class'],
371✔
779
                $context['uri_variables'],
371✔
780
            );
371✔
781

782
            // Anonymous resources
783
            if ($className) {
371✔
784
                $childContext = $this->createChildContext($this->createOperationContext($context, $className), $attribute, $format);
83✔
785
                $childContext['output']['gen_id'] = $propertyMetadata->getGenId() ?? true;
83✔
786

787
                $attributeValue = $this->propertyAccessor->getValue($object, $attribute);
83✔
788

789
                return $this->serializer->normalize($attributeValue, $format, $childContext);
83✔
790
            }
791

792
            if ('array' === $type->getBuiltinType()) {
363✔
793
                if ($className = ($type->getCollectionValueTypes()[0] ?? null)?->getClassName()) {
52✔
794
                    $context = $this->createOperationContext($context, $className);
8✔
795
                }
796

797
                $childContext = $this->createChildContext($context, $attribute, $format);
52✔
798
                $childContext['output']['gen_id'] = $propertyMetadata->getGenId() ?? true;
52✔
799

800
                $attributeValue = $this->propertyAccessor->getValue($object, $attribute);
52✔
801

802
                return $this->serializer->normalize($attributeValue, $format, $childContext);
52✔
803
            }
804
        }
805

806
        if (!$this->serializer instanceof NormalizerInterface) {
367✔
807
            throw new LogicException(\sprintf('The injected serializer must be an instance of "%s".', NormalizerInterface::class));
×
808
        }
809

810
        unset(
367✔
811
            $context['resource_class'],
367✔
812
            $context['force_resource_class'],
367✔
813
            $context['uri_variables']
367✔
814
        );
367✔
815

816
        $attributeValue = $this->propertyAccessor->getValue($object, $attribute);
367✔
817

818
        return $this->serializer->normalize($attributeValue, $format, $context);
367✔
819
    }
820

821
    /**
822
     * Normalizes a collection of relations (to-many).
823
     *
824
     * @throws UnexpectedValueException
825
     */
826
    protected function normalizeCollectionOfRelations(ApiProperty $propertyMetadata, iterable $attributeValue, string $resourceClass, ?string $format, array $context): array
827
    {
828
        $value = [];
24✔
829
        foreach ($attributeValue as $index => $obj) {
24✔
830
            if (!\is_object($obj) && null !== $obj) {
×
831
                throw new UnexpectedValueException('Unexpected non-object element in to-many relation.');
×
832
            }
833

834
            // update context, if concrete object class deviates from general relation class (e.g. in case of polymorphic resources)
835
            $objResourceClass = $this->resourceClassResolver->getResourceClass($obj, $resourceClass);
×
836
            $context['resource_class'] = $objResourceClass;
×
837
            if ($this->resourceMetadataCollectionFactory) {
×
838
                $context['operation'] = $this->resourceMetadataCollectionFactory->create($objResourceClass)->getOperation();
×
839
            }
840

841
            $value[$index] = $this->normalizeRelation($propertyMetadata, $obj, $resourceClass, $format, $context);
×
842
        }
843

844
        return $value;
24✔
845
    }
846

847
    /**
848
     * Normalizes a relation.
849
     *
850
     * @throws LogicException
851
     * @throws UnexpectedValueException
852
     */
853
    protected function normalizeRelation(ApiProperty $propertyMetadata, ?object $relatedObject, string $resourceClass, ?string $format, array $context): \ArrayObject|array|string|null
854
    {
855
        if (null === $relatedObject || !empty($context['attributes']) || $propertyMetadata->isReadableLink()) {
24✔
856
            if (!$this->serializer instanceof NormalizerInterface) {
16✔
857
                throw new LogicException(\sprintf('The injected serializer must be an instance of "%s".', NormalizerInterface::class));
×
858
            }
859

860
            $relatedContext = $this->createOperationContext($context, $resourceClass);
16✔
861
            $normalizedRelatedObject = $this->serializer->normalize($relatedObject, $format, $relatedContext);
16✔
862
            if (!\is_string($normalizedRelatedObject) && !\is_array($normalizedRelatedObject) && !$normalizedRelatedObject instanceof \ArrayObject && null !== $normalizedRelatedObject) {
16✔
863
                throw new UnexpectedValueException('Expected normalized relation to be an IRI, array, \ArrayObject or null');
×
864
            }
865

866
            return $normalizedRelatedObject;
16✔
867
        }
868

869
        $context['iri'] = $iri = $this->iriConverter->getIriFromResource(resource: $relatedObject, context: $context);
8✔
870
        $context['data'] = $iri;
8✔
871
        $context['object'] = $relatedObject;
8✔
872
        unset($context['property_metadata'], $context['api_attribute']);
8✔
873

874
        if ($this->tagCollector) {
8✔
875
            $this->tagCollector->collect($context);
×
876
        } elseif (isset($context['resources'])) {
8✔
877
            $context['resources'][$iri] = $iri;
×
878
        }
879

880
        $push = $propertyMetadata->getPush() ?? false;
8✔
881
        if (isset($context['resources_to_push']) && $push) {
8✔
882
            $context['resources_to_push'][$iri] = $iri;
×
883
        }
884

885
        return $iri;
8✔
886
    }
887

888
    private function createAttributeValue(string $attribute, mixed $value, ?string $format = null, array &$context = []): mixed
889
    {
890
        try {
891
            return $this->createAndValidateAttributeValue($attribute, $value, $format, $context);
12✔
892
        } catch (NotNormalizableValueException $exception) {
×
893
            if (!isset($context['not_normalizable_value_exceptions'])) {
×
894
                throw $exception;
×
895
            }
896
            $context['not_normalizable_value_exceptions'][] = $exception;
×
897

898
            throw $exception;
×
899
        }
900
    }
901

902
    private function createAndValidateAttributeValue(string $attribute, mixed $value, ?string $format = null, array $context = []): mixed
903
    {
904
        $propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $this->getFactoryOptions($context));
12✔
905
        $types = $propertyMetadata->getBuiltinTypes() ?? [];
12✔
906
        $isMultipleTypes = \count($types) > 1;
12✔
907

908
        foreach ($types as $type) {
12✔
909
            if (null === $value && ($type->isNullable() || ($context[static::DISABLE_TYPE_ENFORCEMENT] ?? false))) {
12✔
910
                return $value;
×
911
            }
912

913
            $collectionValueType = $type->getCollectionValueTypes()[0] ?? null;
12✔
914

915
            /* From @see AbstractObjectNormalizer::validateAndDenormalize() */
916
            // Fix a collection that contains the only one element
917
            // This is special to xml format only
918
            if ('xml' === $format && null !== $collectionValueType && (!\is_array($value) || !\is_int(key($value)))) {
12✔
919
                $value = [$value];
×
920
            }
921

922
            if (
923
                $type->isCollection()
12✔
924
                && null !== $collectionValueType
12✔
925
                && null !== ($className = $collectionValueType->getClassName())
12✔
926
                && $this->resourceClassResolver->isResourceClass($className)
12✔
927
            ) {
928
                $resourceClass = $this->resourceClassResolver->getResourceClass(null, $className);
×
929
                $context['resource_class'] = $resourceClass;
×
930
                unset($context['uri_variables']);
×
931

932
                return $this->denormalizeCollection($attribute, $propertyMetadata, $type, $resourceClass, $value, $format, $context);
×
933
            }
934

935
            if (
936
                null !== ($className = $type->getClassName())
12✔
937
                && $this->resourceClassResolver->isResourceClass($className)
12✔
938
            ) {
939
                $resourceClass = $this->resourceClassResolver->getResourceClass(null, $className);
×
940
                $childContext = $this->createChildContext($this->createOperationContext($context, $resourceClass), $attribute, $format);
×
941

942
                return $this->denormalizeRelation($attribute, $propertyMetadata, $resourceClass, $value, $format, $childContext);
×
943
            }
944

945
            if (
946
                $type->isCollection()
12✔
947
                && null !== $collectionValueType
12✔
948
                && null !== ($className = $collectionValueType->getClassName())
12✔
949
                && \is_array($value)
12✔
950
            ) {
951
                if (!$this->serializer instanceof DenormalizerInterface) {
×
952
                    throw new LogicException(\sprintf('The injected serializer must be an instance of "%s".', DenormalizerInterface::class));
×
953
                }
954

955
                unset($context['resource_class'], $context['uri_variables']);
×
956

957
                return $this->serializer->denormalize($value, $className.'[]', $format, $context);
×
958
            }
959

960
            if (null !== $className = $type->getClassName()) {
12✔
961
                if (!$this->serializer instanceof DenormalizerInterface) {
×
962
                    throw new LogicException(\sprintf('The injected serializer must be an instance of "%s".', DenormalizerInterface::class));
×
963
                }
964

965
                unset($context['resource_class'], $context['uri_variables']);
×
966

967
                return $this->serializer->denormalize($value, $className, $format, $context);
×
968
            }
969

970
            /* From @see AbstractObjectNormalizer::validateAndDenormalize() */
971
            // In XML and CSV all basic datatypes are represented as strings, it is e.g. not possible to determine,
972
            // if a value is meant to be a string, float, int or a boolean value from the serialized representation.
973
            // That's why we have to transform the values, if one of these non-string basic datatypes is expected.
974
            if (\is_string($value) && (XmlEncoder::FORMAT === $format || CsvEncoder::FORMAT === $format)) {
12✔
975
                if ('' === $value && $type->isNullable() && \in_array($type->getBuiltinType(), [Type::BUILTIN_TYPE_BOOL, Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_FLOAT], true)) {
×
976
                    return null;
×
977
                }
978

979
                switch ($type->getBuiltinType()) {
×
980
                    case Type::BUILTIN_TYPE_BOOL:
981
                        // according to http://www.w3.org/TR/xmlschema-2/#boolean, valid representations are "false", "true", "0" and "1"
982
                        if ('false' === $value || '0' === $value) {
×
983
                            $value = false;
×
984
                        } elseif ('true' === $value || '1' === $value) {
×
985
                            $value = true;
×
986
                        } else {
987
                            // union/intersect types: try the next type, if not valid, an exception will be thrown at the end
988
                            if ($isMultipleTypes) {
×
989
                                break 2;
×
990
                            }
991
                            throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type of the "%s" attribute for class "%s" must be bool ("%s" given).', $attribute, $className, $value), $value, [Type::BUILTIN_TYPE_BOOL], $context['deserialization_path'] ?? null);
×
992
                        }
993
                        break;
×
994
                    case Type::BUILTIN_TYPE_INT:
995
                        if (ctype_digit($value) || ('-' === $value[0] && ctype_digit(substr($value, 1)))) {
×
996
                            $value = (int) $value;
×
997
                        } else {
998
                            // union/intersect types: try the next type, if not valid, an exception will be thrown at the end
999
                            if ($isMultipleTypes) {
×
1000
                                break 2;
×
1001
                            }
1002
                            throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type of the "%s" attribute for class "%s" must be int ("%s" given).', $attribute, $className, $value), $value, [Type::BUILTIN_TYPE_INT], $context['deserialization_path'] ?? null);
×
1003
                        }
1004
                        break;
×
1005
                    case Type::BUILTIN_TYPE_FLOAT:
1006
                        if (is_numeric($value)) {
×
1007
                            return (float) $value;
×
1008
                        }
1009

1010
                        switch ($value) {
1011
                            case 'NaN':
×
1012
                                return \NAN;
×
1013
                            case 'INF':
×
1014
                                return \INF;
×
1015
                            case '-INF':
×
1016
                                return -\INF;
×
1017
                            default:
1018
                                // union/intersect types: try the next type, if not valid, an exception will be thrown at the end
1019
                                if ($isMultipleTypes) {
×
1020
                                    break 3;
×
1021
                                }
1022
                                throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type of the "%s" attribute for class "%s" must be float ("%s" given).', $attribute, $className, $value), $value, [Type::BUILTIN_TYPE_FLOAT], $context['deserialization_path'] ?? null);
×
1023
                        }
1024
                }
1025
            }
1026

1027
            if ($context[static::DISABLE_TYPE_ENFORCEMENT] ?? false) {
12✔
1028
                return $value;
×
1029
            }
1030

1031
            try {
1032
                $this->validateType($attribute, $type, $value, $format, $context);
12✔
1033

1034
                break;
12✔
1035
            } catch (NotNormalizableValueException $e) {
×
1036
                // union/intersect types: try the next type
1037
                if (!$isMultipleTypes) {
×
1038
                    throw $e;
×
1039
                }
1040
            }
1041
        }
1042

1043
        return $value;
12✔
1044
    }
1045

1046
    /**
1047
     * Sets a value of the object using the PropertyAccess component.
1048
     */
1049
    private function setValue(object $object, string $attributeName, mixed $value): void
1050
    {
1051
        try {
1052
            $this->propertyAccessor->setValue($object, $attributeName, $value);
12✔
1053
        } catch (NoSuchPropertyException) {
×
1054
            // Properties not found are ignored
1055
        }
1056
    }
1057
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc