|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stratadox\Hydration\Hydrator; |
|
6
|
|
|
|
|
7
|
|
|
use Stratadox\Hydration\Hydrates; |
|
8
|
|
|
use Stratadox\Hydration\UnmappableInput; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Delegates hydration, selecting a hydrator based on an input value. |
|
12
|
|
|
* |
|
13
|
|
|
* @package Stratadox\Hydrate |
|
14
|
|
|
* @author Stratadox |
|
15
|
|
|
*/ |
|
16
|
|
|
final class OneOfTheseHydrators implements Hydrates |
|
17
|
|
|
{ |
|
18
|
|
|
private $decisionKey; |
|
19
|
|
|
private $hydratorMap; |
|
20
|
|
|
private $current; |
|
21
|
|
|
|
|
22
|
|
|
private function __construct(string $decisionKey, array $hydratorMap) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->decisionKey = $decisionKey; |
|
25
|
|
|
$this->hydratorMap = $hydratorMap; |
|
26
|
|
|
foreach ($hydratorMap as $key => $instance) { |
|
27
|
|
|
$this->mustBeAString($key); |
|
28
|
|
|
$this->mustBeAHydrator($instance); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public static function decideBasedOnThe(string $key, array $map) : Hydrates |
|
33
|
|
|
{ |
|
34
|
|
|
return new OneOfTheseHydrators($key, $map); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @throws UnmappableInput |
|
39
|
|
|
* @inheritdoc |
|
40
|
|
|
*/ |
|
41
|
|
|
public function fromArray(array $input) |
|
42
|
|
|
{ |
|
43
|
|
|
try { |
|
44
|
|
|
$this->current = $this->hydratorBasedOn($this->keyFromThe($input)); |
|
45
|
|
|
return $this->hydrateAnInstanceUsing($this->current, $input); |
|
46
|
|
|
} finally { |
|
47
|
|
|
$this->current = null; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function currentInstance() |
|
52
|
|
|
{ |
|
53
|
|
|
if (!$this->current instanceof Hydrates) { |
|
54
|
|
|
return null; |
|
55
|
|
|
} |
|
56
|
|
|
return $this->current->currentInstance(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function hydrateAnInstanceUsing(Hydrates $hydrator, array $input) |
|
60
|
|
|
{ |
|
61
|
|
|
return $hydrator->fromArray($input); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** @throws UnmappableInput */ |
|
65
|
|
|
private function hydratorBasedOn(string $key) : Hydrates |
|
66
|
|
|
{ |
|
67
|
|
|
if (!isset($this->hydratorMap[$key])) { |
|
68
|
|
|
throw CannotDecideOnAHydrator::withThis($key); |
|
69
|
|
|
} |
|
70
|
|
|
return $this->hydratorMap[$key]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** @throws UnmappableInput */ |
|
74
|
|
|
private function keyFromThe(array $input) : string |
|
75
|
|
|
{ |
|
76
|
|
|
if (!isset($input[$this->decisionKey])) { |
|
77
|
|
|
throw CannotDecideOnAHydrator::without($this->decisionKey); |
|
78
|
|
|
} |
|
79
|
|
|
return $input[$this->decisionKey]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function mustBeAString(string $key) : void {} |
|
|
|
|
|
|
83
|
|
|
private function mustBeAHydrator(Hydrates $instance) : void {} |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.