-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm.php
261 lines (226 loc) · 6.93 KB
/
Form.php
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* Form.php
* Created by Susan Buck
* https://github.com/susanBuck/foobooks0/blob/master/Form.php
*/
namespace DWA;
class Form
{
/**
* Properties
*/
private $request;
public $hasErrors = false;
/**
* Form constructor.
* @param $postOrGet
*/
public function __construct($postOrGet)
{
# Store form data (POST or GET) in a class property called $request
$this->request = $postOrGet;
}
/**
* Returns True if *either* GET or POST have been submitted.
* @return bool
*/
public function isSubmitted()
{
return $_SERVER['REQUEST_METHOD'] == 'POST' || !empty($_GET);
}
/**
* Get a value from the request, with the option of including a default
* if the value is not set.
* @param $name
* @param null $default
* @return null
*/
public function get($name, $default = null)
{
return $this->request[$name] ?? $default;
}
/**
* Determines if a value is present in the request
* @param $name
* @return bool
*/
public function has($name)
{
return isset($this->request[$name]) ? true : false;
}
/**
* Use in display files to pre-fill the values of fields if those values are in the request.
* Second optional parameter lets you set a default value if value does not exist
* @param $field
* @param string $default
* @param bool $sanitize
* @return array|string
*/
public function prefill($field, $default = '', $sanitize = true)
{
if (isset($this->request[$field])) {
if ($sanitize) {
return $this->sanitize($this->request[$field]);
} else {
return $this->request[$field];
}
}
return $default;
}
/**
* Strips HTML characters; works with arrays or scalar values.
* @param null $mixed
* @return array|string
*/
public function sanitize($mixed = null)
{
if (!is_array($mixed)) {
return $this->convertHtmlEntities($mixed);
}
function arrayMapRecursive($callback, $array)
{
$func = function ($item) use (&$func, &$callback) {
return is_array($item) ? array_map($func, $item) : call_user_func($callback, $item);
};
return array_map($func, $array);
}
return arrayMapRecursive('convertHtmlEntities', $mixed);
}
/**
* Helper function used by sanitize
* @param $mixed
* @return string
*/
private function convertHtmlEntities($mixed)
{
return htmlentities($mixed, ENT_QUOTES, "UTF-8");
}
/**
* Given an array of fields => validation rules
* Will loop through each field's rules
* Returns an array of error messages
* Stops after the first error for a given field
* Available rules: alphaNumeric, alpha, numeric, required, email, min:x, max:x
* @param $fieldsToValidate
* @return array
*/
public function validate($fieldsToValidate)
{
$errors = [];
foreach ($fieldsToValidate as $fieldName => $rules) {
# Each rule is separated by a |
$rules = explode('|', $rules);
foreach ($rules as $rule) {
# Get the value for this field from the request
$value = $this->get($fieldName);
# Handle any parameters with the rule, e.g. max:99
$parameter = null;
if (strstr($rule, ':')) {
list($rule, $parameter) = explode(':', $rule);
}
# Run the validation test with the given rule
$test = $this->$rule($value, $parameter);
# Test failed
if (!$test) {
$errors[] = 'The field ' . $fieldName . $this->getErrorMessage($rule, $parameter);
# Only indicate one error per field
break;
}
}
}
# Set public property hasErrors as Boolean
$this->hasErrors = !empty($errors);
return $errors;
}
/**
* Given a String rule like 'alphaNumeric' or 'required'
* It'll return a String message appropriate for that rule
* Default message is used if no message is set for a given rule
* @param $rule
* @param null $parameter
* @return mixed|string
*/
private function getErrorMessage($rule, $parameter = null)
{
$language = [
'alphaNumeric' => ' can only contain letters or numbers.',
'alpha' => ' can only contain letters.',
'numeric' => ' can only contain positive whole numbers.',
'required' => ' can not be blank.',
'email' => ' is not a valid email address.',
'min' => ' has to be greater than ' . $parameter . '.',
'max' => ' has to be less than ' . $parameter . '.',
];
# If a message for the rule was found, use that, otherwise default to " has an error"
$message = isset($language[$rule]) ? $language[$rule] : ' has an error.';
return $message;
}
### VALIDATION METHODS FOUND BELOW HERE ###
/**
* Returns boolean if given value contains only letters/numbers/spaces
* @param $value
* @return bool
*/
protected function alphaNumeric($value)
{
return ctype_alnum(str_replace(' ', '', $value));
}
/**
* Returns boolean if given value contains only letters/spaces
* @param $value
* @return bool
*/
protected function alpha($value)
{
return ctype_alpha(str_replace(' ', '', $value));
}
/**
* Returns boolean if given value contains only positive whole numbers
* @param $value
* @return bool
*/
protected function numeric($value)
{
return ctype_digit(str_replace(' ', '', $value));
}
/**
* Returns boolean if the given value is not blank
* @param $value
* @return bool
*/
protected function required($value)
{
$value = trim($value);
return $value != '' && isset($value) && !is_null($value);
}
/**
* Returns boolean if the given value is a valid email address
* @param $value
* @return mixed
*/
protected function email($value)
{
return filter_var($value, FILTER_VALIDATE_EMAIL);
}
/**
* Returns value if the given value is GREATER THAN (non-inclusive) the given parameter
* @param $value
* @param $parameter
* @return bool
*/
protected function min($value, $parameter)
{
return floatval($value) > floatval($parameter);
}
/**
* Returns value if the given value is LESS THAN (non-inclusive) the given parameter
* @param $value
* @param $parameter
* @return bool
*/
protected function max($value, $parameter)
{
return floatval($value) < floatval($parameter);
}
}