1
|
|
|
<?php |
2
|
|
|
namespace MetaHydrator\Handler; |
3
|
|
|
|
4
|
|
|
use MetaHydrator\Exception\HydratingException; |
5
|
|
|
use MetaHydrator\Exception\ValidationException; |
6
|
|
|
use MetaHydrator\Exception\ParsingException; |
7
|
|
|
use MetaHydrator\Parser\ParserInterface; |
8
|
|
|
use MetaHydrator\Validator\ValidatorInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* An implementation of HydratingHandlerInterface, as a one to one value parsing/validation. |
12
|
|
|
* |
13
|
|
|
* Class SimpleHydratingHandler |
14
|
|
|
* @package MetaHhydrator\Handler |
15
|
|
|
*/ |
16
|
|
|
class SimpleHydratingHandler implements HydratingHandlerInterface |
17
|
|
|
{ |
18
|
|
|
/** @var string */ |
19
|
|
|
private $key; |
20
|
|
|
public function getKey() { return $this->key; } |
21
|
|
|
public function setKey(string $key) { $this->key = $key; } |
22
|
|
|
|
23
|
|
|
/** @var ParserInterface */ |
24
|
|
|
private $parser; |
25
|
|
|
public function getParser() { return $this->parser; } |
26
|
|
|
public function setParser(ParserInterface $parser) { $this->parser = $parser; } |
27
|
|
|
|
28
|
|
|
/** @var ValidatorInterface[] */ |
29
|
|
|
private $validators; |
30
|
|
|
public function getValidators() { return $this->validators; } |
31
|
|
|
public function setValidators(array $validators) { $this->validators = $validators; } |
32
|
|
|
public function addValidator(ValidatorInterface $validator) { $this->validators[] = $validator; } |
33
|
|
|
|
34
|
|
|
/** @var mixed */ |
35
|
|
|
private $defaultValue; |
36
|
|
|
public function getDefaultValue() { return $this->defaultValue; } |
37
|
|
|
public function setDefaultValue($defaultValue) { $this->defaultValue = $defaultValue; } |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* SimpleHydratingHandler constructor. |
41
|
|
|
* @param string $key |
42
|
|
|
* @param ParserInterface $parser |
43
|
|
|
* @param ValidatorInterface[] $validators |
44
|
|
|
* @param mixed $default |
|
|
|
|
45
|
|
|
*/ |
46
|
|
|
public function __construct(string $key, ParserInterface $parser, array $validators = [], $defaultValue = null) |
47
|
|
|
{ |
48
|
|
|
$this->key = $key; |
49
|
|
|
$this->parser = $parser; |
50
|
|
|
$this->validators = $validators; |
51
|
|
|
$this->defaultValue = $defaultValue; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $data |
56
|
|
|
* @param array $targetData |
57
|
|
|
* @param $object |
58
|
|
|
* |
59
|
|
|
* @throws HydratingException |
60
|
|
|
*/ |
61
|
|
|
public function handle(array $data, array &$targetData, $object = null) |
62
|
|
|
{ |
63
|
|
|
if (array_key_exists($this->key, $data)) { |
64
|
|
|
$rawValue = $data[$this->key]; |
65
|
|
|
} else if ($object === null) { |
66
|
|
|
$rawValue = $this->defaultValue; |
67
|
|
|
} else { |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
try { |
72
|
|
|
$parsedValue = $this->parser->parse($rawValue); |
73
|
|
|
} catch (ParsingException $exception) { |
74
|
|
|
throw new HydratingException([ $this->key => $exception->getInnerError() ]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->validate($parsedValue, $object); |
78
|
|
|
|
79
|
|
|
$targetData[$this->key] = $parsedValue; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param mixed $parsedValue |
84
|
|
|
* @param mixed $contextObject |
85
|
|
|
* |
86
|
|
|
* @throws HydratingException |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
private function validate($parsedValue, $contextObject = null) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
try { |
91
|
|
|
foreach ($this->validators as $validator) { |
92
|
|
|
$validator->validate($parsedValue, $contextObject); |
93
|
|
|
} |
94
|
|
|
} catch (ValidationException $exception) { |
95
|
|
|
throw new HydratingException([ $this->key => $exception->getInnerError() ]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.