Completed
Push — master ( 845de9...0081d5 )
by Arthur
09:56 queued 10s
created

PropertyData::getReader()   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
eloc 4
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Larapie\DataTransferObject\Property;
4
5
use Larapie\DataTransferObject\Annotations\Immutable;
6
use Larapie\DataTransferObject\Annotations\Optional;
7
use Larapie\DataTransferObject\Resolvers\AnnotationResolver;
8
use Larapie\DataTransferObject\Resolvers\ConstraintsResolver;
9
use Larapie\DataTransferObject\Resolvers\PropertyTypeResolver;
10
use ReflectionProperty;
11
12
class PropertyData
13
{
14
    /** @var string */
15
    protected $name;
16
17
    /** @var PropertyType */
18
    protected $type;
19
20
    /** @var string */
21
    protected $fqn;
22
23
    /** @var bool */
24
    protected $optional;
25
26
    /** @var bool */
27
    protected $immutable;
28
29
    /** @var array */
30
    protected $constraints;
31
32
    /** @var array */
33
    protected $annotations;
34
35
    /**
36
     * PropertyData constructor.
37
     * @param ReflectionProperty $property
38
     */
39
    public function __construct(ReflectionProperty $property)
40
    {
41
        $this->name = $property->getName();
42
        $this->fqn = "{$property->getDeclaringClass()->getName()}::{$property->getName()}";
43
        $this->boot($property);
44
    }
45 36
46
    protected function boot(reflectionProperty $reflectionProperty)
47 36
    {
48 36
        $this->annotations = $this->resolveAnnotations($reflectionProperty);
49 36
        $this->constraints = $this->resolveConstraints($reflectionProperty);
50 35
        $this->type = $this->resolveType($reflectionProperty);
51
        $this->immutable = $this->resolveImmutable();
52 36
        $this->optional = $this->resolveOptional();
53
    }
54 36
55 36
    protected function resolveType(ReflectionProperty $reflection)
56 36
    {
57 35
        return (new PropertyTypeResolver($reflection, $this->annotations))->resolve();
58 35
    }
59 35
60
    protected function resolveAnnotations(ReflectionProperty $reflection)
61 36
    {
62
        return (new AnnotationResolver($reflection))->resolve();
63 36
    }
64
65
    protected function resolveOptional()
66 36
    {
67
        foreach ($this->annotations as $annotation) {
68 36
            if ($annotation instanceof Optional) {
69 36
                return true;
70 7
            }
71
        }
72
73 36
        return false;
74
    }
75
76 35
    protected function resolveImmutable()
77
    {
78 35
        foreach ($this->annotations as $annotation) {
79 7
            if ($annotation instanceof Immutable) {
80 7
                return true;
81
            }
82
        }
83
84 34
        return false;
85
    }
86
87 35
    protected function resolveConstraints(ReflectionProperty $reflection)
88
    {
89 35
        return (new ConstraintsResolver($reflection, $this->annotations))->resolve();
90 7
    }
91 7
92
    /**
93
     * @return string
94
     */
95 34
    public function getName(): string
96
    {
97
        return $this->name;
98 36
    }
99
100 36
    /**
101
     * @return PropertyType
102
     */
103 36
    public function getType(): PropertyType
104
    {
105 36
        return $this->type;
106 36
    }
107 1
108
    /**
109
     * @return string
110 36
     */
111
    public function getFqn(): string
112
    {
113
        return $this->fqn;
114
    }
115
116 44
    /**
117
     * @return bool
118 44
     */
119
    public function isOptional(): bool
120
    {
121
        return $this->optional;
122
    }
123
124 41
    /**
125
     * @return bool
126 41
     */
127
    public function isImmutable(): bool
128
    {
129
        return $this->immutable;
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    public function getConstraints(): array
136
    {
137
        return $this->constraints;
138
    }
139
140 44
    /**
141
     * @return array
142 44
     */
143
    public function getAnnotations(): array
144
    {
145
        return $this->annotations;
146
    }
147
148 36
149
}
150