1 | <?php |
||
14 | class ReflectionObject |
||
15 | { |
||
16 | private $object; |
||
17 | private $properties; |
||
18 | private $injectionStrategies; |
||
19 | private $extractionStrategies; |
||
20 | |||
21 | 14 | public function __construct( |
|
22 | $object, |
||
23 | CollectionInterface $properties = null, |
||
24 | InjectionStrategies $injectionStrategies = null, |
||
25 | ExtractionStrategies $extractionStrategies = null |
||
26 | ) { |
||
27 | 14 | if (!is_object($object)) { |
|
28 | 1 | throw new InvalidArgumentException; |
|
29 | } |
||
30 | |||
31 | 13 | $this->injectionStrategies = $injectionStrategies ?? new DefaultInjectionStrategies(); |
|
32 | 13 | $this->extractionStrategies = $extractionStrategies ?? new DefaultExtractionStrategies(); |
|
33 | |||
34 | 13 | $this->object = $object; |
|
35 | |||
36 | 13 | $this->properties = $properties ?? new Collection([]); |
|
37 | 13 | } |
|
38 | |||
39 | /** |
||
40 | * Add a property that will be injected |
||
41 | * |
||
42 | * @param string $name |
||
43 | * @param mixed $value |
||
44 | * |
||
45 | * @return self |
||
46 | */ |
||
47 | 4 | public function withProperty(string $name, $value) |
|
48 | { |
||
49 | 4 | return new self( |
|
50 | 4 | $this->object, |
|
51 | 4 | $this->properties->set($name, $value), |
|
52 | 4 | $this->injectionStrategies, |
|
53 | 4 | $this->extractionStrategies |
|
54 | ); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Add a set of properties that need to be injected |
||
59 | * |
||
60 | * @param array $properties |
||
61 | * |
||
62 | * @return self |
||
63 | */ |
||
64 | 1 | public function withProperties(array $properties): self |
|
65 | { |
||
66 | 1 | return new self( |
|
67 | 1 | $this->object, |
|
68 | 1 | $this->properties->merge(new Collection($properties)), |
|
69 | 1 | $this->injectionStrategies, |
|
70 | 1 | $this->extractionStrategies |
|
71 | ); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Return the collection of properties that will be injected in the object |
||
76 | * |
||
77 | * @return CollectionInterface |
||
78 | */ |
||
79 | 2 | public function getProperties(): CollectionInterface |
|
83 | |||
84 | /** |
||
85 | * Return the list of injection strategies used |
||
86 | * |
||
87 | * @return InjectionStrategies |
||
88 | */ |
||
89 | 1 | public function getInjectionStrategies(): InjectionStrategies |
|
93 | |||
94 | /** |
||
95 | * Return the list of extraction strategies used |
||
96 | * |
||
97 | * @return ExtractionStrategies |
||
98 | */ |
||
99 | 2 | public function getExtractionStrategies(): ExtractionStrategies |
|
103 | |||
104 | /** |
||
105 | * Return the object with the list of properties set on it |
||
106 | * |
||
107 | * @return object |
||
108 | */ |
||
109 | 7 | public function buildObject() |
|
117 | |||
118 | /** |
||
119 | * Extract the given list of properties |
||
120 | * |
||
121 | * @param array $properties |
||
122 | * |
||
123 | * @return CollectionInterface |
||
124 | */ |
||
125 | 3 | public function extract(array $properties): CollectionInterface |
|
135 | |||
136 | /** |
||
137 | * Inject the given key/value pair into the object |
||
138 | * |
||
139 | * @param string $key |
||
140 | * @param mixed $value |
||
141 | * |
||
142 | * @return void |
||
143 | */ |
||
144 | 5 | private function inject(string $key, $value) |
|
151 | |||
152 | /** |
||
153 | * Extract the given property out of the object |
||
154 | * |
||
155 | * @param string $property |
||
156 | * |
||
157 | * @return mixed |
||
158 | */ |
||
159 | 3 | private function extractProperty(string $property) |
|
166 | } |
||
167 |