Completed
Pull Request — master (#11)
by Eric
65:27
created

AnnotationClassMetadataLoader   B

Complexity

Total Complexity 34

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 34
lcom 1
cbo 17
dl 0
loc 126
rs 7.2285
c 0
b 0
f 0
ccs 55
cts 55
cp 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
B loadClass() 0 20 7
A loadProperty() 0 4 1
A loadMethod() 0 8 2
C loadAnnotations() 0 58 22
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Mapping\Loader;
13
14
use Doctrine\Common\Annotations\AnnotationReader;
15
use Doctrine\Common\Annotations\Reader;
16
use Ivory\Serializer\Mapping\Annotation\Accessor;
17
use Ivory\Serializer\Mapping\Annotation\Alias;
18
use Ivory\Serializer\Mapping\Annotation\Exclude;
19
use Ivory\Serializer\Mapping\Annotation\ExclusionPolicy;
20
use Ivory\Serializer\Mapping\Annotation\Expose;
21
use Ivory\Serializer\Mapping\Annotation\Groups;
22
use Ivory\Serializer\Mapping\Annotation\MaxDepth;
23
use Ivory\Serializer\Mapping\Annotation\Mutator;
24
use Ivory\Serializer\Mapping\Annotation\Order;
25
use Ivory\Serializer\Mapping\Annotation\Readable;
26
use Ivory\Serializer\Mapping\Annotation\Since;
27
use Ivory\Serializer\Mapping\Annotation\Type;
28
use Ivory\Serializer\Mapping\Annotation\Until;
29
use Ivory\Serializer\Mapping\Annotation\Writable;
30
use Ivory\Serializer\Mapping\Annotation\XmlAttribute;
31
use Ivory\Serializer\Mapping\Annotation\XmlCollection;
32
use Ivory\Serializer\Mapping\Annotation\XmlRoot;
33
use Ivory\Serializer\Mapping\Annotation\XmlValue;
34
use Ivory\Serializer\Type\Parser\TypeParserInterface;
35
36
/**
37
 * @author GeLo <[email protected]>
38
 */
39
class AnnotationClassMetadataLoader extends AbstractReflectionClassMetadataLoader
40
{
41
    /**
42
     * @var Reader
43
     */
44
    private $reader;
45
46 1388
    /**
47
     * @param Reader|null              $reader
48 1388
     * @param TypeParserInterface|null $typeParser
49
     */
50 1388
    public function __construct(Reader $reader = null, TypeParserInterface $typeParser = null)
51 1388
    {
52
        parent::__construct($typeParser);
53
54
        $this->reader = $reader ?: new AnnotationReader();
55
    }
56 1096
57
    /**
58 1096
     * {@inheritdoc}
59
     */
60 1096
    protected function loadClass(\ReflectionClass $class)
61 252
    {
62 72
        $definition = parent::loadClass($class);
63 128
64 108
        foreach ($this->reader->getClassAnnotations($class) as $annotation) {
65 90
            if ($annotation instanceof ExclusionPolicy) {
66 36
                $definition['exclusion_policy'] = $annotation->getPolicy();
67 36
            } elseif ($annotation instanceof Order) {
68 144
                $definition['order'] = $annotation->getOrder();
69 18
            } elseif ($annotation instanceof Readable) {
70 548
                $definition['readable'] = $annotation->isReadable();
71
            } elseif ($annotation instanceof Writable) {
72 1096
                $definition['writable'] = $annotation->isWritable();
73
            } elseif ($annotation instanceof XmlRoot) {
74
                $definition['xml_root'] = $annotation->getName();
75
            }
76
        }
77
78 1092
        return $definition;
79
    }
80 1092
81
    /**
82
     * {@inheritdoc}
83
     */
84
    protected function loadProperty(\ReflectionProperty $property)
85
    {
86 456
        return $this->loadAnnotations($this->reader->getPropertyAnnotations($property));
87
    }
88 456
89
    /**
90 456
     * {@inheritdoc}
91 68
     */
92
    protected function loadMethod(\ReflectionMethod $method)
93 456
    {
94
        $result = $this->loadAnnotations($this->reader->getMethodAnnotations($method));
95
96
        if (!empty($result)) {
97
            return $result;
98
        }
99
    }
100 1092
101
    /**
102 1092
     * @param object[] $annotations
103
     *
104 1092
     * @return mixed[]
105 840
     */
106 132
    private function loadAnnotations(array $annotations)
107 420
    {
108 336
        $definition = [];
109 421
110 36
        foreach ($annotations as $annotation) {
111 286
            if ($annotation instanceof Alias) {
112 36
                $definition['alias'] = $annotation->getAlias();
113 268
            } elseif ($annotation instanceof Type) {
114 72
                $definition['type'] = $annotation->getType();
115 250
            } elseif ($annotation instanceof Expose) {
116 72
                $definition['expose'] = true;
117 214
            } elseif ($annotation instanceof Exclude) {
118 36
                $definition['exclude'] = true;
119 178
            } elseif ($annotation instanceof Readable) {
120 36
                $definition['readable'] = $annotation->isReadable();
121 162
            } elseif ($annotation instanceof Writable) {
122 100
                $definition['writable'] = $annotation->isWritable();
123 144
            } elseif ($annotation instanceof Accessor) {
124 100
                $definition['accessor'] = $annotation->getAccessor();
125 142
            } elseif ($annotation instanceof Mutator) {
126 68
                $definition['mutator'] = $annotation->getMutator();
127 92
            } elseif ($annotation instanceof Since) {
128 478
                $definition['since'] = $annotation->getVersion();
129 58
            } elseif ($annotation instanceof Until) {
130 546
                $definition['until'] = $annotation->getVersion();
131
            } elseif ($annotation instanceof MaxDepth) {
132 1092
                $definition['max_depth'] = $annotation->getMaxDepth();
133
            } elseif ($annotation instanceof Groups) {
134
                $definition['groups'] = $annotation->getGroups();
135
            } elseif ($annotation instanceof XmlAttribute) {
136
                $definition['xml_attribute'] = true;
137
            } elseif ($annotation instanceof XmlValue) {
138
                $definition['xml_value'] = true;
139
            } elseif ($annotation instanceof XmlCollection) {
140
                if ($annotation->hasEntry()) {
141
                    $definition['xml_entry'] = $annotation->getEntry();
142
                }
143
144
                if ($annotation->hasEntryAttribute()) {
145
                    $definition['xml_entry_attribute'] = $annotation->getEntryAttribute();
146
                }
147
148
                if ($annotation->hasKeyAsAttribute()) {
149
                    $definition['xml_key_as_attribute'] = $annotation->useKeyAsAttribute();
150
                }
151
152
                if ($annotation->hasKeyAsNode()) {
153
                    $definition['xml_key_as_node'] = $annotation->useKeyAsNode();
154
                }
155
156
                if ($annotation->hasInline()) {
157
                    $definition['xml_inline'] = $annotation->isInline();
158
                }
159
            }
160
        }
161
162
        return $definition;
163
    }
164
}
165