-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathValidationbag.php
193 lines (175 loc) · 4.56 KB
/
Validationbag.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
<?php
require_once 'Input.php';
trait ValidationBag {
/**
* instance of maximum number of fields
*
* @param int $fieldsCount
*/
public static $fieldsCount;
public static function fieldsCount() {
return static::$fieldsCount;
}
/**
* to set default error and can be usable for setting your own custom errors
*
* @param string | array $message
* @return void
*/
public function throwError(string $message = '') {
$this->session_error = true;
$this->message = $message;
}
/**
* to check if there is an error
*
* @return bool $session_error
*/
public function isError() {
return $this->session_error;
}
/**
* to get the value set by throwError() method
*
* @return bool $session_error
*/
public function error() {
if (Input::exists()) {
return $this->message;
}
}
/**
* set the supplied field as mandatory
*
* @param $var
* @return void
*/
protected function required($var) {
if (empty(Input::get($var))) {
if (self::fieldsCount() > 1) {
$this->throwError("All fields are required!");
}else {
$this->throwError(ucfirst($var)." field is required!");
}
}
}
/**
* sanitize the user supplied data
*
* @param string $key
* @return void
*/
protected function sanitize($key) {
switch ($key) {
case 'username':
if (preg_match('/ /', Input::get($key)) == true) {
$this->throwError("Usename must not contain any spaces!");
}else if (filter_var(Input::get($key), FILTER_VALIDATE_EMAIL) == true) {
$this->throwError("Usename must not be like email!");
}
break;
case 'email':
if (filter_var(Input::get($key), FILTER_VALIDATE_EMAIL) == false)
{
$this->throwError("Incorrect email address!");
}
break;
}
}
/**
* for checking the length of passed $variable along with an $operator and limited $length
* The argument for the following case should be in this way:
* length[operators]:[length]
*
* @param string $var
* @param array $operators = ['==', '!=', '>', '<', '>=', '<=']
* @param bool $length
*/
protected function checkLength($var, $operator, $length) {
$name = ucfirst($var);
switch ($operator) {
case '==':
if (strlen(Input::get($var)) != $length) {
$this->throwError($name.' must be equal to '.$length.' characters');
}
break;
case '!=':
if (strlen(Input::get($var)) == $length) {
$this->throwError($name.' must not be equal to '.$length.' characters');
}
break;
case '<':
if (strlen(Input::get($var)) > $length) {
$this->throwError($name.' must be less than '.$length.' characters');
}
break;
case '>':
if (strlen(Input::get($var)) < $length) {
$this->throwError($name.' must be greater than '.$length.' characters');
}
break;
case '<=':
if (strlen(Input::get($var)) > $length) {
$this->throwError($name.' must be maximum of '.$length.' characters');
}
break;
case '>=':
if (strlen(Input::get($var)) < $length) {
$this->throwError($name.' must be minimum of '.$length.' characters');
}
break;
default:
die('Oops! something went wrong, please check your conditions');
}
}
/**
* to check if the follolwing case is true, default cases are check and remember for setting a setting
*
* @param string $action
* @param string $name
*/
private function checksBag($action = 'check', $name) {
switch($action) {
case 'check':
echo (Input::get($name)) ? Input::get($name) : 'off';
break;
case 'remember':
if (Input::get($name) == "on") {
$this->session_type = "cookies";
}
break;
}
}
/**
* default exist system to check whether username/email exists, this can be extendable and is totally dependant
*
* @param string $var
* @return void
*/
private function exists($var) {
$exists = $this->get('users', [
$var,
'=',
Input::get($var)
]);
$this->address = (function () {
$filename = explode('/', $_SERVER['SCRIPT_FILENAME']);
$address = end($filename);
$address = substr($address, 0, stripos($address, '.'));
return $address;
});
switch ($this->address()) {
case 'signup':
if ($exists->_count == true) {
$this->throwError(ucfirst($var).' already exists');
}
break;
case 'signin':
if ($exists->_count == false) {
$this->throwError("No user exists! Have you registered ?");
}
break;
}
}
}
?>