|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\Reflection; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Immutable\Collection; |
|
7
|
|
|
use Innmind\Immutable\CollectionInterface; |
|
8
|
|
|
use Innmind\Reflection\InjectionStrategy\DefaultInjectionStrategies; |
|
9
|
|
|
use Innmind\Reflection\InjectionStrategy\InjectionStrategies; |
|
10
|
|
|
use Innmind\Reflection\Instanciator\ReflectionInstanciator; |
|
11
|
|
|
|
|
12
|
|
|
class ReflectionClass |
|
13
|
|
|
{ |
|
14
|
|
|
private $class; |
|
15
|
|
|
private $properties; |
|
16
|
|
|
private $injectionStrategies; |
|
17
|
|
|
private $instanciator; |
|
18
|
|
|
|
|
19
|
5 |
|
public function __construct( |
|
20
|
|
|
string $class, |
|
21
|
|
|
CollectionInterface $properties = null, |
|
22
|
|
|
InjectionStrategies $injectionStrategies = null, |
|
23
|
|
|
InstanciatorInterface $instanciator = null |
|
24
|
|
|
) { |
|
25
|
5 |
|
$injectionStrategies = $injectionStrategies ?? new DefaultInjectionStrategies(); |
|
26
|
|
|
|
|
27
|
5 |
|
$this->class = $class; |
|
28
|
5 |
|
$this->properties = $properties ?? new Collection([]); |
|
29
|
5 |
|
$this->injectionStrategies = $injectionStrategies; |
|
30
|
5 |
|
$this->instanciator = $instanciator ?? new ReflectionInstanciator; |
|
31
|
5 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Add a property to be injected in the new object |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $property |
|
37
|
|
|
* @param mixed $value |
|
38
|
|
|
* |
|
39
|
|
|
* @return self |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function withProperty(string $property, $value): self |
|
42
|
|
|
{ |
|
43
|
2 |
|
return new self( |
|
44
|
2 |
|
$this->class, |
|
45
|
2 |
|
$this->properties->set($property, $value), |
|
46
|
2 |
|
$this->injectionStrategies, |
|
47
|
2 |
|
$this->instanciator |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Add a set of properties that need to be injected |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $properties |
|
55
|
|
|
* |
|
56
|
|
|
* @return self |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function withProperties(array $properties): self |
|
59
|
|
|
{ |
|
60
|
1 |
|
return new self( |
|
61
|
1 |
|
$this->class, |
|
62
|
1 |
|
$this->properties->merge(new Collection($properties)), |
|
|
|
|
|
|
63
|
1 |
|
$this->injectionStrategies, |
|
64
|
1 |
|
$this->instanciator |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Return the collection of properties that will be injected in the object |
|
70
|
|
|
* |
|
71
|
|
|
* @return CollectionInterface |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function getProperties(): CollectionInterface |
|
74
|
|
|
{ |
|
75
|
1 |
|
return $this->properties; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Return the list of injection strategies used |
|
80
|
|
|
* |
|
81
|
|
|
* @return InjectionStrategies |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function getInjectionStrategies(): InjectionStrategies |
|
84
|
|
|
{ |
|
85
|
1 |
|
return $this->injectionStrategies; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Return the object instanciator |
|
90
|
|
|
* |
|
91
|
|
|
* @return InstanciatorInterface |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function getInstanciator(): InstanciatorInterface |
|
94
|
|
|
{ |
|
95
|
1 |
|
return $this->instanciator; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Return a new instance of the class |
|
100
|
|
|
* |
|
101
|
|
|
* @return object |
|
102
|
|
|
*/ |
|
103
|
2 |
|
public function buildObject() |
|
104
|
|
|
{ |
|
105
|
2 |
|
$object = $this->instanciator->build($this->class, $this->properties); |
|
106
|
2 |
|
$parameters = $this->instanciator->getParameters($this->class); |
|
107
|
|
|
|
|
108
|
|
|
//avoid injecting the properties already used in the constructor |
|
109
|
|
|
$properties = $this |
|
110
|
2 |
|
->properties |
|
111
|
2 |
|
->filter( |
|
112
|
2 |
|
function($value, $property) use ($parameters) { |
|
113
|
1 |
|
return !$parameters->contains($property); |
|
114
|
2 |
|
} |
|
115
|
|
|
); |
|
116
|
2 |
|
$refl = new ReflectionObject( |
|
117
|
|
|
$object, |
|
118
|
|
|
$properties, |
|
119
|
2 |
|
$this->injectionStrategies |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
2 |
|
return $refl->buildObject(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: