1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class Validators extends Object
22: {
23: protected static $validators = array(
24: 'bool' => 'is_bool',
25: 'boolean' => 'is_bool',
26: 'int' => 'is_int',
27: 'integer' => 'is_int',
28: 'float' => 'is_float',
29: 'number' => NULL,
30: 'numeric' => array(__CLASS__, 'isNumeric'),
31: 'numericint' => array(__CLASS__, 'isNumericInt'),
32: 'string' => 'is_string',
33: 'unicode' => array(__CLASS__, 'isUnicode'),
34: 'array' => 'is_array',
35: 'list' => array(__CLASS__, 'isList'),
36: 'object' => 'is_object',
37: 'resource' => 'is_resource',
38: 'scalar' => 'is_scalar',
39: 'callable' => array(__CLASS__, 'isCallable'),
40: 'null' => 'is_null',
41: 'email' => array(__CLASS__, 'isEmail'),
42: 'url' => array(__CLASS__, 'isUrl'),
43: 'none' => array(__CLASS__, 'isNone'),
44: 'pattern' => NULL,
45: 'alnum' => 'ctype_alnum',
46: 'alpha' => 'ctype_alpha',
47: 'digit' => 'ctype_digit',
48: 'lower' => 'ctype_lower',
49: 'upper' => 'ctype_upper',
50: 'space' => 'ctype_space',
51: 'xdigit' => 'ctype_xdigit',
52: );
53:
54: protected static $counters = array(
55: 'string' => 'strlen',
56: 'unicode' => array('Strings', 'length'),
57: 'array' => 'count',
58: 'list' => 'count',
59: 'alnum' => 'strlen',
60: 'alpha' => 'strlen',
61: 'digit' => 'strlen',
62: 'lower' => 'strlen',
63: 'space' => 'strlen',
64: 'upper' => 'strlen',
65: 'xdigit' => 'strlen',
66: );
67:
68:
69: 70: 71: 72: 73: 74: 75:
76: public static function assert($value, $expected, $label = 'variable')
77: {
78: if (!self::is($value, $expected)) {
79: $expected = str_replace(array('|', ':'), array(' or ', ' in range '), $expected);
80: if (is_array($value)) {
81: $type = 'array(' . count($value) . ')';
82: } elseif (is_object($value)) {
83: $type = 'object ' . get_class($value);
84: } elseif (is_string($value) && strlen($value) < 40) {
85: $type = "string '$value'";
86: } else {
87: $type = gettype($value);
88: }
89: throw new AssertionException("The $label expects to be $expected, $type given.");
90: }
91: }
92:
93:
94: 95: 96: 97: 98: 99: 100:
101: public static function assertField($arr, $field, $expected = NULL, $label = "item '%' in array")
102: {
103: self::assert($arr, 'array', 'first argument');
104: if (!array_key_exists($field, $arr)) {
105: throw new AssertionException('Missing ' . str_replace('%', $field, $label) . '.');
106:
107: } elseif ($expected) {
108: self::assert($arr[$field], $expected, str_replace('%', $field, $label));
109: }
110: }
111:
112:
113: 114: 115: 116: 117: 118:
119: public static function is($value, $expected)
120: {
121: foreach (explode('|', $expected) as $item) {
122: list($type) = $item = explode(':', $item, 2);
123: if (isset(self::$validators[$type])) {
124: if (!call_user_func(self::$validators[$type], $value)) {
125: continue;
126: }
127: } elseif ($type === 'number') {
128: if (!is_int($value) && !is_float($value)) {
129: continue;
130: }
131: } elseif ($type === 'pattern') {
132: if (preg_match('|^' . (isset($item[1]) ? $item[1] : '') . '\z|', $value)) {
133: return TRUE;
134: }
135: continue;
136: } elseif (!$value instanceof $type) {
137: continue;
138: }
139:
140: if (isset($item[1])) {
141: if (isset(self::$counters[$type])) {
142: $value = call_user_func(self::$counters[$type], $value);
143: }
144: $range = explode('..', $item[1]);
145: if (!isset($range[1])) {
146: $range[1] = $range[0];
147: }
148: if (($range[0] !== '' && $value < $range[0]) || ($range[1] !== '' && $value > $range[1])) {
149: continue;
150: }
151: }
152: return TRUE;
153: }
154: return FALSE;
155: }
156:
157:
158: 159: 160: 161:
162: public static function isNumericInt($value)
163: {
164: return is_int($value) || is_string($value) && preg_match('#^-?[0-9]+\z#', $value);
165: }
166:
167:
168: 169: 170: 171:
172: public static function isNumeric($value)
173: {
174: return is_float($value) || is_int($value) || is_string($value) && preg_match('#^-?[0-9]*[.]?[0-9]+\z#', $value);
175: }
176:
177:
178: 179: 180: 181:
182: public static function isCallable($value)
183: {
184: return $value && is_callable($value, TRUE);
185: }
186:
187:
188: 189: 190: 191: 192:
193: public static function isUnicode($value)
194: {
195: return is_string($value) && preg_match('##u', $value);
196: }
197:
198:
199: 200: 201: 202:
203: public static function isNone($value)
204: {
205: return $value == NULL;
206: }
207:
208:
209: 210: 211: 212: 213:
214: public static function isList($value)
215: {
216: return is_array($value) && (!$value || array_keys($value) === range(0, count($value) - 1));
217: }
218:
219:
220: 221: 222: 223: 224: 225:
226: public static function isInRange($value, $range)
227: {
228: return (!isset($range[0]) || $value >= $range[0]) && (!isset($range[1]) || $value <= $range[1]);
229: }
230:
231:
232: 233: 234: 235: 236:
237: public static function isEmail($value)
238: {
239: $atom = "[-a-z0-9!#$%&'*+/=?^_`{|}~]";
240: $localPart = "(?:\"(?:[ !\\x23-\\x5B\\x5D-\\x7E]*|\\\\[ -~])+\"|$atom+(?:\\.$atom+)*)";
241: $alpha = "a-z\x80-\xFF";
242: $domain = "[0-9$alpha](?:[-0-9$alpha]{0,61}[0-9$alpha])?";
243: $topDomain = "[$alpha][-0-9$alpha]{0,17}[$alpha]";
244: return (bool) preg_match("(^$localPart@(?:$domain\\.)+$topDomain\\z)i", $value);
245: }
246:
247:
248: 249: 250: 251: 252:
253: public static function isUrl($value)
254: {
255: $alpha = "a-z\x80-\xFF";
256: $domain = "[0-9$alpha](?:[-0-9$alpha]{0,61}[0-9$alpha])?";
257: $topDomain = "[$alpha][-0-9$alpha]{0,17}[$alpha]";
258: return (bool) preg_match("(^https?://(?:(?:$domain\\.)*$topDomain|\\d{1,3}\.\\d{1,3}\.\\d{1,3}\.\\d{1,3}|\[[0-9a-f:]{3,39}\])(:\\d{1,5})?(/\\S*)?\\z)i", $value);
259: }
260:
261: }
262:
263:
264: 265: 266: 267:
268: class AssertionException extends Exception
269: {
270: }
271: