1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FigTree\Validation; |
4
|
|
|
|
5
|
|
|
use FigTree\Validation\Contracts\{ |
6
|
|
|
FilterInterface, |
7
|
|
|
RuleFactoryInterface, |
8
|
|
|
RuleInterface |
9
|
|
|
}; |
10
|
|
|
use FigTree\Validation\Exceptions\InvalidRuleException; |
11
|
|
|
|
12
|
|
|
abstract class AbstractFilter implements FilterInterface |
13
|
|
|
{ |
14
|
|
|
protected RuleFactoryInterface $ruleFactory; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Set or remove a RuleFactory. |
18
|
|
|
* |
19
|
|
|
* @param \FigTree\Validation\Contracts\RuleFactoryInterface $ruleFactory |
20
|
|
|
* |
21
|
|
|
* @return $this |
22
|
|
|
*/ |
23
|
|
|
public function setRuleFactory(RuleFactoryInterface $ruleFactory): FilterInterface |
24
|
|
|
{ |
25
|
|
|
$this->ruleFactory = $ruleFactory; |
26
|
|
|
|
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Filter a single value against the Filter. |
32
|
|
|
* |
33
|
|
|
* @param string $field |
34
|
|
|
* @param mixed $value |
35
|
|
|
* |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
|
|
public function filterValue(string $field, mixed $value, $default = null): mixed |
39
|
|
|
{ |
40
|
|
|
$definition = $this->getDefinition($field); |
41
|
|
|
|
42
|
|
|
if (empty($definition)) { |
43
|
|
|
return $default; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$filter = $definition['filter']; |
47
|
|
|
|
48
|
|
|
if (empty($filter)) { |
49
|
|
|
throw new InvalidRuleException($this->getRule($field)); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return filter_var($value, $filter, $definition); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Filter a single input field against the Filter. |
57
|
|
|
* |
58
|
|
|
* @param integer $type |
59
|
|
|
* @param string $field |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public function filterInput(int $type, string $field, $default = null): mixed |
64
|
|
|
{ |
65
|
|
|
$definition = $this->getDefinition($field); |
66
|
|
|
|
67
|
|
|
if (empty($definition)) { |
68
|
|
|
return $default; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$filter = $definition['filter']; |
72
|
|
|
|
73
|
|
|
if (empty($filter)) { |
74
|
|
|
throw new InvalidRuleException($this->getRule($field)); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return filter_input($type, $field, $filter, $definition); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Filter an array of values against the Filter. |
82
|
|
|
* |
83
|
|
|
* @param array $data |
84
|
|
|
* @param boolean $addEmpty |
85
|
|
|
* |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
|
|
public function filterArray(array $data, bool $addEmpty = true): array |
89
|
|
|
{ |
90
|
|
|
$definitions = $this->getDefinitions(); |
91
|
|
|
|
92
|
|
|
$input = filter_var_array($data, $definitions, $addEmpty); |
93
|
|
|
|
94
|
|
|
if (!is_array($input)) { |
95
|
|
|
$input = ($addEmpty) |
96
|
|
|
? array_fill_keys(array_keys($definitions), null) |
97
|
|
|
: []; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $input; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Filter an array of input values against the Filter. |
105
|
|
|
* |
106
|
|
|
* @param integer $type |
107
|
|
|
* @param boolean $addEmpty |
108
|
|
|
* |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
public function filterInputArray(int $type, bool $addEmpty = true): array |
112
|
|
|
{ |
113
|
|
|
$definitions = $this->getDefinitions(); |
114
|
|
|
|
115
|
|
|
$input = filter_input_array($type, $definitions, $addEmpty); |
116
|
|
|
|
117
|
|
|
if (!is_array($input)) { |
118
|
|
|
$input = ($addEmpty) |
119
|
|
|
? array_fill_keys(array_keys($definitions), null) |
120
|
|
|
: []; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $input; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get a single Rule. |
128
|
|
|
* |
129
|
|
|
* @param string $field |
130
|
|
|
* |
131
|
|
|
* @return \FigTree\Validation\Contracts\RuleInterface|null |
132
|
|
|
*/ |
133
|
|
|
protected function getRule(string $field): ?RuleInterface |
134
|
|
|
{ |
135
|
|
|
$rules = $this->getRules(); |
136
|
|
|
|
137
|
|
|
return $rules[$field] ?? null; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get a single Rule as a native filter definition. |
142
|
|
|
* |
143
|
|
|
* @param string $field |
144
|
|
|
* |
145
|
|
|
* @return array|null |
146
|
|
|
*/ |
147
|
|
|
protected function getDefinition(string $field): ?array |
148
|
|
|
{ |
149
|
|
|
$rule = $this->getRule($field); |
150
|
|
|
|
151
|
|
|
if (!($rule instanceof RuleInterface)) { |
152
|
|
|
return null; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $rule->toArray(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the Rules as a native filter definition. |
160
|
|
|
* |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
|
protected function getDefinitions(): array |
164
|
|
|
{ |
165
|
|
|
return array_map( |
166
|
|
|
fn (RuleInterface $rule) => $rule->toArray(), |
167
|
|
|
$this->getRules() |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|