Completed
Pull Request — master (#11)
by Eric
04:56
created

AnnotationClassMetadataLoader::loadMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
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\XmlRoot;
32
use Ivory\Serializer\Mapping\Annotation\XmlValue;
33
use Ivory\Serializer\Type\Parser\TypeParserInterface;
34
35
/**
36
 * @author GeLo <[email protected]>
37
 */
38
class AnnotationClassMetadataLoader extends AbstractReflectionClassMetadataLoader
39
{
40
    /**
41
     * @var Reader
42
     */
43
    private $reader;
44
45
    /**
46
     * @param Reader|null              $reader
47
     * @param TypeParserInterface|null $typeParser
48
     */
49 1460
    public function __construct(Reader $reader = null, TypeParserInterface $typeParser = null)
50
    {
51 1460
        parent::__construct($typeParser);
52
53 1460
        $this->reader = $reader ?: new AnnotationReader();
54 1460
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 1168
    protected function loadClass(\ReflectionClass $class)
60
    {
61 1168
        $definition = parent::loadClass($class);
62
63 1168
        foreach ($this->reader->getClassAnnotations($class) as $annotation) {
64 324
            if ($annotation instanceof ExclusionPolicy) {
65 72
                $definition['exclusion_policy'] = $annotation->getPolicy();
66 164
            } elseif ($annotation instanceof Order) {
67 108
                $definition['order'] = $annotation->getOrder();
68 126
            } elseif ($annotation instanceof Readable) {
69 36
                $definition['readable'] = $annotation->isReadable();
70 72
            } elseif ($annotation instanceof Writable) {
71 36
                $definition['writable'] = $annotation->isWritable();
72 54
            } elseif ($annotation instanceof XmlRoot) {
73 198
                $definition['xml_root'] = $annotation->getName();
74 36
            }
75 584
        }
76
77 1168
        return $definition;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 1164
    protected function loadProperty(\ReflectionProperty $property)
84
    {
85 1164
        return $this->loadAnnotations($this->reader->getPropertyAnnotations($property));
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 456
    protected function loadMethod(\ReflectionMethod $method)
92
    {
93 456
        $result = $this->loadAnnotations($this->reader->getMethodAnnotations($method));
94
95 456
        if (!empty($result)) {
96 68
            return $result;
97
        }
98 456
    }
99
100
    /**
101
     * @param object[] $annotations
102
     *
103
     * @return mixed[]
104
     */
105 1164
    private function loadAnnotations(array $annotations)
106
    {
107 1164
        $definition = [];
108
109 1164
        foreach ($annotations as $annotation) {
110 912
            if ($annotation instanceof Alias) {
111 132
                $definition['alias'] = $annotation->getAlias();
112 456
            } elseif ($annotation instanceof Type) {
113 336
                $definition['type'] = $annotation->getType();
114 457
            } elseif ($annotation instanceof Expose) {
115 36
                $definition['expose'] = true;
116 322
            } elseif ($annotation instanceof Exclude) {
117 36
                $definition['exclude'] = true;
118 304
            } elseif ($annotation instanceof Readable) {
119 72
                $definition['readable'] = $annotation->isReadable();
120 286
            } elseif ($annotation instanceof Writable) {
121 72
                $definition['writable'] = $annotation->isWritable();
122 250
            } elseif ($annotation instanceof Accessor) {
123 36
                $definition['accessor'] = $annotation->getAccessor();
124 214
            } elseif ($annotation instanceof Mutator) {
125 36
                $definition['mutator'] = $annotation->getMutator();
126 198
            } elseif ($annotation instanceof Since) {
127 100
                $definition['since'] = $annotation->getVersion();
128 180
            } elseif ($annotation instanceof Until) {
129 100
                $definition['until'] = $annotation->getVersion();
130 178
            } elseif ($annotation instanceof MaxDepth) {
131 68
                $definition['max_depth'] = $annotation->getMaxDepth();
132 128
            } elseif ($annotation instanceof Groups) {
133 116
                $definition['groups'] = $annotation->getGroups();
134 94
            } elseif ($annotation instanceof XmlAttribute) {
135 72
                $definition['xml_attribute'] = true;
136 36
            } elseif ($annotation instanceof XmlValue) {
137 474
                $definition['xml_value'] = true;
138 18
            }
139 582
        }
140
141 1164
        return $definition;
142
    }
143
}
144