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