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
|
7 |
|
public function __construct( |
23
|
|
|
$object, |
24
|
|
|
CollectionInterface $properties = null, |
25
|
|
|
TypedCollectionInterface $injectionStrategies = null |
26
|
|
|
) { |
27
|
7 |
|
if (!is_object($object)) { |
28
|
1 |
|
throw new InvalidArgumentException; |
29
|
|
|
} |
30
|
|
|
|
31
|
6 |
|
$this->object = $object; |
32
|
|
|
|
33
|
6 |
|
$this->initProperties($properties); |
34
|
6 |
|
$this->initInjectionStrategies($injectionStrategies); |
35
|
6 |
|
} |
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 object with the list of properties set on it |
82
|
|
|
* |
83
|
|
|
* @return object |
84
|
|
|
*/ |
85
|
5 |
|
public function buildObject() |
86
|
|
|
{ |
87
|
5 |
|
foreach ($this->properties as $key => $value) { |
88
|
4 |
|
$this->inject($key, $value); |
89
|
|
|
} |
90
|
|
|
|
91
|
3 |
|
return $this->object; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Inject the given key/value pair into the object |
96
|
|
|
* |
97
|
|
|
* @param string $key |
98
|
|
|
* @param mixed $value |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
4 |
|
private function inject(string $key, $value) |
103
|
|
|
{ |
104
|
4 |
|
foreach ($this->injectionStrategies as $strategy) { |
105
|
4 |
|
if ($strategy->supports($this->object, $key, $value)) { |
106
|
2 |
|
$strategy->inject($this->object, $key, $value); |
107
|
|
|
|
108
|
4 |
|
return; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
throw new LogicException(sprintf( |
113
|
2 |
|
'Property "%s" cannot be injected', |
114
|
|
|
$key |
115
|
|
|
)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param CollectionInterface|null $properties |
120
|
|
|
* |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
6 |
|
private function initProperties(CollectionInterface $properties = null) |
124
|
|
|
{ |
125
|
6 |
|
if ($properties === null) { |
126
|
6 |
|
$properties = new Collection([]); |
127
|
|
|
} |
128
|
|
|
|
129
|
6 |
|
$this->properties = $properties; |
130
|
6 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param TypedCollectionInterface $strategies |
134
|
|
|
* |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
6 |
|
private function initInjectionStrategies(TypedCollectionInterface $strategies = null) |
138
|
|
|
{ |
139
|
6 |
|
if ($strategies === null) { |
140
|
6 |
|
$strategies = new TypedCollection( |
141
|
6 |
|
InjectionStrategyInterface::class, |
142
|
|
|
[ |
143
|
6 |
|
new SetterStrategy, |
144
|
6 |
|
new NamedMethodStrategy, |
145
|
6 |
|
new ReflectionStrategy, |
146
|
|
|
] |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
6 |
|
if ($strategies->getType() !== InjectionStrategyInterface::class) { |
151
|
|
|
throw new InvalidArgumentException; |
152
|
|
|
} |
153
|
|
|
|
154
|
6 |
|
$this->injectionStrategies = $strategies; |
155
|
6 |
|
} |
156
|
|
|
} |
157
|
|
|
|