1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace System; |
4
|
|
|
|
5
|
|
|
class Validation |
6
|
|
|
{ |
7
|
|
|
private $app; |
8
|
|
|
|
9
|
|
|
private $input; |
10
|
|
|
|
11
|
|
|
private $errors = []; |
12
|
|
|
|
13
|
|
|
public function __construct(Application $app) |
14
|
|
|
{ |
15
|
|
|
$this->app = $app; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function input($input) |
19
|
|
|
{ |
20
|
|
|
$this->input = $input; |
21
|
|
|
|
22
|
|
|
return $this; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function require($input = null, $msg = null) |
26
|
|
|
{ |
27
|
|
|
if (! $this->input) $this->input = $input; |
28
|
|
|
|
29
|
|
|
$value = $this->value($this->input); |
30
|
|
|
|
31
|
|
|
if (! $value) { |
32
|
|
|
|
33
|
|
|
$msg = $msg ?: 'This input is required'; |
34
|
|
|
|
35
|
|
|
$this->addError($this->input, $msg); |
36
|
|
|
} |
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function email($input = null, $msg = null) |
41
|
|
|
{ |
42
|
|
|
if (! $this->input) $this->input = $input; |
43
|
|
|
|
44
|
|
|
$value = $this->value($this->input); |
45
|
|
|
|
46
|
|
|
if (! filter_var($value, FILTER_VALIDATE_EMAIL)) { |
47
|
|
|
|
48
|
|
|
$msg = $msg ?: sprintf('%s is not valid Email', ucfirst($this->input)); |
49
|
|
|
|
50
|
|
|
$this->addError($this->input, $msg); |
51
|
|
|
} |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function number($input = null, $msg = null) |
56
|
|
|
{ |
57
|
|
|
if (! $this->input) $this->input = $input; |
58
|
|
|
|
59
|
|
|
$value = $this->value($this->input); |
60
|
|
|
|
61
|
|
|
if ($value) { |
62
|
|
|
|
63
|
|
|
if (! is_numeric($value)) { |
64
|
|
|
|
65
|
|
|
$msg = $msg ?: 'the Input must be number'; |
66
|
|
|
|
67
|
|
|
$this->addError($this->input, $msg); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function text($input = null, $msg = null) |
74
|
|
|
{ |
75
|
|
|
if (! $this->input) $this->input = $input; |
76
|
|
|
|
77
|
|
|
$value = $this->value($this->input); |
78
|
|
|
|
79
|
|
|
if ($value) { |
80
|
|
|
|
81
|
|
|
if (is_numeric($value)) { |
82
|
|
|
|
83
|
|
|
$msg = $msg ?: 'the Input must be Text'; |
84
|
|
|
|
85
|
|
|
$this->addError($this->input, $msg); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function minLen($length, $msg = null) |
92
|
|
|
{ |
93
|
|
|
$value = $this->value($this->input); |
94
|
|
|
|
95
|
|
|
if ($value) { |
96
|
|
|
|
97
|
|
|
if (strlen($value) < $length) { |
98
|
|
|
|
99
|
|
|
$msg = $msg ?: 'This input must be at least ' . $length; |
100
|
|
|
|
101
|
|
|
$this->addError($this->input, $msg); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function maxLen($length, $msg = null) |
108
|
|
|
{ |
109
|
|
|
$value = $this->value($this->input); |
110
|
|
|
|
111
|
|
|
if ($value) { |
112
|
|
|
|
113
|
|
|
if (strlen($value) > $length) { |
114
|
|
|
|
115
|
|
|
$msg = $msg ?: 'This must be ' . $length . ' or fewer'; |
116
|
|
|
|
117
|
|
|
$this->addError($this->input, $msg); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function match($input, $msg = null) |
124
|
|
|
{ |
125
|
|
|
$valuePassword = $this->value($this->input); |
126
|
|
|
|
127
|
|
|
$valueConfirm = $this->value($input); |
128
|
|
|
|
129
|
|
|
if ($valuePassword && $valueConfirm ) { |
130
|
|
|
|
131
|
|
|
if ($valuePassword !== $valueConfirm) { |
132
|
|
|
|
133
|
|
|
$msg = $msg ?: 'Passwords does not match'; |
134
|
|
|
|
135
|
|
|
$this->addError('match', $msg); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function unique($data, $msg = null) |
142
|
|
|
{ |
143
|
|
|
$value = $this->value($this->input); |
144
|
|
|
|
145
|
|
|
$table = null; |
146
|
|
|
$column = null; |
147
|
|
|
|
148
|
|
|
$id = null; |
149
|
|
|
$userId = null; |
150
|
|
|
|
151
|
|
|
if (count($data) == 2) list($table, $column) = $data; |
152
|
|
|
if (count($data) == 4) list($table, $column, $id, $userId) = $data; |
153
|
|
|
|
154
|
|
|
$sql = $userId ? $column . ' = ? AND ' . $id . ' != ? ' : $column . ' = ?'; |
155
|
|
|
|
156
|
|
|
$valueSql = $userId ? [$value, $userId] : $value; |
157
|
|
|
|
158
|
|
|
$result = $this->app->db->select($column) |
|
|
|
|
159
|
|
|
->from($table) |
160
|
|
|
->where($sql, $valueSql) |
161
|
|
|
->fetch(); |
162
|
|
|
|
163
|
|
|
if ($result) { |
164
|
|
|
|
165
|
|
|
$msg = $msg ?: sprintf('%s is already exist', ucfirst($this->input)); |
166
|
|
|
|
167
|
|
|
$this->addError($this->input, $msg); |
168
|
|
|
} |
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function message($msg = null) |
173
|
|
|
{ |
174
|
|
|
$this->errors[] = $msg; |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function passes() |
180
|
|
|
{ |
181
|
|
|
return empty($this->errors); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function fails() |
185
|
|
|
{ |
186
|
|
|
return ! empty($this->errors); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function getMsgs() |
190
|
|
|
{ |
191
|
|
|
return $this->errors; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
private function value($input) |
195
|
|
|
{ |
196
|
|
|
return $this->app->request->post($input); |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function addError($input, $msg) |
200
|
|
|
{ |
201
|
|
|
if (! $this->checkError($input)) { |
202
|
|
|
|
203
|
|
|
$this->errors[$input] = $msg; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
private function checkError($input) |
208
|
|
|
{ |
209
|
|
|
return array_key_exists($input, $this->errors); |
210
|
|
|
} |
211
|
|
|
} |