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
|
6 |
|
public function __construct( |
23
|
|
|
$object, |
24
|
|
|
CollectionInterface $properties = null, |
25
|
|
|
TypedCollectionInterface $injectionStrategies = null |
26
|
|
|
) { |
27
|
6 |
|
if (!is_object($object)) { |
28
|
1 |
|
throw new InvalidArgumentException; |
29
|
|
|
} |
30
|
|
|
|
31
|
5 |
|
$this->object = $object; |
32
|
|
|
|
33
|
5 |
|
$this->initProperties($properties); |
34
|
5 |
|
$this->initInjectionStrategies($injectionStrategies); |
35
|
5 |
|
} |
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
|
|
|
* Return the collection of properties that will be injected in the object |
56
|
|
|
* |
57
|
|
|
* @return CollectionInterface |
58
|
|
|
*/ |
59
|
2 |
|
public function getProperties(): CollectionInterface |
60
|
|
|
{ |
61
|
2 |
|
return $this->properties; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Return the object with the list of properties set on it |
66
|
|
|
* |
67
|
|
|
* @return object |
68
|
|
|
*/ |
69
|
4 |
|
public function buildObject() |
70
|
|
|
{ |
71
|
4 |
|
foreach ($this->properties as $key => $value) { |
72
|
3 |
|
$this->inject($key, $value); |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
return $this->object; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Inject the given key/value pair into the object |
80
|
|
|
* |
81
|
|
|
* @param string $key |
82
|
|
|
* @param mixed $value |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
3 |
|
private function inject(string $key, $value) |
87
|
|
|
{ |
88
|
3 |
|
foreach ($this->injectionStrategies as $strategy) { |
89
|
3 |
|
if ($strategy->supports($this->object, $key, $value)) { |
90
|
1 |
|
$strategy->inject($this->object, $key, $value); |
91
|
|
|
|
92
|
3 |
|
return; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
throw new LogicException(sprintf( |
97
|
2 |
|
'Property "%s" cannot be injected', |
98
|
|
|
$key |
99
|
|
|
)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param CollectionInterface|null $properties |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
5 |
|
private function initProperties(CollectionInterface $properties = null) |
108
|
|
|
{ |
109
|
5 |
|
if ($properties === null) { |
110
|
5 |
|
$properties = new Collection([]); |
111
|
|
|
} |
112
|
|
|
|
113
|
5 |
|
$this->properties = $properties; |
114
|
5 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param TypedCollectionInterface $strategies |
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
5 |
|
private function initInjectionStrategies(TypedCollectionInterface $strategies = null) |
122
|
|
|
{ |
123
|
5 |
|
if ($strategies === null) { |
124
|
5 |
|
$strategies = new TypedCollection( |
125
|
5 |
|
InjectionStrategyInterface::class, |
126
|
|
|
[ |
127
|
5 |
|
new SetterStrategy, |
128
|
5 |
|
new NamedMethodStrategy, |
129
|
5 |
|
new ReflectionStrategy, |
130
|
|
|
] |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
5 |
|
if ($strategies->getType() !== InjectionStrategyInterface::class) { |
135
|
|
|
throw new InvalidArgumentException; |
136
|
|
|
} |
137
|
|
|
|
138
|
5 |
|
$this->injectionStrategies = $strategies; |
139
|
5 |
|
} |
140
|
|
|
} |
141
|
|
|
|