forked from T-Hugs/ValidCake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriteValidations.php
132 lines (116 loc) · 3.88 KB
/
writeValidations.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
<?php
define('T', "\t");
define('NL', "\r\n");
define('BR', "<br />\r\n");
define('Q', "'");
$ruleOptionDefaults = array(
'required' => 'false',
'allowEmpty' => 'ignore',
'on' => 'null',
'last' => 'false',
'message' => 'null'
);
function wrap($string, $token) {return $token.$string.$token;}
function c($s,$t){return $s.$t;}
function reprArr($arr)
{
return 'array('.implode(', ',array_map('wrap', $arr, array_fill(0, sizeof($arr), Q))).')';
}
if (!isset($_POST['fields'])) exit('Please add one or more fields.');
$def = isset($_POST['options']['extra_options']);
$t = '';
while ($_POST['options']['num_tabs']--) $t .= T;
$validate = $t.'var $validate = array('.NL;
$fields = $_POST['fields'];
$commaSpace1 = '';
foreach ($fields as $field)
{
$commaSpace2 = '';
$validate .= $commaSpace1;
$name = $field['name'];
$validate .= $t.T.wrap($name, Q).' => array('.NL;
$rules = $field['rules'];
foreach ($rules as $rule)
{
$required = isset($rule['required'])?'true':'false';
$requiredDef = wrap('required', Q).' => '.$required.','.NL;
$allowEmpty = $rule['allowEmpty'];
$allowEmptyDef = wrap('allowEmpty', Q).' => '.($allowEmpty == 'ignore'?'null':$allowEmpty).','.NL;
$on = '';
if (isset($rule['update']) and isset($rule['create'])) $on = 'null';
elseif (isset($rule['update'])) $on = wrap('update', Q);
elseif (isset($rule['create'])) $on = wrap('create', Q);
else $on = 'null';
$onDef = wrap('on', Q).' => '.$on.','.NL;
$last = isset($rule['validateThisRuleLast'])?'true':'false';
$lastDef = wrap('last', Q).' => '.$last.','.NL;
$message = ($rule['customMessage'] === '')?'null':$rule['customMessage'];
$messageDef = wrap('message', Q).' => '.($message=='null'?$message:wrap($message, Q)).NL;
$ruleType = $rule['ruleType'];
$ruleDef = wrap('rule', Q).' => ';
if (isset($rule['typeOptions']))
{
if ($ruleType === 'customFunc') $ruleType = array_shift($rule['typeOptions']);
$ruleDef .= 'array('.wrap($ruleType, Q);
foreach ($rule['typeOptions'] as $key => $typeOption)
{
if (is_array($typeOption))
{
$typeOption = reprArr($typeOption);
$ruleDef .= ', '.$typeOption;
}
elseif ($ruleType === 'inList' or $ruleType === 'extension')
{
$typeOption = reprArr(explode(';', $typeOption));
$ruleDef .= ', '.$typeOption;
}
else
{
$temp = explode('_', $key);
$valType = (strpos($key, '_') === false) ? 'string' : $temp[1];
switch ($valType)
{
case 'bool':
case 'int':
case 'float':
case 'manual':
$ruleDef .= ', '.$typeOption;
break;
case 'string':
default:
if ($typeOption === '')
$ruleDef .= ', '.'null';
else
$ruleDef .= ', '.wrap($typeOption, Q);
break;
}
}
}
$ruleDef .= '),'.NL;
}
else
$ruleDef .= wrap($ruleType, Q).','.NL;
$writeReq = $def || ($ruleOptionDefaults['required'] !== $required);
$writeAE = $def || ($ruleOptionDefaults['allowEmpty'] !== $allowEmpty);
$writeOn = $def || ($ruleOptionDefaults['on'] !== $on);
$writeLast = $def || ($ruleOptionDefaults['last'] !== $last);
$writeMsg = $def || ($ruleOptionDefaults['message'] !== $message);
$ruleName = $rule['ruleName'];
$validate .= $commaSpace2;
$validate .= $t.T.T.wrap($ruleName, Q).' => array('.NL;
$validate .= $t.T.T.T.$ruleDef;
if ($writeReq) $validate .= $t.T.T.T.$requiredDef;
if ($writeAE) $validate .= $t.T.T.T.$allowEmptyDef;
if ($writeOn) $validate .= $t.T.T.T.$onDef;
if ($writeLast) $validate .= $t.T.T.T.$lastDef;
if ($writeMsg) $validate .= $t.T.T.T.$messageDef;
if ($validate{strlen($validate)-3} == ',') $validate = substr($validate, 0, strlen($validate)-3).substr($validate,strlen($validate)-2);
$validate .= $t.T.T.')';
$commaSpace2 = ', '.NL;
}
$validate .= NL.$t.T.')';
$commaSpace1 = ', '.NL;
}
$validate .= NL.$t.');'.NL;
echo $validate;
?>