| 1 |
<?php |
| 2 |
|
| 3 |
// Copyright (c) 2009 Katsuhisa Yuasa <berupon [at] gmail.com> |
| 4 |
// License http://www.opensource.org/licenses/mit-license.html |
| 5 |
|
| 6 |
/* |
| 7 |
リクエスト値 |
| 8 |
|
| 9 |
$params = array( |
| 10 |
'name' => 'value string', |
| 11 |
,,, |
| 12 |
) |
| 13 |
|
| 14 |
検証設定 |
| 15 |
|
| 16 |
settings = '{ |
| 17 |
"name" : { |
| 18 |
"label" : "名前", |
| 19 |
"nullable" : true, |
| 20 |
"notBlank", |
| 21 |
"length" : {"min" : 5, "max" : 20}, |
| 22 |
"digit" : {"min" : 10, "max" : 100}, |
| 23 |
"word", |
| 24 |
"date" : {"from" : "2000/1/1", "to" : "2015/1/1"}, |
| 25 |
"regex" : {0:"regex string"}, |
| 26 |
"inList" : {0:[]}, |
| 27 |
}, |
| 28 |
} |
| 29 |
'; |
| 30 |
|
| 31 |
戻り値:発見したエラーの配列 |
| 32 |
|
| 33 |
array( |
| 34 |
'name' => array('notBlank', 'length'), |
| 35 |
,,, |
| 36 |
) |
| 37 |
|
| 38 |
*/ |
| 39 |
|
| 40 |
class Validator |
| 41 |
{ |
| 42 |
var $values; |
| 43 |
|
| 44 |
function notBlank($value) { |
| 45 |
return !isBlank($value); |
| 46 |
} |
| 47 |
|
| 48 |
function notNull($value) { |
| 49 |
return !is_null($value); |
| 50 |
} |
| 51 |
|
| 52 |
function length($value, $params) { |
| 53 |
extract($params); |
| 54 |
$len = strlen($value); |
| 55 |
if (isset($min) && $len < $min) { |
| 56 |
return false; |
| 57 |
}else if (isset($max) && $max < $len) { |
| 58 |
return false; |
| 59 |
}else { |
| 60 |
return true; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
function alnum($value) { |
| 65 |
return ctype_alnum($value); |
| 66 |
} |
| 67 |
|
| 68 |
function digit($value, $params) { |
| 69 |
if (!ctype_digit($value)) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
return $this->range(intval($value), $params); |
| 73 |
} |
| 74 |
|
| 75 |
function range($value, $params) { |
| 76 |
extract($params); |
| 77 |
if (isset($min) && $value < $min) { |
| 78 |
return false; |
| 79 |
}else if (isset($max) && $max < $value) { |
| 80 |
return false; |
| 81 |
}else { |
| 82 |
return true; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
function word($value) { |
| 87 |
return preg_match("/^[a-zA-Z0-9_]+$/", $value); |
| 88 |
} |
| 89 |
|
| 90 |
function inList($value, $params) { |
| 91 |
return in_array($value, $params); |
| 92 |
} |
| 93 |
|
| 94 |
function validate($values) { |
| 95 |
$this->values = $values; |
| 96 |
$settings = $this->settings(); |
| 97 |
|
| 98 |
$errors = array(); |
| 99 |
foreach ($settings as $key => $rules) { |
| 100 |
$value = array_key_exists($key, $this->values) ? $this->values[$key] : null; |
| 101 |
// assert(!is_null($value)); |
| 102 |
foreach ($rules as $ruleKey => $ruleValue) { |
| 103 |
if (is_int($ruleKey)) { |
| 104 |
$ruleKey = $ruleValue; |
| 105 |
} |
| 106 |
list($ruleName, $errMsg) = explodeTrim($ruleKey, ':', 2); |
| 107 |
|
| 108 |
// echo "$key $ruleName $value<BR>"; |
| 109 |
if (in_array($ruleName, array('label', 'validate', 'settings'))) { |
| 110 |
continue; |
| 111 |
} |
| 112 |
if (!method_exists($this, $ruleName)) { |
| 113 |
assert(false); |
| 114 |
continue; |
| 115 |
} |
| 116 |
if (!call_user_func(array($this, $ruleName), $value, $ruleValue)) { |
| 117 |
$errors[$key][] = $ruleKey; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
return $errors; |
| 122 |
} |
| 123 |
|
| 124 |
static function GetErrorMessages($errors) { |
| 125 |
$messages = array(); |
| 126 |
foreach ($errors as $key => $ruleKeys) { |
| 127 |
foreach ($ruleKeys as $ruleKey) { |
| 128 |
list($ruleName, $errMsg) = explodeTrim($ruleKey, ':', 2); |
| 129 |
$messages[] = $errMsg; |
| 130 |
} |
| 131 |
} |
| 132 |
return $messages; |
| 133 |
} |
| 134 |
|
| 135 |
} |
| 136 |
|