-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidate.php
More file actions
executable file
·226 lines (205 loc) · 7.01 KB
/
Validate.php
File metadata and controls
executable file
·226 lines (205 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
namespace MaplePHP\Form;
use MaplePHP\Form\Interfaces\FieldInterface;
use MaplePHP\Validate\Inp;
use MaplePHP\DTO\Format\Local;
class Validate
{
public const WHITELIST_INC_ARR_FIELD = ["list"];
public const TO_REQURED_FLAG = ["length", "hasValue", "required"];
private $validArr;
private $fields;
private $post;
private $request = array();
private $files = array();
private $local;
private $value;
private $length = 0;
private $validate;
/**
* Built to auto validate FieldInterface fields
* @param FieldInterface $fields
* @param array $post
*/
public function __construct(FieldInterface $fields, array $post)
{
$this->fields = $fields;
$this->post = $post;
}
/**
* Get filtered RAW filtered POST data
* @return array
*/
public function getRequest(): array
{
return $this->request;
}
/**
* Get filtered RAW filtered FILES data
* @return array
*/
public function getFiles(): array
{
return $this->files;
}
/**
* Set local to return message in right language
* @param Local $local
*/
public function setLocal(Local $local): void
{
$this->local = $local;
}
/**
* Add an error item manually
* @param string $name The input/field name
* @param string $type Type of error (e.g. required, min, max...)
* @param string $message Nice error message
*/
public function add(string $name, string $type, ?string $message = null)
{
$this->validArr[$name] = ["type" => $type, "message" => $message];
return $this;
}
/**
* Validate all fields in form
* @return array|NULL (array=error and NULL=Success)
*/
public function execute(): ?array
{
$this->fields->setValues($this->post);
$this->fields->build();
$postArr = array();
$arr = $this->fields->getValidateData();
foreach ($arr as $name => $arr) {
$field = $this->fields->{$arr['type']}();
$input = $field->name($name)->fieldType($arr['type'])->value(false)->default($arr['default'] ?? null);
$nameKey = $input->getName();
$exp = explode(",", $name);
// Build request array from exploded protocol and return only passed values
$value = $this->buildPostArr($exp, $this->post, $postArr, $input);
if (!is_array($value)) {
if (isset($arr['validate'])) {
$this->value = htmlspecialchars((string)$value);
$this->length = strlen($this->value);
$this->validate = Inp::value($this->value);
if ($error = $this->isInvalid($arr['validate'])) {
$this->validArr[$nameKey] = $error;
}
}
}
}
$this->request = $postArr;
return $this->validArr;
}
/**
* Validate field
* @param array $arr [ Method => [Arg1, Arg2] ]
* @return boolean|array
*/
protected function isInvalid($arr): bool|array
{
if (is_array($arr)) {
foreach ($arr as $method => $args) {
$valFilledIn = false;
if (strpos($method, "!") !== false) {
$valFilledIn = true;
$method = substr($method, 1);
}
if (method_exists($this->validate, $method)) {
if ($this->validateWithMethod($method, $args, $valFilledIn)) {
if (in_array($method, self::TO_REQURED_FLAG)) {
if ($this->length === 0) {
$method = "required";
}
}
return $this->buildMessage($method, $args, $this->message($method, $args));
}
} else {
$message = "Validation method ({$method}) does not exist";
return $this->buildMessage($method, $args, $message);
}
}
}
return false;
}
/**
* Validate with the validation library
* @param string $method
* @param array $args
* @param bool $valFilledIn
* @return bool
*/
private function validateWithMethod(string $method, ?array $args, bool $valFilledIn): bool
{
if (!is_array($args)) {
$args = array();
}
$object = call_user_func_array([$this->validate, $method], $args);
return (($valFilledIn && $this->length > 0 && !$object) || (!$valFilledIn && !$object));
}
/**
* Build response structure
* @param string $method
* @param array $args
* @param string $message
* @return array
*/
private function buildMessage(string $method, ?array $args, string $message): array
{
return ["type" => $method, "args" => $args, "message" => $message];
}
/**
* Will be used to extract a nice message from key
* @param string $key error key (e.g. required, length, max, min...)
* @param array $args sprint push possible values
* @return string
*/
protected function message(string $key, array $args = array())
{
if (!is_null($this->local)) {
return $this->local->get($key, $key, $args);
}
return $key;
}
/**
* Will build a validateable post array from extisting fields
* @param array $exp
* @param array &$arr
* @param mixed &$new
* @return mixed (field value)
*/
protected function buildPostArr(array $exp, array &$arr, mixed &$new, object $field)
{
$firstKey = array_shift($exp);
if (isset($arr[$firstKey])) {
if (count($exp) > 0) {
//if(isset($new[$firstKey])) $new[$firstKey] = [];
return $this->buildPostArr($exp, $arr[$firstKey], $new[$firstKey], $field);
} else {
// Pass _FILE value to array
if (isset($arr[$firstKey]['tmp_name'])) {
$this->files[$firstKey] = $arr[$firstKey];
return $arr[$firstKey];
} elseif (!is_array($arr[$firstKey])) {
$new[$firstKey] = $arr[$firstKey];
return $arr[$firstKey];
} else {
// Pass on incremental values from field type
if (
isset($arr[$firstKey][0]) && is_string($arr[$firstKey][0]) &&
in_array($field->getFieldType(), static::WHITELIST_INC_ARR_FIELD)
) {
$new[$firstKey] = $arr[$firstKey];
return $arr[$firstKey];
} else {
$new[$firstKey] = $field->getDefault();
}
}
return null;
}
} else {
$new[$firstKey] = $field->getDefault();
}
}
}