Namespaces

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

Classes

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