|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\Reflection; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Reflection\Exception\InvalidArgumentException; |
|
7
|
|
|
use Innmind\Reflection\Exception\LogicException; |
|
8
|
|
|
use Innmind\Reflection\InjectionStrategy\SetterStrategy; |
|
9
|
|
|
use Innmind\Reflection\InjectionStrategy\NamedMethodStrategy; |
|
10
|
|
|
use Innmind\Reflection\InjectionStrategy\ReflectionStrategy; |
|
11
|
|
|
use Innmind\Immutable\Collection; |
|
12
|
|
|
use Innmind\Immutable\CollectionInterface; |
|
13
|
|
|
use Innmind\Immutable\TypedCollection; |
|
14
|
|
|
use Innmind\Immutable\TypedCollectionInterface; |
|
15
|
|
|
|
|
16
|
|
|
class ReflectionObject |
|
17
|
|
|
{ |
|
18
|
|
|
private $object; |
|
19
|
|
|
private $properties; |
|
20
|
|
|
private $injectionStrategies; |
|
21
|
|
|
|
|
22
|
8 |
|
public function __construct( |
|
23
|
|
|
$object, |
|
24
|
|
|
CollectionInterface $properties = null, |
|
25
|
|
|
TypedCollectionInterface $injectionStrategies = null |
|
26
|
|
|
) { |
|
27
|
8 |
|
if (!is_object($object)) { |
|
28
|
1 |
|
throw new InvalidArgumentException; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
7 |
|
$this->object = $object; |
|
32
|
|
|
|
|
33
|
7 |
|
$this->initProperties($properties); |
|
34
|
7 |
|
$this->initInjectionStrategies($injectionStrategies); |
|
35
|
7 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Add a property that will be injected |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $name |
|
41
|
|
|
* @param mixed $value |
|
42
|
|
|
* |
|
43
|
|
|
* @return self |
|
44
|
|
|
*/ |
|
45
|
4 |
|
public function withProperty(string $name, $value) |
|
46
|
|
|
{ |
|
47
|
4 |
|
return new self( |
|
48
|
4 |
|
$this->object, |
|
49
|
4 |
|
$this->properties->set($name, $value), |
|
50
|
4 |
|
$this->injectionStrategies |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Add a set of properties that need to be injected |
|
56
|
|
|
* |
|
57
|
|
|
* @param array $properties |
|
58
|
|
|
* |
|
59
|
|
|
* @return self |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function withProperties(array $properties): self |
|
62
|
|
|
{ |
|
63
|
1 |
|
return new self( |
|
64
|
1 |
|
$this->object, |
|
65
|
1 |
|
$this->properties->merge(new Collection($properties)), |
|
66
|
1 |
|
$this->injectionStrategies |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Return the collection of properties that will be injected in the object |
|
72
|
|
|
* |
|
73
|
|
|
* @return CollectionInterface |
|
74
|
|
|
*/ |
|
75
|
2 |
|
public function getProperties(): CollectionInterface |
|
76
|
|
|
{ |
|
77
|
2 |
|
return $this->properties; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Return the list of injection strategies used |
|
82
|
|
|
* |
|
83
|
|
|
* @return TypedCollectionInterface |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function getInjectionStrategies(): TypedCollectionInterface |
|
86
|
|
|
{ |
|
87
|
1 |
|
return $this->injectionStrategies; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Return the object with the list of properties set on it |
|
92
|
|
|
* |
|
93
|
|
|
* @return object |
|
94
|
|
|
*/ |
|
95
|
5 |
|
public function buildObject() |
|
96
|
|
|
{ |
|
97
|
5 |
|
foreach ($this->properties as $key => $value) { |
|
98
|
4 |
|
$this->inject($key, $value); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
3 |
|
return $this->object; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Inject the given key/value pair into the object |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $key |
|
108
|
|
|
* @param mixed $value |
|
109
|
|
|
* |
|
110
|
|
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
4 |
|
private function inject(string $key, $value) |
|
113
|
|
|
{ |
|
114
|
4 |
|
foreach ($this->injectionStrategies as $strategy) { |
|
115
|
4 |
|
if ($strategy->supports($this->object, $key, $value)) { |
|
116
|
2 |
|
$strategy->inject($this->object, $key, $value); |
|
117
|
|
|
|
|
118
|
4 |
|
return; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
2 |
|
throw new LogicException(sprintf( |
|
123
|
2 |
|
'Property "%s" cannot be injected', |
|
124
|
|
|
$key |
|
125
|
|
|
)); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param CollectionInterface|null $properties |
|
130
|
|
|
* |
|
131
|
|
|
* @return void |
|
132
|
|
|
*/ |
|
133
|
7 |
|
private function initProperties(CollectionInterface $properties = null) |
|
134
|
|
|
{ |
|
135
|
7 |
|
if ($properties === null) { |
|
136
|
7 |
|
$properties = new Collection([]); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
7 |
|
$this->properties = $properties; |
|
140
|
7 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param TypedCollectionInterface $strategies |
|
144
|
|
|
* |
|
145
|
|
|
* @return void |
|
146
|
|
|
*/ |
|
147
|
7 |
|
private function initInjectionStrategies(TypedCollectionInterface $strategies = null) |
|
148
|
|
|
{ |
|
149
|
7 |
|
if ($strategies === null) { |
|
150
|
7 |
|
$strategies = new TypedCollection( |
|
151
|
7 |
|
InjectionStrategyInterface::class, |
|
152
|
|
|
[ |
|
153
|
7 |
|
new SetterStrategy, |
|
154
|
7 |
|
new NamedMethodStrategy, |
|
155
|
7 |
|
new ReflectionStrategy, |
|
156
|
|
|
] |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
7 |
|
if ($strategies->getType() !== InjectionStrategyInterface::class) { |
|
161
|
|
|
throw new InvalidArgumentException; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
7 |
|
$this->injectionStrategies = $strategies; |
|
165
|
7 |
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|