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:      * Throws exception if a variable is of unexpected type.
 71:      * @param  mixed
 72:      * @param  string  expected types separated by pipe
 73:      * @param  string  label
 74:      * @return void
 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 NAssertionException("The $label expects to be $expected, $type given.");
 90:         }
 91:     }
 92: 
 93: 
 94:     /**
 95:      * Throws exception if an array field is missing or of unexpected type.
 96:      * @param  array
 97:      * @param  string  item
 98:      * @param  string  expected types separated by pipe
 99:      * @return void
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 NAssertionException('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:      * Finds whether a variable is of expected type.
115:      * @param  mixed
116:      * @param  string  expected types separated by pipe with optional ranges
117:      * @return bool
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:      * Finds whether a value is an integer.
160:      * @return bool
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:      * Finds whether a string is a floating point number in decimal base.
170:      * @return bool
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:      * Finds whether a value is a syntactically correct callback.
180:      * @return bool
181:      */
182:     public static function isCallable($value)
183:     {
184:         return $value && is_callable($value, TRUE);
185:     }
186: 
187: 
188:     /**
189:      * Finds whether a value is an UTF-8 encoded string.
190:      * @param  string
191:      * @return bool
192:      */
193:     public static function isUnicode($value)
194:     {
195:         return is_string($value) && preg_match('##u', $value);
196:     }
197: 
198: 
199:     /**
200:      * Finds whether a value is "falsy".
201:      * @return bool
202:      */
203:     public static function isNone($value)
204:     {
205:         return $value == NULL; // intentionally ==
206:     }
207: 
208: 
209:     /**
210:      * Finds whether a variable is a zero-based integer indexed array.
211:      * @param  array
212:      * @return bool
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:      * Is a value in specified range?
222:      * @param  mixed
223:      * @param  array  min and max value pair
224:      * @return bool
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:      * 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: 
264: /**
265:  * The exception that indicates assertion error.
266:  * @package Nette\Utils
267:  */
268: class NAssertionException extends Exception
269: {
270: }
271: 
Nette Framework 2.0.12 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0