|
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\Hydrator\Exception\InvalidDelegateException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* An abstract hydrator that allows for the hydration to be delegated to |
|
19
|
|
|
* another callable. By default, a named method is attempted to be found, but |
|
20
|
|
|
* any callable could be returned through overrides. |
|
21
|
|
|
* |
|
22
|
|
|
* This enables a lot of interesting uses, most notably this allows hydrators |
|
23
|
|
|
* to be created that have strongly type-hinted hydration arguments while still |
|
24
|
|
|
* perfectly satisfying the `Hydrator`. Essentially this allows the bypassing |
|
25
|
|
|
* of the type variance rules enforced by PHP in a way that provides a |
|
26
|
|
|
* generics-like definition. Ultimately, if/when PHP gets generics this will no |
|
27
|
|
|
* longer be necessary, as one could simply implement a hydrator using typed |
|
28
|
|
|
* arguments like: `Hydrator<IncomingDataType, ModelType>`. |
|
29
|
|
|
* |
|
30
|
|
|
* @link http://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science) |
|
31
|
|
|
* @link http://en.wikipedia.org/wiki/Generic_programming |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract class AbstractDelegateHydrator implements Hydrator |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Constants |
|
38
|
|
|
*/ |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The name of the default delegate method. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
const DEFAULT_DELEGATE_METHOD_NAME = 'hydrateModel'; |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Methods |
|
50
|
|
|
*/ |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
* |
|
55
|
|
|
* @param mixed $incoming The input data. |
|
56
|
|
|
* @param mixed $model The model to hydrate. |
|
57
|
|
|
* @return mixed The hydrated model. |
|
58
|
|
|
*/ |
|
59
|
9 |
|
public function hydrate($incoming, $model) |
|
60
|
|
|
{ |
|
61
|
9 |
|
$callable = $this->getDelegate(); |
|
62
|
|
|
|
|
63
|
6 |
|
return $callable($incoming, $model); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get the delegate hydration callable. |
|
68
|
|
|
* |
|
69
|
|
|
* Override this method if a custom delegate is desired. |
|
70
|
|
|
* |
|
71
|
|
|
* @return callable The delegate hydrator callable. |
|
72
|
|
|
* @throws InvalidDelegateException If the delegate isn't callable. |
|
73
|
|
|
*/ |
|
74
|
15 |
|
protected function getDelegate(): callable |
|
75
|
|
|
{ |
|
76
|
15 |
|
$delegate = [$this, static::DEFAULT_DELEGATE_METHOD_NAME]; |
|
77
|
|
|
|
|
78
|
15 |
|
if (!is_callable($delegate, false, $callable_name)) { |
|
79
|
3 |
|
throw InvalidDelegateException::forNonCallable($callable_name); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
12 |
|
return $delegate; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* The delegate hydrate method. |
|
87
|
|
|
* |
|
88
|
|
|
* This doc-block and commented out abstract method is provided here to show |
|
89
|
|
|
* what the delegate method signature WOULD be if PHP allowed the proper |
|
90
|
|
|
* typing support to enable a generic definition in this manner. |
|
91
|
|
|
* |
|
92
|
|
|
* See the class description for more info. |
|
93
|
|
|
* |
|
94
|
|
|
* @param IncomingDataType $incoming The input data. |
|
95
|
|
|
* @param ModelType $model The model to hydrate. |
|
96
|
|
|
* @return ModelType The hydrated model. |
|
97
|
|
|
*/ |
|
98
|
|
|
// abstract protected function hydrateModel(IncomingDataType $incoming, ModelType $model): ModelType; |
|
99
|
|
|
} |
|
100
|
|
|
|