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
|
|
|
use Innmind\Immutable\StringPrimitive; |
16
|
|
|
|
17
|
|
|
class ReflectionObject |
18
|
|
|
{ |
19
|
|
|
private $object; |
20
|
|
|
private $properties; |
21
|
|
|
private $injectionStrategies; |
22
|
|
|
|
23
|
6 |
|
public function __construct( |
24
|
|
|
$object, |
25
|
|
|
CollectionInterface $properties = null, |
26
|
|
|
TypedCollectionInterface $injectionStrategies = null |
27
|
|
|
) { |
28
|
6 |
|
if (!is_object($object)) { |
29
|
1 |
|
throw new InvalidArgumentException; |
30
|
|
|
} |
31
|
|
|
|
32
|
5 |
|
$this->object = $object; |
33
|
|
|
|
34
|
5 |
|
if ($properties === null) { |
35
|
5 |
|
$properties = new Collection([]); |
36
|
|
|
} |
37
|
|
|
|
38
|
5 |
|
if ($injectionStrategies === null) { |
39
|
5 |
|
$injectionStrategies = new TypedCollection( |
40
|
5 |
|
InjectionStrategyInterface::class, |
41
|
|
|
[ |
42
|
5 |
|
new SetterStrategy, |
43
|
5 |
|
new NamedMethodStrategy, |
44
|
5 |
|
new ReflectionStrategy, |
45
|
|
|
] |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
5 |
|
if ($injectionStrategies->getType() !== InjectionStrategyInterface::class) { |
50
|
|
|
throw new InvalidArgumentException; |
51
|
|
|
} |
52
|
|
|
|
53
|
5 |
|
$this->properties = $properties; |
54
|
5 |
|
$this->injectionStrategies = $injectionStrategies; |
55
|
5 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Add a property that will be injected |
59
|
|
|
* |
60
|
|
|
* @param string $name |
61
|
|
|
* @param mixed $value |
62
|
|
|
* |
63
|
|
|
* @return self |
64
|
|
|
*/ |
65
|
4 |
|
public function withProperty(string $name, $value) |
66
|
|
|
{ |
67
|
4 |
|
return new self( |
68
|
4 |
|
$this->object, |
69
|
4 |
|
$this->properties->set($name, $value), |
70
|
4 |
|
$this->injectionStrategies |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Return the collection of properties that will be injected in the object |
76
|
|
|
* |
77
|
|
|
* @return CollectionInterface |
78
|
|
|
*/ |
79
|
2 |
|
public function getProperties(): CollectionInterface |
80
|
|
|
{ |
81
|
2 |
|
return $this->properties; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Return the object with the list of properties set on it |
86
|
|
|
* |
87
|
|
|
* @return object |
88
|
|
|
*/ |
89
|
4 |
|
public function buildObject() |
90
|
|
|
{ |
91
|
4 |
|
foreach ($this->properties as $key => $value) { |
92
|
3 |
|
$this->inject($key, $value); |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
return $this->object; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Inject the given key/value pair into the object |
100
|
|
|
* |
101
|
|
|
* @param string $key |
102
|
|
|
* @param mixed $value |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
3 |
|
private function inject(string $key, $value) |
107
|
|
|
{ |
108
|
3 |
|
foreach ($this->injectionStrategies as $strategy) { |
109
|
3 |
|
if ($strategy->supports($this->object, $key, $value)) { |
110
|
1 |
|
$strategy->inject($this->object, $key, $value); |
111
|
|
|
|
112
|
3 |
|
return; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
throw new LogicException(sprintf( |
117
|
2 |
|
'Property "%s" cannot be injected', |
118
|
|
|
$key |
119
|
|
|
)); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|