1 | <?php |
||
32 | class Processor implements ModelProcessor, TypeProcessor |
||
33 | { |
||
34 | |||
35 | /** |
||
36 | * Properties |
||
37 | */ |
||
38 | |||
39 | /** |
||
40 | * An input transformer to pre-process the input data before hydration. |
||
41 | * |
||
42 | * @var Transformer |
||
43 | */ |
||
44 | private $input_transformer; |
||
45 | |||
46 | /** |
||
47 | * A factory for building hydrators for a given model. |
||
48 | * |
||
49 | * @var HydratorFactory|null |
||
50 | */ |
||
51 | private $hydrator_factory; |
||
52 | |||
53 | /** |
||
54 | * A factory for building builders for a given model. |
||
55 | * |
||
56 | * @var BuilderFactory|null |
||
57 | */ |
||
58 | private $builder_factory; |
||
59 | |||
60 | /** |
||
61 | * A configuration flag that denotes whether hydration should always be run |
||
62 | * after building a new model when processing specified types. |
||
63 | * |
||
64 | * @var bool |
||
65 | */ |
||
66 | private $always_hydrate_after_building = false; |
||
67 | |||
68 | |||
69 | /** |
||
70 | * Methods |
||
71 | */ |
||
72 | |||
73 | /** |
||
74 | * Constructor |
||
75 | * |
||
76 | * @param Transformer|null $input_transformer The input transformer. |
||
77 | * @param HydratorFactory|null $hydrator_factory A hydrator factory. |
||
78 | * @param BuilderFactory|null $builder_factory A builder factory. |
||
79 | * @param bool $always_hydrate_after_building A configuration flag that |
||
80 | * denotes whether hydration should always be run after building a new |
||
81 | * model when processing specified types. |
||
82 | */ |
||
83 | 45 | public function __construct( |
|
94 | |||
95 | /** |
||
96 | * Get the input transformer. |
||
97 | * |
||
98 | * @return Transformer The input transformer. |
||
99 | */ |
||
100 | 3 | public function getInputTransformer(): Transformer |
|
104 | |||
105 | /** |
||
106 | * Set the input transformer. |
||
107 | * |
||
108 | * @param Transformer $input_transformer The input transformer. |
||
109 | * @return $this This instance. |
||
110 | */ |
||
111 | 3 | public function setInputTransformer(Transformer $input_transformer): self |
|
117 | |||
118 | /** |
||
119 | * Get the hydrator factory. |
||
120 | * |
||
121 | * @return HydratorFactory|null The hydrator factory. |
||
122 | */ |
||
123 | 3 | public function getHydratorFactory() |
|
127 | |||
128 | /** |
||
129 | * Set the hydrator factory. |
||
130 | * |
||
131 | * @param HydratorFactory|null $hydrator_factory The hydrator factory. |
||
132 | * @return $this This instance. |
||
133 | */ |
||
134 | 9 | public function setHydratorFactory(HydratorFactory $hydrator_factory = null): self |
|
140 | |||
141 | /** |
||
142 | * Get the builder factory. |
||
143 | * |
||
144 | * @return BuilderFactory|null The builder factory. |
||
145 | */ |
||
146 | 3 | public function getBuilderFactory() |
|
150 | |||
151 | /** |
||
152 | * Set the builder factory. |
||
153 | * |
||
154 | * @param BuilderFactory|null $builder_factory The builder factory. |
||
155 | * @return $this This instance. |
||
156 | */ |
||
157 | 6 | public function setBuilderFactory(BuilderFactory $builder_factory = null): self |
|
163 | |||
164 | /** |
||
165 | * Get the value of the configuration flag that denotes whether hydration |
||
166 | * should always be run after building a new model when processing |
||
167 | * specified types. |
||
168 | * |
||
169 | * @return bool The value of the flag. |
||
170 | */ |
||
171 | 3 | public function getAlwaysHydrateAfterBuilding(): bool |
|
175 | |||
176 | /** |
||
177 | * Set the value of the configuration flag that denotes whether hydration |
||
178 | * should always be run after building a new model when processing |
||
179 | * specified types. |
||
180 | * |
||
181 | * @param bool $always_hydrate_after_building Whether or not to always |
||
182 | * hydrate after building a new model when processing types. |
||
183 | * @return $this This instance. |
||
184 | */ |
||
185 | 9 | public function setAlwaysHydrateAfterBuilding(bool $always_hydrate_after_building): self |
|
191 | |||
192 | /** |
||
193 | * {@inheritdoc} |
||
194 | * |
||
195 | * If a hydrator isn't provided, an attempt will be made to automatically |
||
196 | * resolve and build an appropriate hydrator from the provided factory. |
||
197 | * |
||
198 | * @param mixed $input_data The input data. |
||
199 | * @param mixed $model The model to hydrate. |
||
200 | * @param Hydrator|null $hydrator The hydrator to use in the process. |
||
201 | * @param Map|null $context An optional generic key-value map, for providing |
||
202 | * contextual values during the process. |
||
203 | * @return mixed The hydrated model. |
||
204 | */ |
||
205 | 12 | public function processForModel($input_data, $model, Hydrator $hydrator = null, Map $context = null) |
|
211 | |||
212 | /** |
||
213 | * {@inheritdoc} |
||
214 | * |
||
215 | * If a builder isn't provided, an attempt will be made to automatically |
||
216 | * resolve and build an appropriate builder from the provided factory. |
||
217 | * |
||
218 | * If a hydrator is provided, it will be used to hydrate the provided type |
||
219 | * after building via the builder. |
||
220 | * |
||
221 | * If a hydrator isn't provided, but the "always_hydrate_after_building" |
||
222 | * property is set to true, an attempt to hydrate the type will be made |
||
223 | * after building via the builder, and the hydrator will be automatically |
||
224 | * resolved from the provided factory. |
||
225 | * |
||
226 | * @param mixed $input_data The input data. |
||
227 | * @param string $type The type to build. |
||
228 | * @param Builder|null $builder The builder to use in the process. |
||
229 | * @param Hydrator|null $hydrator An optional hydrator to use in the |
||
230 | * process, after the type is built, to aid in the full hydration of the |
||
231 | * resulting model. |
||
232 | * @param Map|null $context An optional generic key-value map, for providing |
||
233 | * contextual values during the process. |
||
234 | * @return mixed The built model. |
||
235 | */ |
||
236 | 21 | public function processForType( |
|
253 | |||
254 | /** |
||
255 | * Transform the input data. |
||
256 | * |
||
257 | * @param mixed $input_data The input data. |
||
258 | * @return mixed The resulting transformed data. |
||
259 | */ |
||
260 | 33 | protected function transformInput($input_data) |
|
264 | |||
265 | /** |
||
266 | * Hydrate a model from incoming data. |
||
267 | * |
||
268 | * If a hydrator isn't provided, an attempt will be made to automatically |
||
269 | * resolve and build an appropriate hydrator from the provided factory. |
||
270 | * |
||
271 | * @param mixed $input_data The input data. |
||
272 | * @param mixed $model The model to hydrate. |
||
273 | * @param Hydrator|null $hydrator The hydrator to use. |
||
274 | * @param Map|null $context An optional generic key-value map, for providing |
||
275 | * contextual values during the process. |
||
276 | * @return mixed The hydrated model. |
||
277 | */ |
||
278 | 21 | protected function hydrateModel($input_data, $model, Hydrator $hydrator = null, Map $context = null) |
|
290 | |||
291 | /** |
||
292 | * Build a model from incoming data. |
||
293 | * |
||
294 | * If a builder isn't provided, an attempt will be made to automatically |
||
295 | * resolve and build an appropriate builder from the provided factory. |
||
296 | * |
||
297 | * @param mixed $input_data The input data. |
||
298 | * @param string $type The type to build. |
||
299 | * @param Builder|null $builder The builder to use. |
||
300 | * @param Map|null $context An optional generic key-value map, for providing |
||
301 | * contextual values during the process. |
||
302 | * @return mixed The built model. |
||
303 | */ |
||
304 | 21 | protected function buildModel($input_data, string $type, Builder $builder = null, Map $context = null) |
|
316 | |||
317 | /** |
||
318 | * Get a Hydrator for a given model. |
||
319 | * |
||
320 | * @param mixed $model The model to get a hydrator for. |
||
321 | * @throws UnresolvableHydratorException If a hydrator can't be resolved for |
||
322 | * the given model. |
||
323 | * @return Hydrator The resulting hydrator. |
||
324 | */ |
||
325 | 12 | protected function getHydratorForModel($model): Hydrator |
|
333 | |||
334 | /** |
||
335 | * Get a Builder for a given model. |
||
336 | * |
||
337 | * @param string $type The type to get a builder for. |
||
338 | * @throws UnresolvableBuilderException If a builder can't be resolved for |
||
339 | * the given model. |
||
340 | * @return Builder The resulting builder. |
||
341 | */ |
||
342 | 6 | protected function getBuilderForType(string $type): Builder |
|
350 | } |
||
351 |