RecursiveObjectNormalizer::setAttributeValue()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 4
nc 6
nop 5
1
<?php
2
3
namespace Guzzle\ConfigOperationsBundle\Normalizer;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Guzzle\ConfigOperationsBundle\Normalizer\Annotation\Type;
7
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
8
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
9
10
/**
11
 * @author Pierre Rolland <[email protected]>
12
 */
13
class RecursiveObjectNormalizer extends ObjectNormalizer
14
{
15
    /**
16
     * @var AnnotationReader
17
     */
18
    private $annotationReader;
19
20
    /**
21
     * @var \ReflectionClass[]
22
     */
23
    private $reflectionClasses;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function instantiateObject(
29
        array &$data,
30
        $class,
31
        array &$context,
32
        \ReflectionClass $reflectionClass,
33
        $allowedAttributes,
34
        string $format = null
35
    ) {
36
        $this->reflectionClasses[$class] = $reflectionClass;
37
        return parent::instantiateObject($data, $class, $context, $reflectionClass, $allowedAttributes, $format);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = [])
44
    {
45
        try {
46
            if ($this->hasProperty($object, $attribute) && ($type = $this->getTypeAnnotation($object, $attribute))) {
47
                $value = $this->getDenormalizedValue($type, $value, $format, $context);
48
            }
49
            $this->propertyAccessor->setValue($object, $attribute, $value);
50
        } catch (NoSuchPropertyException $exception) {
51
            // Properties not found are ignored
52
        }
53
    }
54
55
    protected function getDenormalizedValue(Type $type, $value, ?string $format, array $context)
56
    {
57
        if (substr($type->name, -2) === '[]' && is_array($value)) {
58
            $denormalized = [];
59
            $class = substr($type->name, 0, -2);
60
            foreach ($value as $item) {
61
                $denormalized[] = $this->denormalize($item, $class, $format, $context);
62
            }
63
64
            return $denormalized;
65
        }
66
67
        return $this->denormalize($value, $type->name, $format, $context);
68
    }
69
70
    protected function getTypeAnnotation($object, string $attribute): ?Type
71
    {
72
        return $this->getAnnotationReader()->getPropertyAnnotation(
73
            $this->reflectionClasses[get_class($object)]->getProperty($attribute),
74
            Type::class
75
        );
76
    }
77
78
    protected function hasProperty($object, string $attribute): bool
79
    {
80
        return $this->reflectionClasses[get_class($object)]->hasProperty($attribute);
81
    }
82
83
    public function getAnnotationReader(): AnnotationReader
84
    {
85
        if (null === $this->annotationReader) {
86
            $this->annotationReader = new AnnotationReader();
87
        }
88
89
        return $this->annotationReader;
90
    }
91
92
    public function setAnnotationReader(AnnotationReader $annotationReader): void
93
    {
94
        $this->annotationReader = $annotationReader;
95
    }
96
}
97