Completed
Push — master ( 454f7d...fc2d7f )
by Eric
02:39
created

AnnotationClassMetadataLoader::loadAnnotations()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.6037
c 0
b 0
f 0
cc 8
eloc 16
nc 8
nop 1
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\Groups;
18
use Ivory\Serializer\Mapping\Annotation\MaxDepth;
19
use Ivory\Serializer\Mapping\Annotation\Since;
20
use Ivory\Serializer\Mapping\Annotation\Type;
21
use Ivory\Serializer\Mapping\Annotation\Until;
22
use Ivory\Serializer\Type\Parser\TypeParserInterface;
23
24
/**
25
 * @author GeLo <[email protected]>
26
 */
27
class AnnotationClassMetadataLoader extends AbstractReflectionClassMetadataLoader
28
{
29
    /**
30
     * @var Reader
31
     */
32
    private $reader;
33
34
    /**
35
     * @param Reader|null              $reader
36
     * @param TypeParserInterface|null $typeParser
37
     */
38
    public function __construct(Reader $reader = null, TypeParserInterface $typeParser = null)
39
    {
40
        parent::__construct($typeParser);
41
42
        $this->reader = $reader ?: new AnnotationReader();
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function loadProperty(\ReflectionProperty $property)
49
    {
50
        return $this->loadAnnotations($this->reader->getPropertyAnnotations($property));
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function loadMethod(\ReflectionMethod $method)
57
    {
58
        $result = $this->loadAnnotations($this->reader->getMethodAnnotations($method));
59
60
        if (!empty($result)) {
61
            return $result;
62
        }
63
    }
64
65
    /**
66
     * @param object[] $annotations
67
     *
68
     * @return mixed[]
69
     */
70
    private function loadAnnotations(array $annotations)
71
    {
72
        $definition = [];
73
74
        foreach ($annotations as $annotation) {
75
            if ($annotation instanceof Alias) {
76
                $definition['alias'] = $annotation->getAlias();
77
            } elseif ($annotation instanceof Type) {
78
                $definition['type'] = $annotation->getType();
79
            } elseif ($annotation instanceof Since) {
80
                $definition['since'] = $annotation->getVersion();
81
            } elseif ($annotation instanceof Until) {
82
                $definition['until'] = $annotation->getVersion();
83
            } elseif ($annotation instanceof MaxDepth) {
84
                $definition['max_depth'] = $annotation->getMaxDepth();
85
            } elseif ($annotation instanceof Groups) {
86
                $definition['groups'] = $annotation->getGroups();
87
            }
88
        }
89
90
        return $definition;
91
    }
92
}
93