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