|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mcustiel\SimpleRequest\Strategies\Properties; |
|
3
|
|
|
|
|
4
|
|
|
use Mcustiel\SimpleRequest\Strategies\Properties\PropertyParser; |
|
5
|
|
|
use Mcustiel\SimpleRequest\Exception\InvalidValueException; |
|
6
|
|
|
|
|
7
|
|
|
class SimplePropertyParser implements PropertyParser |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* |
|
11
|
|
|
* @var \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface[] |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $validators; |
|
14
|
|
|
/** |
|
15
|
|
|
* |
|
16
|
|
|
* @var \Mcustiel\SimpleRequest\Interfaces\FilterInterface[] |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $filters; |
|
19
|
|
|
/** |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $name; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct($name) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->name = $name; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function setFilters(array $filters) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->filters = $filters; |
|
33
|
|
|
return $this; |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function setValidators(array $validators) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->validators = $validators; |
|
39
|
|
|
return $this; |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getName() |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->name; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Filters and validates a value. And return the filtered value. |
|
49
|
|
|
* It throws an exception if the value is not valid. |
|
50
|
|
|
* |
|
51
|
|
|
* @param mixed $value |
|
|
|
|
|
|
52
|
|
|
* @return mixed |
|
53
|
|
|
* |
|
54
|
|
|
* @throws \Mcustiel\SimpleRequest\Exception\InvalidValueException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function parse($propertyValue) |
|
57
|
|
|
{ |
|
58
|
|
|
$return = $this->cloneValue($propertyValue); |
|
59
|
|
|
$return = $this->runFilters($return); |
|
60
|
|
|
$this->validate($return); |
|
61
|
|
|
|
|
62
|
|
|
return $return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns a copy of the received value. |
|
67
|
|
|
* |
|
68
|
|
|
* @param mixed $value |
|
69
|
|
|
* @return mixed |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function cloneValue($value) |
|
72
|
|
|
{ |
|
73
|
|
|
if (is_object($value)) { |
|
74
|
|
|
return clone $value; |
|
75
|
|
|
} |
|
76
|
|
|
return $value; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Checks the value against all validators. |
|
81
|
|
|
* |
|
82
|
|
|
* @param mixed $value |
|
83
|
|
|
* |
|
84
|
|
|
* @throws \Mcustiel\SimpleRequest\Exception\InvalidValueException |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function validate($value) |
|
87
|
|
|
{ |
|
88
|
|
|
foreach ($this->validators as $validator) { |
|
89
|
|
|
if (!$validator->validate($value)) { |
|
90
|
|
|
throw new InvalidValueException( |
|
91
|
|
|
"Field {$this->name}, was set with invalid value: " . var_export($value, true) |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Run all the filters on the value. |
|
99
|
|
|
* |
|
100
|
|
|
* @param mixed $value |
|
101
|
|
|
* |
|
102
|
|
|
* @return mixed |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function runFilters($value) |
|
105
|
|
|
{ |
|
106
|
|
|
$return = $value; |
|
107
|
|
|
if ($return !== null) { |
|
108
|
|
|
foreach ($this->filters as $filter) { |
|
109
|
|
|
$return = $filter->filter($return); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $return; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.