~ubuntu-branches/debian/experimental/php-nette/experimental

« back to all changes in this revision

Viewing changes to Nette-2.0.13/Nette/Utils/Validators.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2013-11-30 08:47:54 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20131130084754-4udf1xsu9085tnfc
Tags: 2.1.0~rc-1
* New upstream branch
* Update copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 * Validation utilites.
19
 
 *
20
 
 * @author     David Grudl
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, // is_int || is_float,
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
 
         * 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 (!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
 
         * Throws exception if an array field is missing or of unexpected type.
97
 
         * @param  array
98
 
         * @param  string  item
99
 
         * @param  string  expected types separated by pipe
100
 
         * @return void
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
 
         * Finds whether a variable is of expected type.
116
 
         * @param  mixed
117
 
         * @param  string  expected types separated by pipe with optional ranges
118
 
         * @return bool
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
 
         * Finds whether a value is an integer.
161
 
         * @return bool
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
 
         * Finds whether a string is a floating point number in decimal base.
171
 
         * @return bool
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
 
         * Finds whether a value is a syntactically correct callback.
181
 
         * @return bool
182
 
         */
183
 
        public static function isCallable($value)
184
 
        {
185
 
                return $value && is_callable($value, TRUE);
186
 
        }
187
 
 
188
 
 
189
 
        /**
190
 
         * Finds whether a value is an UTF-8 encoded string.
191
 
         * @param  string
192
 
         * @return bool
193
 
         */
194
 
        public static function isUnicode($value)
195
 
        {
196
 
                return is_string($value) && preg_match('##u', $value);
197
 
        }
198
 
 
199
 
 
200
 
        /**
201
 
         * Finds whether a value is "falsy".
202
 
         * @return bool
203
 
         */
204
 
        public static function isNone($value)
205
 
        {
206
 
                return $value == NULL; // intentionally ==
207
 
        }
208
 
 
209
 
 
210
 
        /**
211
 
         * Finds whether a variable is a zero-based integer indexed array.
212
 
         * @param  array
213
 
         * @return bool
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
 
         * Is a value in specified range?
223
 
         * @param  mixed
224
 
         * @param  array  min and max value pair
225
 
         * @return bool
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
 
         * Finds whether a string is a valid email address.
235
 
         * @param  string
236
 
         * @return bool
237
 
         */
238
 
        public static function isEmail($value)
239
 
        {
240
 
                $atom = "[-a-z0-9!#$%&'*+/=?^_`{|}~]"; // RFC 5322 unquoted characters in local-part
241
 
                $localPart = "(?:\"(?:[ !\\x23-\\x5B\\x5D-\\x7E]*|\\\\[ -~])+\"|$atom+(?:\\.$atom+)*)"; // quoted or unquoted
242
 
                $alpha = "a-z\x80-\xFF"; // superset of IDN
243
 
                $domain = "[0-9$alpha](?:[-0-9$alpha]{0,61}[0-9$alpha])?"; // RFC 1034 one domain component
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
 
         * Finds whether a string is a valid URL.
251
 
         * @param  string
252
 
         * @return bool
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
 
 * The exception that indicates assertion error.
267
 
 */
268
 
class AssertionException extends \Exception
269
 
{
270
 
}