/
validator.php
129 lines (100 loc) · 2.66 KB
/
validator.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
<?php
declare(encoding='UTF-8');
namespace jolt\form;
require_once('jolt/form/validator/rule.php');
class validator {
private $rule_set = NULL;
private $rule = NULL;
private $rule_sets = array();
private $errors = array();
public function __construct() {
}
public function __destruct() {
}
public function __call($method, $argv) {
if (!$this->rule_exists()) {
return $this;
}
$method = strtolower($method);
if (isset($argv[0])) {
$this->add_rule($method, $argv[0]);
if (isset($argv[1])) {
$this->add_error($method, $argv[1]);
}
}
return $this;
}
public function rule_set($rule_set) {
if (!array_key_exists($rule_set, $this->rule_sets)) {
$this->rule_sets[$rule_set] = array();
}
$this->rule_set = $rule_set;
return $this;
}
public function error($error) {
$this->errors[$this->rule_set] = $error;
return $this;
}
public function rule($rule, $field) {
$this->rule = $rule;
if ($this->rule_set_exists()) {
$validator_rule = new jolt\form\validator\rule;
$validator_rule->set_field($field);
$this->rule_sets[$this->rule_set][$rule] = $validator_rule;
}
return $this;
}
public function not_empty($error) {
$this->add_rule('empty', false, $error);
return $this;
}
public function min_max($min, $max, $min_error, $max_error) {
$this->add_rule('minlength', $min)
->add_rule('maxlength', $max);
$this->add_error('minlength', $min_error)
->add_error('maxlength', $max_error);
return $this;
}
public function empty_array($error) {
$this->add_rule('callback', function($array) {
return ((!is_array($array) || 0 === count($array)) ? false : true);
}, $error);
return $this;
}
public function get_error() {
if (array_key_exists($this->rule_set, $this->errors)) {
return $this->errors[$this->rule_set];
}
return NULL;
}
public function is_empty() {
if ($this->rule_set_exists()) {
return (0 === count($this->rule_sets[$this->rule_set]));
}
return true;
}
public function get_rule_set() {
if ($this->rule_set_exists()) {
return $this->rule_sets[$this->rule_set];
}
return array();
}
private function rule_set_exists() {
return (array_key_exists($this->rule_set, $this->rule_sets));
}
private function rule_exists() {
return ($this->rule_set_exists() && array_key_exists($this->rule, $this->rule_sets[$this->rule_set]));
}
private function add_rule($key, $rule) {
if ($this->rule_exists()) {
$this->rule_sets[$this->rule_set][$this->rule]->add_rule($key, $rule);
}
return $this;
}
private function add_error($key, $error) {
if ($this->rule_exists()) {
$this->rule_sets[$this->rule_set][$this->rule]->add_error($key, $error);
}
return $this;
}
}