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