Passed
Push — master ( 0c064e...87b116 )
by Arthur
08:29 queued 10s
created

PropertyData::resolveConstraints()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Larapie\DataTransferObject\Property;
4
5
use ReflectionProperty;
6
use Doctrine\Common\Annotations\Reader;
7
use Symfony\Component\Validator\Constraint;
8
use Doctrine\Common\Annotations\AnnotationReader;
9
use Doctrine\Common\Annotations\AnnotationRegistry;
10
use Larapie\DataTransferObject\Annotations\Optional;
11
use Larapie\DataTransferObject\Annotations\Immutable;
12
use Larapie\DataTransferObject\Resolvers\AnnotationResolver;
13
use Larapie\DataTransferObject\Resolvers\PropertyTypeResolver;
14
15
class PropertyData
16
{
17
    /** @var string */
18
    protected $name;
19
20
    /** @var PropertyType */
21
    protected $type;
22
23
    /** @var string */
24
    protected $fqn;
25
26
    /** @var bool */
27
    protected $optional;
28
29
    /** @var bool */
30
    protected $immutable;
31
32
    /** @var array */
33
    protected $constraints;
34
35
    /** @var Reader */
36
    private static $reader;
37
38
    /**
39
     * PropertyData constructor.
40
     * @param ReflectionProperty $property
41
     */
42
    public function __construct(ReflectionProperty $property)
43
    {
44
        $this->name = $property->getName();
45
        $this->fqn = "{$property->getDeclaringClass()->getName()}::{$property->getName()}";
46
        $this->boot($property);
47
    }
48
49
    protected function boot(reflectionProperty $reflectionProperty)
50
    {
51
        $annotations = $this->resolveAnnotations($reflectionProperty);
52
        $this->type = $this->resolveType($reflectionProperty);
53
        $this->immutable = $this->resolveImmutable($annotations);
54
        $this->constraints = $this->resolveConstraints($annotations);
55
        $this->optional = $this->resolveOptional($annotations);
56
    }
57
58
    protected function resolveType(ReflectionProperty $reflection)
59
    {
60
        return (new PropertyTypeResolver($reflection))->resolve();
61
    }
62
63
    protected function resolveAnnotations(ReflectionProperty $reflection)
64
    {
65
        $annotations = [];
66
        foreach (self::getReader()->getPropertyAnnotations($reflection) as $annotation) {
67
            $annotations[] = $annotation;
68
        }
69
70
        return (new AnnotationResolver($reflection))->resolve();
71
    }
72
73
    protected function resolveOptional($annotations)
74
    {
75
        foreach ($annotations as $annotation) {
76
            if ($annotation instanceof Optional) {
77
                return true;
78
            }
79
        }
80
81
        return false;
82
    }
83
84
    protected function resolveImmutable($annotations)
85
    {
86
        foreach ($annotations as $annotation) {
87
            if ($annotation instanceof Immutable) {
88
                return true;
89
            }
90
        }
91
92
        return false;
93
    }
94
95
    protected function resolveConstraints($annotations)
96
    {
97
        $constraints = [];
98
        foreach ($annotations as $annotation) {
99
            if ($annotation instanceof Constraint) {
100
                $constraints[] = $annotation;
101
            }
102
        }
103
104
        return $constraints;
105
    }
106
107
    protected static function getReader()
108
    {
109
        AnnotationRegistry::registerUniqueLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...:registerUniqueLoader() has been deprecated: this method is deprecated and will be removed in doctrine/annotations 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

109
        /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerUniqueLoader('class_exists');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
110
        if (! isset(self::$reader)) {
111
            self::$reader = new AnnotationReader();
112
        }
113
114
        return self::$reader;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getName(): string
121
    {
122
        return $this->name;
123
    }
124
125
    /**
126
     * @return PropertyType
127
     */
128
    public function getType(): PropertyType
129
    {
130
        return $this->type;
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    public function getFqn(): string
137
    {
138
        return $this->fqn;
139
    }
140
141
    /**
142
     * @return bool
143
     */
144
    public function isOptional(): bool
145
    {
146
        return $this->optional;
147
    }
148
149
    /**
150
     * @return bool
151
     */
152
    public function isImmutable(): bool
153
    {
154
        return $this->immutable;
155
    }
156
157
    /**
158
     * @return array
159
     */
160
    public function getConstraints(): array
161
    {
162
        return $this->constraints;
163
    }
164
}
165