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