-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayValidate.class.php
More file actions
157 lines (140 loc) · 4.57 KB
/
ArrayValidate.class.php
File metadata and controls
157 lines (140 loc) · 4.57 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
147
148
149
150
151
152
153
154
155
156
157
<?php
class ArrayValidate
{
/**
* @var array
*/
private $config = array(
/**
* lb_erro => Labels Erro, change the "?" the desired value.
*/
"lb_erro" => array(
"notnull" => "Field ?, can not be null.",
"maxlength" => "Field ?, can not be greater than ? characters.",
"minlength" => "Field ?, can not be less than ? characters.",
"dateValid" => "Field ?, date invalid.",
),
/**
* validates => Validations, Create your own validation.
* $valid => value method. eg "notnull" => true, $valid => true;
* $valObj => field value;
* $key => field index;
* $label => field label;
*/
"validates" => array(
"notnull" =>
'if($valid === true && empty($valObj)){
$this->construct_erro("notnull", array($label));
}',
"maxlength" =>
'if (strlen($valObj) > intval($valid)) {
$this->construct_erro("maxlength", array($label,$valid));
}',
"minlength" =>
'if (strlen($valObj) < intval($valid)) {
$this->construct_erro("minlength", array($label,$valid));
}',
"dateValid" =>
'if($valid === true && count(explode("/", $valObj)) !== 3){
$this->construct_erro("dateValid", array($label));
}else if($valid === true && count(explode("/", $valObj)) === 3){
list($m, $d, $y) = explode("/", $valObj);
if(!checkdate($m, $d, $y)){
$this->construct_erro("dateValid", array($label));
}
}'
),
/**
* Format, format the return value.
* $valObj => field value;
* $key => field index;
* $label => field label;
*/
"format" => array(
"date" => 'list($m, $d, $y) = explode("/", $valObj); $this->construct_rs($key, $y . "-" . $m . "-" . $d);',
"convertNumber" => '$this->construct_rs($key, preg_replace("/\D/", "", $valObj));'
)
);
/**
* Get Config
* @return array
*/
public function getConfig()
{
return $this->config;
}
/**
* Set APPEND Config
* @param $config
*/
public function setConfig($config, $key = null)
{
foreach ($config as $k => $v) {
if(is_array($v)){
$this->setConfig($v, $k);
}else if(!empty($key)){
$this->config[$key][$k] = $v;
}
}
}
/**
* @var array
*/
private $return = array(
"data" => array(),
"erros" => array()
);
/**
* @param $lb_erro
* @param $data
*/
private function construct_erro($lb_erro, $data)
{
if (array_key_exists($lb_erro, $this->config['lb_erro'])) {
if (is_array($data)) {
$text = $this->config['lb_erro'][$lb_erro];
for ($i = 0; strpos($text, '?'); $i++) {
$text = preg_replace('/\?/', $data[$i], $text, $i + 1);
}
$this->return["erros"][] = $text;
} else {
$this->return["erros"][] = $this->config['lb_erro'][$lb_erro];
}
}
}
/**
* @param $key
* @param $valObj
*/
private function construct_rs($key, $valObj)
{
$this->return["data"][$key] = $valObj;
}
/**
* @param $validate
* @param $object
* @return array
*/
public function margeValidate($validate, $object)
{
foreach ($validate as $k => $v) {
$key = $k;
$valObj = $object[$key];
$label = $v['label'];
foreach ($v['validates'] as $kv => $vv) {
if (array_key_exists($kv, $this->config['validates'])) {
$valid = $vv;
eval($this->config['validates'][$kv]);
}
}
if (!empty($valObj)) {
if (isset($v['format']) && array_key_exists($v['format'], $this->config['format'])) {
eval($this->config['format'][$v['format']]);
} else {
$this->construct_rs($key, $object[$key]);
}
}
}
return $this->return;
}
}