|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mcustiel\SimpleRequest\Strategies\Properties; |
|
3
|
|
|
|
|
4
|
|
|
use Mcustiel\SimpleRequest\Exception\InvalidValueException; |
|
5
|
|
|
|
|
6
|
|
|
class SimplePropertyParser implements PropertyParser |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* |
|
10
|
|
|
* @var \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface[] |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $validators = []; |
|
13
|
|
|
/** |
|
14
|
|
|
* |
|
15
|
|
|
* @var \Mcustiel\SimpleRequest\Interfaces\FilterInterface[] |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $filters = []; |
|
18
|
|
|
/** |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $name; |
|
23
|
|
|
|
|
24
|
97 |
|
public function __construct($name) |
|
25
|
1 |
|
{ |
|
26
|
97 |
|
$this->name = $name; |
|
27
|
97 |
|
} |
|
28
|
|
|
|
|
29
|
95 |
|
public function setFilters(array $filters) |
|
30
|
|
|
{ |
|
31
|
95 |
|
$this->filters = $filters; |
|
32
|
95 |
|
return $this; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
95 |
|
public function setValidators(array $validators) |
|
36
|
|
|
{ |
|
37
|
95 |
|
$this->validators = $validators; |
|
38
|
95 |
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
97 |
|
public function getName() |
|
42
|
|
|
{ |
|
43
|
97 |
|
return $this->name; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Filters and validates a value. And return the filtered value. |
|
48
|
|
|
* It throws an exception if the value is not valid. |
|
49
|
|
|
* |
|
50
|
|
|
* @param mixed $value |
|
|
|
|
|
|
51
|
|
|
* |
|
52
|
|
|
* @return mixed |
|
53
|
|
|
* |
|
54
|
|
|
* @throws \Mcustiel\SimpleRequest\Exception\InvalidValueException |
|
55
|
|
|
*/ |
|
56
|
96 |
|
public function parse($propertyValue) |
|
57
|
|
|
{ |
|
58
|
96 |
|
$return = $this->cloneValue($propertyValue); |
|
59
|
96 |
|
$return = $this->runFilters($return); |
|
60
|
96 |
|
$this->validate($return); |
|
61
|
|
|
|
|
62
|
94 |
|
return $return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns a copy of the received value. |
|
67
|
|
|
* |
|
68
|
|
|
* @param mixed $value |
|
69
|
|
|
* |
|
70
|
|
|
* @return mixed |
|
71
|
|
|
*/ |
|
72
|
98 |
|
protected function cloneValue($value) |
|
73
|
|
|
{ |
|
74
|
98 |
|
if (is_object($value)) { |
|
75
|
77 |
|
return clone $value; |
|
76
|
|
|
} |
|
77
|
98 |
|
return $value; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Checks the value against all validators. |
|
82
|
|
|
* |
|
83
|
|
|
* @param mixed $value |
|
84
|
|
|
* |
|
85
|
|
|
* @throws \Mcustiel\SimpleRequest\Exception\InvalidValueException |
|
86
|
|
|
*/ |
|
87
|
97 |
|
protected function validate($value) |
|
88
|
|
|
{ |
|
89
|
97 |
|
foreach ($this->validators as $validator) { |
|
90
|
94 |
|
if (!$validator->validate($value)) { |
|
91
|
57 |
|
throw new InvalidValueException( |
|
92
|
57 |
|
"Field {$this->name}, was set with invalid value: " . var_export($value, true) |
|
93
|
57 |
|
); |
|
94
|
|
|
} |
|
95
|
95 |
|
} |
|
96
|
95 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Run all the filters on the value. |
|
100
|
|
|
* |
|
101
|
|
|
* @param mixed $value |
|
102
|
|
|
* |
|
103
|
|
|
* @return mixed |
|
104
|
|
|
*/ |
|
105
|
97 |
|
protected function runFilters($value) |
|
106
|
|
|
{ |
|
107
|
97 |
|
$return = $value; |
|
108
|
97 |
|
foreach ($this->filters as $filter) { |
|
109
|
14 |
|
$return = $filter->filter($return); |
|
110
|
97 |
|
} |
|
111
|
|
|
|
|
112
|
97 |
|
return $return; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.