1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Incoming |
4
|
|
|
* |
5
|
|
|
* @author Trevor Suarez (Rican7) |
6
|
|
|
* @copyright (c) Trevor Suarez |
7
|
|
|
* @link https://github.com/Rican7/incoming |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Incoming\Hydrator; |
14
|
|
|
|
15
|
|
|
use Incoming\Structure\Map; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* An abstract, context enabled extension of the `AbstractDelegateBuilderHydrator`. |
19
|
|
|
* |
20
|
|
|
* @see AbstractDelegateBuilderHydrator |
21
|
|
|
* @see AbstractDelegateContextualBuilder |
22
|
|
|
* @see AbstractDelegateContextualHydrator |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractDelegateContextualBuilderHydrator extends AbstractDelegateBuilderHydrator implements |
25
|
|
|
ContextualBuilder, |
26
|
|
|
ContextualHydrator |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Properties |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Whether or not to provide a fallback empty context, when a `null` context |
35
|
|
|
* is otherwise provided, to make processes simpler by not having to rely on |
36
|
|
|
* null checks of the actual parameter before usage. |
37
|
|
|
* |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
private $provide_fallback_context = false; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Methods |
45
|
|
|
*/ |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Constructor |
49
|
|
|
* |
50
|
|
|
* @param bool $provide_fallback_context Whether or not to provide a |
51
|
|
|
* fallback empty context, when a `null` context is otherwise provided, to |
52
|
|
|
* make processes simpler by not having to rely on null checks of the |
53
|
|
|
* actual parameter before usage. |
54
|
|
|
*/ |
55
|
12 |
|
protected function __construct(bool $provide_fallback_context = false) |
56
|
|
|
{ |
57
|
12 |
|
$this->provide_fallback_context = $provide_fallback_context; |
58
|
12 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
* |
63
|
|
|
* @param mixed $incoming The input data. |
64
|
|
|
* @param Map|null $context An optional generic key-value map, for providing |
65
|
|
|
* contextual values during the build process. |
66
|
|
|
* @return mixed The built model. |
67
|
|
|
*/ |
68
|
6 |
|
public function build($incoming, Map $context = null) |
69
|
|
|
{ |
70
|
6 |
|
$callable = $this->getDelegateBuilder(); |
71
|
|
|
|
72
|
6 |
|
if (null === $context && $this->provide_fallback_context) { |
73
|
|
|
// Provide a non-null context so null checks aren't later required |
74
|
3 |
|
$context = new Map(); |
75
|
|
|
} |
76
|
|
|
|
77
|
6 |
|
return $callable($incoming, $context); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
* |
83
|
|
|
* @param mixed $incoming The input data. |
84
|
|
|
* @param mixed $model The model to hydrate. |
85
|
|
|
* @param Map|null $context An optional generic key-value map, for providing |
86
|
|
|
* contextual values during the hydrate process. |
87
|
|
|
* @return mixed The hydrated model. |
88
|
|
|
*/ |
89
|
6 |
|
public function hydrate($incoming, $model, Map $context = null) |
90
|
|
|
{ |
91
|
6 |
|
$callable = $this->getDelegateHydrator(); |
92
|
|
|
|
93
|
6 |
|
if (null === $context && $this->provide_fallback_context) { |
94
|
|
|
// Provide a non-null context so null checks aren't later required |
95
|
3 |
|
$context = new Map(); |
96
|
|
|
} |
97
|
|
|
|
98
|
6 |
|
return $callable($incoming, $model, $context); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|