1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Larapie\DataTransferObject\Property; |
4
|
|
|
|
5
|
|
|
use ReflectionProperty; |
6
|
|
|
use Larapie\DataTransferObject\Annotations\Optional; |
7
|
|
|
use Larapie\DataTransferObject\Annotations\Immutable; |
8
|
|
|
use Larapie\DataTransferObject\Resolvers\AnnotationResolver; |
9
|
|
|
use Larapie\DataTransferObject\Resolvers\ConstraintsResolver; |
10
|
|
|
use Larapie\DataTransferObject\Resolvers\PropertyTypeResolver; |
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
|
36 |
|
public function __construct(ReflectionProperty $property) |
40
|
|
|
{ |
41
|
36 |
|
$this->boot($property); |
42
|
35 |
|
} |
43
|
|
|
|
44
|
36 |
|
protected function boot(reflectionProperty $reflectionProperty) |
45
|
|
|
{ |
46
|
36 |
|
$this->name = $reflectionProperty->getName(); |
47
|
36 |
|
$this->fqn = "{$reflectionProperty->getDeclaringClass()->getName()}::{$reflectionProperty->getName()}"; |
48
|
36 |
|
$this->annotations = $this->resolveAnnotations($reflectionProperty); |
49
|
36 |
|
$this->constraints = $this->resolveConstraints($reflectionProperty); |
50
|
36 |
|
$this->type = $this->resolveType($reflectionProperty); |
51
|
35 |
|
$this->immutable = $this->resolveImmutable(); |
52
|
35 |
|
$this->optional = $this->resolveOptional(); |
53
|
35 |
|
} |
54
|
|
|
|
55
|
36 |
|
protected function resolveType(ReflectionProperty $reflection) |
56
|
|
|
{ |
57
|
36 |
|
return (new PropertyTypeResolver($reflection, $this->annotations))->resolve(); |
58
|
|
|
} |
59
|
|
|
|
60
|
36 |
|
protected function resolveAnnotations(ReflectionProperty $reflection) |
61
|
|
|
{ |
62
|
36 |
|
return (new AnnotationResolver($reflection))->resolve(); |
63
|
|
|
} |
64
|
|
|
|
65
|
35 |
|
protected function resolveOptional() |
66
|
|
|
{ |
67
|
35 |
|
foreach ($this->annotations as $annotation) { |
68
|
7 |
|
if ($annotation instanceof Optional) { |
69
|
7 |
|
return true; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
34 |
|
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
|
36 |
|
protected function resolveConstraints(ReflectionProperty $reflection) |
88
|
|
|
{ |
89
|
36 |
|
return (new ConstraintsResolver($reflection, $this->annotations))->resolve(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
44 |
|
public function getName(): string |
96
|
|
|
{ |
97
|
44 |
|
return $this->name; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return PropertyType |
102
|
|
|
*/ |
103
|
41 |
|
public function getType(): PropertyType |
104
|
|
|
{ |
105
|
41 |
|
return $this->type; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getFqn(): string |
112
|
|
|
{ |
113
|
|
|
return $this->fqn; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
44 |
|
public function isOptional(): bool |
120
|
|
|
{ |
121
|
44 |
|
return $this->optional; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
36 |
|
public function isImmutable(): bool |
128
|
|
|
{ |
129
|
36 |
|
return $this->immutable; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
41 |
|
public function getConstraints(): array |
136
|
|
|
{ |
137
|
41 |
|
return $this->constraints; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
public function getAnnotations(): array |
144
|
|
|
{ |
145
|
|
|
return $this->annotations; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|