|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Composite Utils package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Emily Shepherd <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the |
|
8
|
|
|
* LICENSE.md file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @package spaark/composite-utils |
|
11
|
|
|
* @author Emily Shepherd <[email protected]> |
|
12
|
|
|
* @license MIT |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Spaark\CompositeUtils\Factory\Reflection; |
|
16
|
|
|
|
|
17
|
|
|
use Spaark\CompositeUtils\Model\Reflection\ReflectionComposite; |
|
18
|
|
|
use Spaark\CompositeUtils\Model\Reflection\ReflectionProperty; |
|
19
|
|
|
use Spaark\CompositeUtils\Model\Reflection\ReflectionParameter; |
|
20
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\BooleanType; |
|
21
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\CollectionType; |
|
22
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\IntegerType; |
|
23
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\MixedType; |
|
24
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\ObjectType; |
|
25
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\StringType; |
|
26
|
|
|
use Spaark\CompositeUtils\Service\RawPropertyAccessor; |
|
27
|
|
|
use \ReflectionProperty as PHPNativeReflectionProperty; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Builds a ReflectionProperty for a given class and property name |
|
31
|
|
|
*/ |
|
32
|
|
|
class ReflectionPropertyFactory extends ReflectorFactory |
|
33
|
|
|
{ |
|
34
|
|
|
const REFLECTION_OBJECT = ReflectionProperty::class; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var PHPNativeReflectionProperty |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $reflector; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var ReflectionProperty |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $object; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritDoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $acceptedParams = |
|
50
|
|
|
[ |
|
51
|
|
|
'readable' => 'setBool', |
|
52
|
|
|
'writable' => 'setBool', |
|
53
|
|
|
'var' => 'setType', |
|
54
|
|
|
'construct' => 'setConstruct' |
|
55
|
|
|
]; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns a new ReflectionPropertyFactory using the given class and |
|
59
|
|
|
* property names |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $class The classname of the property |
|
62
|
|
|
* @param string $property The property to reflect |
|
63
|
|
|
* @return ReflectionPropertyFactory |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function fromName($class, $property) |
|
66
|
|
|
{ |
|
67
|
|
|
return new static(new PHPNativeReflectionProperty |
|
68
|
|
|
( |
|
69
|
|
|
$class, $property |
|
70
|
|
|
)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Builds the ReflectionProperty from the provided parameters, |
|
75
|
|
|
* linking to a parent ReflectionComposite |
|
76
|
|
|
* |
|
77
|
|
|
* @param ReflectionCompostite $parent The reflector for the class |
|
78
|
|
|
* this property belongs to |
|
79
|
|
|
* @param mixed $default This property's default value |
|
80
|
|
|
* @return ReflectionProperty |
|
81
|
|
|
*/ |
|
82
|
19 |
|
public function build(ReflectionComposite $parent, $default) |
|
83
|
|
|
{ |
|
84
|
19 |
|
$this->accessor->setRawValue('owner', $parent); |
|
85
|
19 |
|
$this->accessor->setRawValue('defaultValue', $default); |
|
86
|
19 |
|
$this->accessor->setRawValue |
|
87
|
|
|
( |
|
88
|
19 |
|
'name', |
|
89
|
19 |
|
$this->reflector->getName() |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
19 |
|
$this->parseDocComment(); |
|
93
|
|
|
|
|
94
|
19 |
|
return $this->object; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Sets the property's type by parsing the @type annotation |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $name Should be 'var' |
|
101
|
|
|
* @param string $value The value of the annotation |
|
102
|
|
|
*/ |
|
103
|
19 |
|
protected function setType($name, $value) |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
19 |
View Code Duplication |
if ($value{0} !== '?') |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
19 |
|
$nullable = false; |
|
108
|
|
|
} |
|
109
|
|
|
else |
|
110
|
|
|
{ |
|
111
|
16 |
|
$nullable = true; |
|
112
|
16 |
|
$value = substr($value, 1); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
19 |
View Code Duplication |
if (substr($value, -2) !== '[]') |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
19 |
|
$collection = false; |
|
118
|
|
|
} |
|
119
|
|
|
else |
|
120
|
|
|
{ |
|
121
|
1 |
|
$collection = true; |
|
122
|
1 |
|
$value = substr($value, 0, -2); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
switch ($value) |
|
126
|
|
|
{ |
|
127
|
19 |
|
case 'string': |
|
128
|
17 |
|
$class = new StringType(); |
|
129
|
17 |
|
break; |
|
130
|
19 |
|
case 'int': |
|
131
|
19 |
|
case 'integer': |
|
132
|
1 |
|
$class = new IntegerType(); |
|
133
|
1 |
|
break; |
|
134
|
19 |
|
case 'bool': |
|
135
|
19 |
|
case 'boolean': |
|
136
|
18 |
|
$class = new BooleanType(); |
|
137
|
18 |
|
break; |
|
138
|
19 |
|
case 'mixed': |
|
139
|
19 |
|
case '': |
|
140
|
|
|
$class = new MixedType(); |
|
141
|
|
|
break; |
|
142
|
19 |
|
case 'null': |
|
143
|
|
|
$class = new NullType(); |
|
144
|
|
|
break; |
|
145
|
|
|
default: |
|
146
|
|
|
$useStatements = |
|
147
|
19 |
|
$this->object->owner->namespace->useStatements; |
|
148
|
|
|
|
|
149
|
19 |
|
if ($useStatements->containsKey($value)) |
|
|
|
|
|
|
150
|
|
|
{ |
|
151
|
17 |
|
$value = $useStatements[$value]->classname; |
|
152
|
|
|
} |
|
153
|
|
|
else |
|
154
|
|
|
{ |
|
155
|
19 |
|
$value = $this->object->owner->namespace->namespace |
|
156
|
19 |
|
. '\\' . $value; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
19 |
|
$class = new ObjectType($value); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
19 |
|
if ($nullable) |
|
163
|
|
|
{ |
|
164
|
16 |
|
(new RawPropertyAccessor($class)) |
|
165
|
16 |
|
->setRawValue('nullable', true); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
19 |
|
if ($collection) |
|
169
|
|
|
{ |
|
170
|
1 |
|
$class = new CollectionType($class); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
19 |
|
$this->accessor->setRawValue('type', $class); |
|
174
|
19 |
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Sets the property's constructor options by parsing the @construct |
|
178
|
|
|
* annotation |
|
179
|
|
|
* |
|
180
|
|
|
* @param string $name Should be 'construct' |
|
181
|
|
|
* @param string $value The value of the annotation |
|
182
|
|
|
*/ |
|
183
|
18 |
|
protected function setConstruct($name, $value) |
|
|
|
|
|
|
184
|
|
|
{ |
|
185
|
18 |
|
$value = explode(' ', $value); |
|
186
|
|
|
$compositeAccessor = |
|
187
|
18 |
|
new RawPropertyAccessor($this->object->owner); |
|
188
|
|
|
|
|
189
|
18 |
|
switch ($value[0]) |
|
190
|
|
|
{ |
|
191
|
18 |
|
case 'required': |
|
192
|
18 |
|
$this->accessor->setRawValue |
|
193
|
|
|
( |
|
194
|
18 |
|
'passedToConstructor', |
|
195
|
18 |
|
true |
|
196
|
|
|
); |
|
197
|
18 |
|
$this->accessor->setRawValue |
|
198
|
|
|
( |
|
199
|
18 |
|
'requiredInConstructor', |
|
200
|
18 |
|
true |
|
201
|
|
|
); |
|
202
|
18 |
|
$compositeAccessor->rawAddToValue |
|
|
|
|
|
|
203
|
|
|
( |
|
204
|
18 |
|
'requiredProperties', |
|
205
|
18 |
|
$this->object |
|
206
|
|
|
); |
|
207
|
18 |
|
break; |
|
208
|
17 |
|
case 'new': |
|
209
|
17 |
|
$this->accessor->setRawValue |
|
210
|
|
|
( |
|
211
|
17 |
|
'builtInConstructor', |
|
212
|
17 |
|
true |
|
213
|
|
|
); |
|
214
|
17 |
|
$compositeAccessor->rawAddToValue |
|
|
|
|
|
|
215
|
|
|
( |
|
216
|
17 |
|
'builtProperties', |
|
217
|
17 |
|
$this->object |
|
218
|
|
|
); |
|
219
|
17 |
|
break; |
|
220
|
17 |
|
case 'optional': |
|
221
|
17 |
|
$this->accessor->setRawValue |
|
222
|
|
|
( |
|
223
|
17 |
|
'passedToConstructor', |
|
224
|
17 |
|
true |
|
225
|
|
|
); |
|
226
|
17 |
|
$compositeAccessor->rawAddToValue |
|
|
|
|
|
|
227
|
|
|
( |
|
228
|
17 |
|
'optionalProperties', |
|
229
|
17 |
|
$this->object |
|
230
|
|
|
); |
|
231
|
|
|
|
|
232
|
17 |
|
if (isset($value[1]) && $value[1] === 'new') |
|
233
|
|
|
{ |
|
234
|
17 |
|
$this->accessor->setRawValue |
|
235
|
|
|
( |
|
236
|
17 |
|
'builtInConstructor', |
|
237
|
17 |
|
true |
|
238
|
|
|
); |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
18 |
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.