Packages

  • 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

  • NArrays
  • NFinder
  • NHtml
  • NJson
  • NLimitedScope
  • NMimeTypeDetector
  • NNeon
  • NNeonEntity
  • NPaginator
  • NStrings
  • NTokenizer
  • NValidators

Exceptions

  • NAssertionException
  • NJsonException
  • NNeonException
  • NRegexpException
  • NTokenizerException
  • Overview
  • Package
  • 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:  * @package Nette\Utils
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Validation utilites.
 17:  *
 18:  * @author     David Grudl
 19:  * @package Nette\Utils
 20:  */
 21: class NValidators extends NObject
 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, // is_int || is_float,
 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('NStrings', '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:      * Throws exception if a variable is of unexpected type.
 72:      * @param  mixed
 73:      * @param  string  expected types separated by pipe
 74:      * @param  string  label
 75:      * @return void
 76:      */
 77:     public static function assert($value, $expected, $label = 'variable')
 78:     {
 79:         if (!self::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 NAssertionException("The $label expects to be $expected, $type given.");
 91:         }
 92:     }
 93: 
 94: 
 95: 
 96:     /**
 97:      * Throws exception if an array field is missing or of unexpected type.
 98:      * @param  array
 99:      * @param  string  item
100:      * @param  string  expected types separated by pipe
101:      * @return void
102:      */
103:     public static function assertField($arr, $field, $expected = NULL, $label = "item '%' in array")
104:     {
105:         self::assert($arr, 'array', 'first argument');
106:         if (!array_key_exists($field, $arr)) {
107:             throw new NAssertionException('Missing ' . str_replace('%', $field, $label) . '.');
108: 
109:         } elseif ($expected) {
110:             self::assert($arr[$field], $expected, str_replace('%', $field, $label));
111:         }
112:     }
113: 
114: 
115: 
116:     /**
117:      * Finds whether a variable is of expected type.
118:      * @param  mixed
119:      * @param  string  expected types separated by pipe with optional ranges
120:      * @return bool
121:      */
122:     public static function is($value, $expected)
123:     {
124:         foreach (explode('|', $expected) as $item) {
125:             list($type) = $item = explode(':', $item, 2);
126:             if (isset(self::$validators[$type])) {
127:                 if (!call_user_func(self::$validators[$type], $value)) {
128:                     continue;
129:                 }
130:             } elseif ($type === 'number') {
131:                 if (!is_int($value) && !is_float($value)) {
132:                     continue;
133:                 }
134:             } elseif ($type === 'pattern') {
135:                 if (preg_match('|^' . (isset($item[1]) ? $item[1] : '') . '\z|', $value)) {
136:                     return TRUE;
137:                 }
138:                 continue;
139:             } elseif (!$value instanceof $type) {
140:                 continue;
141:             }
142: 
143:             if (isset($item[1])) {
144:                 if (isset(self::$counters[$type])) {
145:                     $value = call_user_func(self::$counters[$type], $value);
146:                 }
147:                 $range = explode('..', $item[1]);
148:                 if (!isset($range[1])) {
149:                     $range[1] = $range[0];
150:                 }
151:                 if (($range[0] !== '' && $value < $range[0]) || ($range[1] !== '' && $value > $range[1])) {
152:                     continue;
153:                 }
154:             }
155:             return TRUE;
156:         }
157:         return FALSE;
158:     }
159: 
160: 
161: 
162:     /**
163:      * Finds whether a value is an integer.
164:      * @param  mixed
165:      * @return bool
166:      */
167:     public static function isNumericInt($value)
168:     {
169:         return is_int($value) || is_string($value) && preg_match('#^-?[0-9]+\z#', $value);
170:     }
171: 
172: 
173: 
174:     /**
175:      * Finds whether a string is a floating point number in decimal base.
176:      * @param  mixed
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:      * @param  mixed
189:      * @return bool
190:      */
191:     public static function isCallable($value)
192:     {
193:         return $value && is_callable($value, TRUE);
194:     }
195: 
196: 
197: 
198:     /**
199:      * Finds whether a value is an UTF-8 encoded string.
200:      * @param  string
201:      * @return bool
202:      */
203:     public static function isUnicode($value)
204:     {
205:         return is_string($value) && preg_match('##u', $value);
206:     }
207: 
208: 
209: 
210:     /**
211:      * Finds whether a value is "falsy".
212:      * @param  mixed
213:      * @return bool
214:      */
215:     public static function isNone($value)
216:     {
217:         return $value == NULL; // intentionally ==
218:     }
219: 
220: 
221: 
222:     /**
223:      * Finds whether a variable is a zero-based integer indexed array.
224:      * @param  array
225:      * @return bool
226:      */
227:     public static function isList($value)
228:     {
229:         return is_array($value) && (!$value || array_keys($value) === range(0, count($value) - 1));
230:     }
231: 
232: 
233: 
234:     /**
235:      * Is a value in specified range?
236:      * @param  mixed
237:      * @param  array  min and max value pair
238:      * @return bool
239:      */
240:     public static function isInRange($value, $range)
241:     {
242:         return (!isset($range[0]) || $value >= $range[0]) && (!isset($range[1]) || $value <= $range[1]);
243:     }
244: 
245: 
246: 
247:     /**
248:      * Finds whether a string is a valid email address.
249:      * @param  string
250:      * @return bool
251:      */
252:     public static function isEmail($value)
253:     {
254:         $atom = "[-a-z0-9!#$%&'*+/=?^_`{|}~]"; // RFC 5322 unquoted characters in local-part
255:         $localPart = "(?:\"(?:[ !\\x23-\\x5B\\x5D-\\x7E]*|\\\\[ -~])+\"|$atom+(?:\\.$atom+)*)"; // quoted or unquoted
256:         $alpha = "a-z\x80-\xFF"; // superset of IDN
257:         $domain = "[0-9$alpha](?:[-0-9$alpha]{0,61}[0-9$alpha])?"; // RFC 1034 one domain component
258:         $topDomain = "[$alpha][-0-9$alpha]{0,17}[$alpha]";
259:         return (bool) preg_match("(^$localPart@(?:$domain\\.)+$topDomain\\z)i", $value);
260:     }
261: 
262: 
263: 
264:     /**
265:      * Finds whether a string is a valid URL.
266:      * @param  string
267:      * @return bool
268:      */
269:     public static function isUrl($value)
270:     {
271:         $alpha = "a-z\x80-\xFF";
272:         $domain = "[0-9$alpha](?:[-0-9$alpha]{0,61}[0-9$alpha])?";
273:         $topDomain = "[$alpha][-0-9$alpha]{0,17}[$alpha]";
274:         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);
275:     }
276: 
277: }
278: 
279: 
280: 
281: /**
282:  * The exception that indicates assertion error.
283:  * @package Nette\Utils
284:  */
285: class NAssertionException extends Exception
286: {
287: }
288: 
Nette Framework 2.0.7 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0