1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Reflection; |
5
|
|
|
|
6
|
|
|
use Innmind\Reflection\{ |
7
|
|
|
ExtractionStrategy\ExtractionStrategies, |
8
|
|
|
InjectionStrategy\InjectionStrategies, |
9
|
|
|
Exception\InvalidArgumentException, |
10
|
|
|
}; |
11
|
|
|
use Innmind\Immutable\{ |
12
|
|
|
MapInterface, |
13
|
|
|
Map, |
14
|
|
|
}; |
15
|
|
|
|
16
|
|
|
final class ReflectionObject |
17
|
|
|
{ |
18
|
|
|
private $object; |
19
|
|
|
private $properties; |
20
|
|
|
private $injectionStrategy; |
21
|
|
|
private $extractionStrategy; |
22
|
|
|
|
23
|
10 |
|
public function __construct( |
24
|
|
|
object $object, |
25
|
|
|
MapInterface $properties = null, |
26
|
|
|
InjectionStrategy $injectionStrategy = null, |
27
|
|
|
ExtractionStrategy $extractionStrategy = null |
28
|
|
|
) { |
29
|
10 |
|
$properties = $properties ?? new Map('string', 'mixed'); |
30
|
|
|
|
31
|
|
|
if ( |
32
|
10 |
|
(string) $properties->keyType() !== 'string' || |
33
|
10 |
|
(string) $properties->valueType() !== 'mixed' |
34
|
|
|
) { |
35
|
|
|
throw new \TypeError('Argument 2 must be of type MapInterface<string, mixed>'); |
36
|
|
|
} |
37
|
|
|
|
38
|
10 |
|
$this->object = $object; |
39
|
10 |
|
$this->properties = $properties; |
40
|
10 |
|
$this->injectionStrategy = $injectionStrategy ?? InjectionStrategies::default(); |
41
|
10 |
|
$this->extractionStrategy = $extractionStrategy ?? ExtractionStrategies::default(); |
42
|
10 |
|
} |
43
|
|
|
|
44
|
5 |
|
public static function of( |
45
|
|
|
object $object, |
46
|
|
|
MapInterface $properties = null, |
47
|
|
|
InjectionStrategy $injectionStrategy = null, |
48
|
|
|
ExtractionStrategy $extractionStrategy = null |
49
|
|
|
): self { |
50
|
5 |
|
return new self($object, $properties, $injectionStrategy, $extractionStrategy); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Add a property that will be injected |
55
|
|
|
* |
56
|
|
|
* @param mixed $value |
57
|
|
|
*/ |
58
|
4 |
|
public function withProperty(string $name, $value): self |
59
|
|
|
{ |
60
|
4 |
|
return new self( |
61
|
4 |
|
$this->object, |
62
|
4 |
|
$this->properties->put($name, $value), |
63
|
4 |
|
$this->injectionStrategy, |
64
|
4 |
|
$this->extractionStrategy |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Add a set of properties that need to be injected |
70
|
|
|
* |
71
|
|
|
* @param array<string, mixed> $properties |
72
|
|
|
* |
73
|
|
|
* @return self |
74
|
|
|
*/ |
75
|
1 |
|
public function withProperties(array $properties): self |
76
|
|
|
{ |
77
|
1 |
|
$map = $this->properties; |
78
|
|
|
|
79
|
1 |
|
foreach ($properties as $key => $value) { |
80
|
1 |
|
$map = $map->put($key, $value); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return new self( |
84
|
1 |
|
$this->object, |
85
|
1 |
|
$map, |
86
|
1 |
|
$this->injectionStrategy, |
87
|
1 |
|
$this->extractionStrategy |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return the object with the list of properties set on it |
93
|
|
|
*/ |
94
|
7 |
|
public function build(): object |
95
|
|
|
{ |
96
|
7 |
|
return $this->properties->reduce( |
97
|
7 |
|
$this->object, |
98
|
|
|
function(object $object, string $key, $value): object { |
99
|
5 |
|
return $this->inject($object, $key, $value); |
100
|
7 |
|
} |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Extract the given list of properties |
106
|
|
|
* |
107
|
|
|
* @return MapInterface<string, mixed> |
108
|
|
|
*/ |
109
|
2 |
|
public function extract(string ...$properties): MapInterface |
110
|
|
|
{ |
111
|
2 |
|
$map = new Map('string', 'mixed'); |
112
|
|
|
|
113
|
2 |
|
foreach ($properties as $property) { |
114
|
2 |
|
$map = $map->put( |
115
|
2 |
|
$property, |
116
|
2 |
|
$this->extractProperty($property) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
return $map; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Inject the given key/value pair into the object |
125
|
|
|
* |
126
|
|
|
* @param mixed $value |
127
|
|
|
*/ |
128
|
5 |
|
private function inject(object $object, string $key, $value): object |
129
|
|
|
{ |
130
|
5 |
|
return $this->injectionStrategy->inject($object, $key, $value); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Extract the given property out of the object |
135
|
|
|
* |
136
|
|
|
* @return mixed |
137
|
|
|
*/ |
138
|
2 |
|
private function extractProperty(string $property) |
139
|
|
|
{ |
140
|
2 |
|
return $this->extractionStrategy->extract($this->object, $property); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|