forked from Innmind/Reflection
/
ReflectionClass.php
126 lines (112 loc) · 3.32 KB
/
ReflectionClass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
declare(strict_types = 1);
namespace Innmind\Reflection;
use Innmind\Reflection\Exception\InvalidArgumentException;
use Innmind\Reflection\Instanciator\ReflectionInstanciator;
use Innmind\Immutable\Collection;
use Innmind\Immutable\CollectionInterface;
use Innmind\Immutable\TypedCollectionInterface;
class ReflectionClass
{
private $class;
private $properties;
private $injectionStrategies;
private $instanciator;
public function __construct(
string $class,
CollectionInterface $properties = null,
TypedCollectionInterface $injectionStrategies = null,
InstanciatorInterface $instanciator = null
) {
$injectionStrategies = $injectionStrategies ?? InjectionStrategies::defaults();
if ($injectionStrategies->getType() !== InjectionStrategyInterface::class) {
throw new InvalidArgumentException;
}
$this->class = $class;
$this->properties = $properties ?? new Collection([]);
$this->injectionStrategies = $injectionStrategies;
$this->instanciator = $instanciator ?? new ReflectionInstanciator;
}
/**
* Add a property to be injected in the new object
*
* @param string $property
* @param mixed $value
*
* @return self
*/
public function withProperty(string $property, $value): self
{
return new self(
$this->class,
$this->properties->set($property, $value),
$this->injectionStrategies,
$this->instanciator
);
}
/**
* Add a set of properties that need to be injected
*
* @param array $properties
*
* @return self
*/
public function withProperties(array $properties): self
{
return new self(
$this->class,
$this->properties->merge(new Collection($properties)),
$this->injectionStrategies,
$this->instanciator
);
}
/**
* Return the collection of properties that will be injected in the object
*
* @return CollectionInterface
*/
public function getProperties(): CollectionInterface
{
return $this->properties;
}
/**
* Return the list of injection strategies used
*
* @return TypedCollectionInterface
*/
public function getInjectionStrategies(): TypedCollectionInterface
{
return $this->injectionStrategies;
}
/**
* Return the object instanciator
*
* @return InstanciatorInterface
*/
public function getInstanciator(): InstanciatorInterface
{
return $this->instanciator;
}
/**
* Return a new instance of the class
*
* @return object
*/
public function buildObject()
{
$object = $this->instanciator->build($this->class, $this->properties);
$parameters = $this->instanciator->getParameters($this->class);
//avoid injecting the properties already used in the constructor
$properties = $this
->properties
->filter(function($value, $property) use ($parameters) {
return !$parameters->contains($property);
});
$refl = new ReflectionObject(
$object,
$properties,
$this->injectionStrategies
);
return $refl->buildObject();
}
}