Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • Arrays
  • Finder
  • Html
  • Json
  • LimitedScope
  • MimeTypeDetector
  • Neon
  • NeonEntity
  • Paginator
  • Strings
  • Tokenizer
  • Validators

Exceptions

  • AssertionException
  • JsonException
  • NeonException
  • RegexpException
  • TokenizerException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette\Utils;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18: /**
 19:  * Validation utilites.
 20:  *
 21:  * @author     David Grudl
 22:  */
 23: class Validators extends Nette\Object
 24: {
 25:     protected static $validators = array(
 26:         'bool' => 'is_bool',
 27:         'boolean' => 'is_bool',
 28:         'int' => 'is_int',
 29:         'integer' => 'is_int',
 30:         'float' => 'is_float',
 31:         'number' => NULL, // is_int || is_float,
 32:         'numeric' => array(__CLASS__, 'isNumeric'),
 33:         'numericint' => array(__CLASS__, 'isNumericInt'),
 34:         'string' =>  'is_string',
 35:         'unicode' => array(__CLASS__, 'isUnicode'),
 36:         'array' => 'is_array',
 37:         'list' => array(__CLASS__, 'isList'),
 38:         'object' => 'is_object',
 39:         'resource' => 'is_resource',
 40:         'scalar' => 'is_scalar',
 41:         'callable' => array(__CLASS__, 'isCallable'),
 42:         'null' => 'is_null',
 43:         'email' => array(__CLASS__, 'isEmail'),
 44:         'url' => array(__CLASS__, 'isUrl'),
 45:         'none' => array(__CLASS__, 'isNone'),
 46:         'pattern' => NULL,
 47:         'alnum' => 'ctype_alnum',
 48:         'alpha' => 'ctype_alpha',
 49:         'digit' => 'ctype_digit',
 50:         'lower' => 'ctype_lower',
 51:         'upper' => 'ctype_upper',
 52:         'space' => 'ctype_space',
 53:         'xdigit' => 'ctype_xdigit',
 54:     );
 55: 
 56:     protected static $counters = array(
 57:         'string' =>  'strlen',
 58:         'unicode' => array('Nette\Utils\Strings', 'length'),
 59:         'array' => 'count',
 60:         'list' => 'count',
 61:         'alnum' => 'strlen',
 62:         'alpha' => 'strlen',
 63:         'digit' => 'strlen',
 64:         'lower' => 'strlen',
 65:         'space' => 'strlen',
 66:         'upper' => 'strlen',
 67:         'xdigit' => 'strlen',
 68:     );
 69: 
 70: 
 71: 
 72:     /**
 73:      * Throws exception if a variable is of unexpected type.
 74:      * @param  mixed
 75:      * @param  string  expected types separated by pipe
 76:      * @param  string  label
 77:      * @return void
 78:      */
 79:     public static function assert($value, $expected, $label = 'variable')
 80:     {
 81:         if (!static::is($value, $expected)) {
 82:             $expected = str_replace(array('|', ':'), array(' or ', ' in range '), $expected);
 83:             if (is_array($value)) {
 84:                 $type = 'array(' . count($value) . ')';
 85:             } elseif (is_object($value)) {
 86:                 $type = 'object ' . get_class($value);
 87:             } elseif (is_string($value) && strlen($value) < 40) {
 88:                 $type = "string '$value'";
 89:             } else {
 90:                 $type = gettype($value);
 91:             }
 92:             throw new AssertionException("The $label expects to be $expected, $type given.");
 93:         }
 94:     }
 95: 
 96: 
 97: 
 98:     /**
 99:      * Throws exception if an array field is missing or of unexpected type.
100:      * @param  array
101:      * @param  string  item
102:      * @param  string  expected types separated by pipe
103:      * @return void
104:      */
105:     public static function assertField($arr, $field, $expected = NULL, $label = "item '%' in array")
106:     {
107:         self::assert($arr, 'array', 'first argument');
108:         if (!array_key_exists($field, $arr)) {
109:             throw new AssertionException('Missing ' . str_replace('%', $field, $label) . '.');
110: 
111:         } elseif ($expected) {
112:             static::assert($arr[$field], $expected, str_replace('%', $field, $label));
113:         }
114:     }
115: 
116: 
117: 
118:     /**
119:      * Finds whether a variable is of expected type.
120:      * @param  mixed
121:      * @param  string  expected types separated by pipe with optional ranges
122:      * @return bool
123:      */
124:     public static function is($value, $expected)
125:     {
126:         foreach (explode('|', $expected) as $item) {
127:             list($type) = $item = explode(':', $item, 2);
128:             if (isset(static::$validators[$type])) {
129:                 if (!call_user_func(static::$validators[$type], $value)) {
130:                     continue;
131:                 }
132:             } elseif ($type === 'number') {
133:                 if (!is_int($value) && !is_float($value)) {
134:                     continue;
135:                 }
136:             } elseif ($type === 'pattern') {
137:                 if (preg_match('|^' . (isset($item[1]) ? $item[1] : '') . '\z|', $value)) {
138:                     return TRUE;
139:                 }
140:                 continue;
141:             } elseif (!$value instanceof $type) {
142:                 continue;
143:             }
144: 
145:             if (isset($item[1])) {
146:                 if (isset(static::$counters[$type])) {
147:                     $value = call_user_func(static::$counters[$type], $value);
148:                 }
149:                 $range = explode('..', $item[1]);
150:                 if (!isset($range[1])) {
151:                     $range[1] = $range[0];
152:                 }
153:                 if (($range[0] !== '' && $value < $range[0]) || ($range[1] !== '' && $value > $range[1])) {
154:                     continue;
155:                 }
156:             }
157:             return TRUE;
158:         }
159:         return FALSE;
160:     }
161: 
162: 
163: 
164:     /**
165:      * Finds whether a value is an integer.
166:      * @return bool
167:      */
168:     public static function isNumericInt($value)
169:     {
170:         return is_int($value) || is_string($value) && preg_match('#^-?[0-9]+\z#', $value);
171:     }
172: 
173: 
174: 
175:     /**
176:      * Finds whether a string is a floating point number in decimal base.
177:      * @return bool
178:      */
179:     public static function isNumeric($value)
180:     {
181:         return is_float($value) || is_int($value) || is_string($value) && preg_match('#^-?[0-9]*[.]?[0-9]+\z#', $value);
182:     }
183: 
184: 
185: 
186:     /**
187:      * Finds whether a value is a syntactically correct callback.
188:      * @return bool
189:      */
190:     public static function isCallable($value)
191:     {
192:         return $value && is_callable($value, TRUE);
193:     }
194: 
195: 
196: 
197:     /**
198:      * Finds whether a value is an UTF-8 encoded string.
199:      * @param  string
200:      * @return bool
201:      */
202:     public static function isUnicode($value)
203:     {
204:         return is_string($value) && preg_match('##u', $value);
205:     }
206: 
207: 
208: 
209:     /**
210:      * Finds whether a value is "falsy".
211:      * @return bool
212:      */
213:     public static function isNone($value)
214:     {
215:         return $value == NULL; // intentionally ==
216:     }
217: 
218: 
219: 
220:     /**
221:      * Finds whether a variable is a zero-based integer indexed array.
222:      * @param  array
223:      * @return bool
224:      */
225:     public static function isList($value)
226:     {
227:         return is_array($value) && (!$value || array_keys($value) === range(0, count($value) - 1));
228:     }
229: 
230: 
231: 
232:     /**
233:      * Is a value in specified range?
234:      * @param  mixed
235:      * @param  array  min and max value pair
236:      * @return bool
237:      */
238:     public static function isInRange($value, $range)
239:     {
240:         return (!isset($range[0]) || $value >= $range[0]) && (!isset($range[1]) || $value <= $range[1]);
241:     }
242: 
243: 
244: 
245:     /**
246:      * Finds whether a string is a valid email address.
247:      * @param  string
248:      * @return bool
249:      */
250:     public static function isEmail($value)
251:     {
252:         $atom = "[-a-z0-9!#$%&'*+/=?^_`{|}~]"; // RFC 5322 unquoted characters in local-part
253:         $localPart = "(?:\"(?:[ !\\x23-\\x5B\\x5D-\\x7E]*|\\\\[ -~])+\"|$atom+(?:\\.$atom+)*)"; // quoted or unquoted
254:         $alpha = "a-z\x80-\xFF"; // superset of IDN
255:         $domain = "[0-9$alpha](?:[-0-9$alpha]{0,61}[0-9$alpha])?"; // RFC 1034 one domain component
256:         $topDomain = "[$alpha][-0-9$alpha]{0,17}[$alpha]";
257:         return (bool) preg_match("(^$localPart@(?:$domain\\.)+$topDomain\\z)i", $value);
258:     }
259: 
260: 
261: 
262:     /**
263:      * Finds whether a string is a valid URL.
264:      * @param  string
265:      * @return bool
266:      */
267:     public static function isUrl($value)
268:     {
269:         $alpha = "a-z\x80-\xFF";
270:         $domain = "[0-9$alpha](?:[-0-9$alpha]{0,61}[0-9$alpha])?";
271:         $topDomain = "[$alpha][-0-9$alpha]{0,17}[$alpha]";
272:         return (bool) preg_match("(^https?://(?:(?:$domain\\.)*$topDomain|\\d{1,3}\.\\d{1,3}\.\\d{1,3}\.\\d{1,3})(:\\d{1,5})?(/\\S*)?\\z)i", $value);
273:     }
274: 
275: }
276: 
277: 
278: 
279: /**
280:  * The exception that indicates assertion error.
281:  */
282: class AssertionException extends \Exception
283: {
284: }
285: 
Nette Framework 2.0.10 API API documentation generated by ApiGen 2.8.0