Completed
Push — master ( fc2d7f...dda5b7 )
by Eric
03:12
created

AnnotationClassMetadataLoader   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 10
dl 0
loc 86
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A loadClass() 0 12 3
A loadProperty() 0 4 1
A loadMethod() 0 8 2
D loadAnnotations() 0 26 10
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\Alias;
17
use Ivory\Serializer\Mapping\Annotation\Exclude;
18
use Ivory\Serializer\Mapping\Annotation\ExclusionPolicy;
19
use Ivory\Serializer\Mapping\Annotation\Expose;
20
use Ivory\Serializer\Mapping\Annotation\Groups;
21
use Ivory\Serializer\Mapping\Annotation\MaxDepth;
22
use Ivory\Serializer\Mapping\Annotation\Since;
23
use Ivory\Serializer\Mapping\Annotation\Type;
24
use Ivory\Serializer\Mapping\Annotation\Until;
25
use Ivory\Serializer\Type\Parser\TypeParserInterface;
26
27
/**
28
 * @author GeLo <[email protected]>
29
 */
30
class AnnotationClassMetadataLoader extends AbstractReflectionClassMetadataLoader
31
{
32
    /**
33
     * @var Reader
34
     */
35
    private $reader;
36
37
    /**
38
     * @param Reader|null              $reader
39
     * @param TypeParserInterface|null $typeParser
40
     */
41
    public function __construct(Reader $reader = null, TypeParserInterface $typeParser = null)
42
    {
43
        parent::__construct($typeParser);
44
45
        $this->reader = $reader ?: new AnnotationReader();
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected function loadClass(\ReflectionClass $class)
52
    {
53
        $definition = parent::loadClass($class);
54
55
        foreach ($this->reader->getClassAnnotations($class) as $annotation) {
56
            if ($annotation instanceof ExclusionPolicy) {
57
                $definition['exclusion_policy'] = $annotation->getPolicy();
58
            }
59
        }
60
61
        return $definition;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected function loadProperty(\ReflectionProperty $property)
68
    {
69
        return $this->loadAnnotations($this->reader->getPropertyAnnotations($property));
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected function loadMethod(\ReflectionMethod $method)
76
    {
77
        $result = $this->loadAnnotations($this->reader->getMethodAnnotations($method));
78
79
        if (!empty($result)) {
80
            return $result;
81
        }
82
    }
83
84
    /**
85
     * @param object[] $annotations
86
     *
87
     * @return mixed[]
88
     */
89
    private function loadAnnotations(array $annotations)
90
    {
91
        $definition = [];
92
93
        foreach ($annotations as $annotation) {
94
            if ($annotation instanceof Alias) {
95
                $definition['alias'] = $annotation->getAlias();
96
            } elseif ($annotation instanceof Type) {
97
                $definition['type'] = $annotation->getType();
98
            } elseif ($annotation instanceof Expose) {
99
                $definition['expose'] = true;
100
            } elseif ($annotation instanceof Exclude) {
101
                $definition['exclude'] = true;
102
            } elseif ($annotation instanceof Since) {
103
                $definition['since'] = $annotation->getVersion();
104
            } elseif ($annotation instanceof Until) {
105
                $definition['until'] = $annotation->getVersion();
106
            } elseif ($annotation instanceof MaxDepth) {
107
                $definition['max_depth'] = $annotation->getMaxDepth();
108
            } elseif ($annotation instanceof Groups) {
109
                $definition['groups'] = $annotation->getGroups();
110
            }
111
        }
112
113
        return $definition;
114
    }
115
}
116