-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRules.php
More file actions
146 lines (134 loc) · 3.13 KB
/
Rules.php
File metadata and controls
146 lines (134 loc) · 3.13 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
<?php
/**
* @package framework
* @copyright Copyright (c) 2005-2020 The Regents of the University of California.
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Qubeshub\Database;
/**
* Database validation class
*/
class Rules
{
/**
* Do validation checks on provided data
*
* @param array $data The fields to validate
* @param array $rules The rules upon which to validate
* @return array|bool
* @since 2.0.0
**/
public static function validate($data, $rules)
{
$errors = array();
foreach ($data as $k => $v)
{
if (array_key_exists($k, $rules))
{
// (Re)set rule variable
$rule = null;
if (is_callable($rules[$k]))
{
if ($error = call_user_func_array($rules[$k], [$data]))
{
$errors[] = $error;
}
}
else if (strpos($rules[$k], '|'))
{
$rule = explode('|', $rules[$k]);
}
else
{
$rule = array($rules[$k]);
}
if (isset($rule))
{
foreach ($rule as $r)
{
if (method_exists(__CLASS__, $r))
{
if ($error = self::$r($k, $v))
{
$errors[] = $error;
}
}
}
}
}
}
return (count($errors) > 0) ? $errors : true;
}
/**
* Checks that var isn't empty
*
* @param string $key The field name
* @param mixed $var The field content
* @return bool|string
* @since 2.0.0
**/
private static function notempty($key, $var)
{
return !empty($var) ? false : "{$key} cannot be empty";
}
/**
* Checks that var is positive
*
* @param string $key The field name
* @param mixed $var The field content
* @return bool|string
* @since 2.0.0
**/
private static function positive($key, $var)
{
return ($var >= 0) ? false : "{$key} must be a positive integer";
}
/**
* Checks that var is non-zero
*
* @param string $key The field name
* @param mixed $var The field content
* @return bool|string
* @since 2.0.0
**/
private static function nonzero($key, $var)
{
return ($var > 0 || $var < 0) ? false : "{$key} cannot be zero";
}
/**
* Checks that var is alphabetical
*
* @param string $key The field name
* @param mixed $var The field content
* @return bool|string
* @since 2.0.0
**/
private static function alpha($key, $var)
{
return (preg_match('/^[[:alpha:] ]+$/', $var)) ? false : "{$key} can only contain alphabetical characters";
}
/**
* Checks that var is phone
*
* @param string $key The field name
* @param mixed $var The field content
* @return bool|string
* @since 2.0.0
**/
private static function phone($key, $var)
{
return (\Hubzero\Utility\Validate::phone($var)) ? false : "{$key} does not appear to be a valid phone number";
}
/**
* Checks that var is email
*
* @param string $key The field name
* @param mixed $var The field content
* @return bool|string
* @since 2.0.0
**/
private static function email($key, $var)
{
return (\Hubzero\Utility\Validate::email($var)) ? false : "{$key} does not appear to be a valid email address";
}
}