1
|
|
|
<?php |
2
|
|
|
namespace MetaHydrator\Handler; |
3
|
|
|
|
4
|
|
|
use MetaHydrator\Exception\HydratingException; |
5
|
|
|
use MetaHydrator\MetaHydrator; |
6
|
|
|
use MetaHydrator\Reflection\Getter; |
7
|
|
|
use MetaHydrator\Reflection\GetterInterface; |
8
|
|
|
use MetaHydrator\Validator\ValidatorInterface; |
9
|
|
|
use Mouf\Hydrator\Hydrator; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* An implementation of HydratingHandlerInterface aiming to manage partial edition of sub-objects |
13
|
|
|
* |
14
|
|
|
* Class SubHydratingHandler |
15
|
|
|
* @package MetaHydrator\Handler |
16
|
|
|
*/ |
17
|
|
|
class SubHydratingHandler extends MetaHydrator implements HydratingHandlerInterface |
18
|
|
|
{ |
19
|
|
|
/** @var string */ |
20
|
|
|
protected $key; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $className; |
24
|
|
|
|
25
|
|
|
/** @var mixed */ |
26
|
|
|
protected $errorMessage; |
27
|
|
|
|
28
|
|
|
/** @var GetterInterface */ |
29
|
|
|
protected $getter; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* SubHydratingHandler constructor. |
33
|
|
|
* @param string $key |
34
|
|
|
* @param string $className |
35
|
|
|
* @param HydratingHandlerInterface[] $handlers |
36
|
|
|
* @param ValidatorInterface[] $validators |
37
|
|
|
* @param string $errorMessage |
38
|
|
|
* @param Hydrator $simpleHydrator |
39
|
|
|
* @param GetterInterface $getter |
40
|
|
|
*/ |
41
|
|
|
public function __construct(string $key, string $className, array $handlers, array $validators = [], string $errorMessage = null, Hydrator $simpleHydrator = null, GetterInterface $getter = null) |
42
|
|
|
{ |
43
|
|
|
parent::__construct($handlers, $validators, $simpleHydrator); |
44
|
|
|
$this->key = $key; |
45
|
|
|
$this->className = $className; |
46
|
|
|
$this->errorMessage = $errorMessage; |
47
|
|
|
$this->getter = $getter ?? $this->defaultGetter(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $data |
52
|
|
|
* @param array $targetData |
53
|
|
|
* @param $object |
54
|
|
|
* |
55
|
|
|
* @throws HydratingException |
56
|
|
|
*/ |
57
|
|
|
public function handle(array $data, array &$targetData, $object = null) |
58
|
|
|
{ |
59
|
|
|
$subData = $this->getSubData($data, $object); |
60
|
|
|
if ($subData === null) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
try { |
64
|
|
|
$subObject = $this->getSubObject($object); |
65
|
|
|
if ($subObject !== null) { |
66
|
|
|
$this->hydrateObject($subData, $subObject); |
67
|
|
|
} else { |
68
|
|
|
$targetData[$this->key] = $this->hydrateNewObject($subData, $this->className); |
69
|
|
|
} |
70
|
|
|
} catch (HydratingException $exception) { |
71
|
|
|
throw new HydratingException([$this->key => $exception->getErrorsMap()]); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $object |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
protected function getSubObject($object) |
80
|
|
|
{ |
81
|
|
|
if ($object === null) { |
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
try { |
85
|
|
|
return $this->getter->get($object, $this->key); |
86
|
|
|
} catch (\ReflectionException $exception) { |
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $data |
93
|
|
|
* @param $object |
94
|
|
|
* @return array |
95
|
|
|
* @throws HydratingException |
96
|
|
|
*/ |
97
|
|
|
protected function getSubData($data, $object) |
98
|
|
|
{ |
99
|
|
|
if (array_key_exists($this->key, $data)) { |
100
|
|
|
$subData = $data[$this->key]; |
101
|
|
|
if (!is_array($subData)) { |
102
|
|
|
throw new HydratingException([$this->key => $this->errorMessage]); |
103
|
|
|
} else { |
104
|
|
|
return $subData; |
105
|
|
|
} |
106
|
|
|
} else if ($object === null) { |
107
|
|
|
return []; |
108
|
|
|
} else { |
109
|
|
|
return null; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return GetterInterface |
115
|
|
|
*/ |
116
|
|
|
private function defaultGetter() |
117
|
|
|
{ |
118
|
|
|
return new Getter(false); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|