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
|
|
|
* Returns a new ReflectionPropertyFactory using the given class and |
48
|
|
|
* property names |
49
|
|
|
* |
50
|
|
|
* @param string $class The classname of the property |
51
|
|
|
* @param string $property The property to reflect |
52
|
|
|
* @return ReflectionPropertyFactory |
53
|
|
|
*/ |
54
|
|
|
public static function fromName($class, $property) |
55
|
|
|
{ |
56
|
|
|
return new static(new PHPNativeReflectionProperty |
57
|
|
|
( |
58
|
|
|
$class, $property |
59
|
|
|
)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Builds the ReflectionProperty from the provided parameters, |
64
|
|
|
* linking to a parent ReflectionComposite |
65
|
|
|
* |
66
|
|
|
* @param ReflectionCompostite $parent The reflector for the class |
67
|
|
|
* this property belongs to |
68
|
|
|
* @param mixed $default This property's default value |
69
|
|
|
* @return ReflectionProperty |
70
|
|
|
*/ |
71
|
20 |
|
public function build(ReflectionComposite $parent, $default) |
72
|
|
|
{ |
73
|
20 |
|
$this->accessor->setRawValue('owner', $parent); |
74
|
20 |
|
$this->accessor->setRawValue('defaultValue', $default); |
75
|
20 |
|
$this->accessor->setRawValue |
76
|
|
|
( |
77
|
20 |
|
'name', |
78
|
20 |
|
$this->reflector->getName() |
79
|
|
|
); |
80
|
|
|
|
81
|
20 |
|
$this->parseDocComment |
82
|
|
|
([ |
83
|
20 |
|
'readable' => 'setBool', |
84
|
|
|
'writable' => 'setBool', |
85
|
|
|
'var' => 'setType', |
86
|
|
|
'construct' => 'setConstruct' |
87
|
|
|
]); |
88
|
|
|
|
89
|
20 |
|
return $this->object; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Sets the property's type by parsing the @type annotation |
94
|
|
|
* |
95
|
|
|
* @param string $name Should be 'var' |
96
|
|
|
* @param string $value The value of the annotation |
97
|
|
|
*/ |
98
|
20 |
|
protected function setType($name, $value) |
|
|
|
|
99
|
|
|
{ |
100
|
20 |
|
$this->accessor->setRawValue |
101
|
|
|
( |
102
|
20 |
|
'type', |
103
|
20 |
|
(new TypeParser($this->object->owner))->parse($value) |
104
|
|
|
); |
105
|
20 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Sets the property's constructor options by parsing the @construct |
109
|
|
|
* annotation |
110
|
|
|
* |
111
|
|
|
* @param string $name Should be 'construct' |
112
|
|
|
* @param string $value The value of the annotation |
113
|
|
|
*/ |
114
|
19 |
|
protected function setConstruct($name, $value) |
|
|
|
|
115
|
|
|
{ |
116
|
19 |
|
$value = explode(' ', $value); |
117
|
|
|
$compositeAccessor = |
118
|
19 |
|
new RawPropertyAccessor($this->object->owner); |
119
|
|
|
|
120
|
19 |
|
switch ($value[0]) |
121
|
|
|
{ |
122
|
19 |
|
case 'required': |
123
|
19 |
|
$this->accessor->setRawValue |
124
|
|
|
( |
125
|
19 |
|
'passedToConstructor', |
126
|
19 |
|
true |
127
|
|
|
); |
128
|
19 |
|
$this->accessor->setRawValue |
129
|
|
|
( |
130
|
19 |
|
'requiredInConstructor', |
131
|
19 |
|
true |
132
|
|
|
); |
133
|
19 |
|
$compositeAccessor->rawAddToValue |
|
|
|
|
134
|
|
|
( |
135
|
19 |
|
'requiredProperties', |
136
|
19 |
|
$this->object |
137
|
|
|
); |
138
|
19 |
|
break; |
139
|
19 |
|
case 'new': |
140
|
19 |
|
$this->accessor->setRawValue |
141
|
|
|
( |
142
|
19 |
|
'builtInConstructor', |
143
|
19 |
|
true |
144
|
|
|
); |
145
|
19 |
|
$compositeAccessor->rawAddToValue |
|
|
|
|
146
|
|
|
( |
147
|
19 |
|
'builtProperties', |
148
|
19 |
|
$this->object |
149
|
|
|
); |
150
|
19 |
|
break; |
151
|
19 |
|
case 'optional': |
152
|
19 |
|
$this->accessor->setRawValue |
153
|
|
|
( |
154
|
19 |
|
'passedToConstructor', |
155
|
19 |
|
true |
156
|
|
|
); |
157
|
19 |
|
$compositeAccessor->rawAddToValue |
|
|
|
|
158
|
|
|
( |
159
|
19 |
|
'optionalProperties', |
160
|
19 |
|
$this->object |
161
|
|
|
); |
162
|
|
|
|
163
|
19 |
|
if (isset($value[1]) && $value[1] === 'new') |
164
|
|
|
{ |
165
|
19 |
|
$this->accessor->setRawValue |
166
|
|
|
( |
167
|
19 |
|
'builtInConstructor', |
168
|
19 |
|
true |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
} |
172
|
19 |
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.