• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / utils / 22290136219

23 Feb 2026 01:47AM UTC coverage: 93.125% (-0.003%) from 93.128%
22290136219

push

github

dg
added CLAUDE.md

2086 of 2240 relevant lines covered (93.13%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.23
/src/Utils/Validators.php
1
<?php declare(strict_types=1);
1✔
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Nette\Utils;
9

10
use Nette;
11
use function array_key_exists, class_exists, explode, gettype, interface_exists, is_callable, is_float, is_int, is_iterable, is_numeric, is_object, is_string, preg_match, str_ends_with, str_replace, str_starts_with, strlen, strtolower, substr, trait_exists, var_export;
12

13

14
/**
15
 * Validation utilities.
16
 */
17
class Validators
18
{
19
        use Nette\StaticClass;
20

21
        private const BuiltinTypes = [
22
                'string' => 1, 'int' => 1, 'float' => 1, 'bool' => 1, 'array' => 1, 'object' => 1,
23
                'callable' => 1, 'iterable' => 1, 'void' => 1, 'null' => 1, 'mixed' => 1, 'false' => 1,
24
                'never' => 1, 'true' => 1,
25
        ];
26

27
        /** @var array<string, ?(callable(mixed): bool)> */
28
        protected static $validators = [
29
                // PHP types
30
                'array' => 'is_array',
31
                'bool' => 'is_bool',
32
                'boolean' => 'is_bool',
33
                'float' => 'is_float',
34
                'int' => 'is_int',
35
                'integer' => 'is_int',
36
                'null' => 'is_null',
37
                'object' => 'is_object',
38
                'resource' => 'is_resource',
39
                'scalar' => 'is_scalar',
40
                'string' => 'is_string',
41

42
                // pseudo-types
43
                'callable' => [self::class, 'isCallable'],
44
                'iterable' => 'is_iterable',
45
                'list' => [Arrays::class, 'isList'],
46
                'mixed' => [self::class, 'isMixed'],
47
                'none' => [self::class, 'isNone'],
48
                'number' => [self::class, 'isNumber'],
49
                'numeric' => [self::class, 'isNumeric'],
50
                'numericint' => [self::class, 'isNumericInt'],
51

52
                // string patterns
53
                'alnum' => 'ctype_alnum',
54
                'alpha' => 'ctype_alpha',
55
                'digit' => 'ctype_digit',
56
                'lower' => 'ctype_lower',
57
                'pattern' => null,
58
                'space' => 'ctype_space',
59
                'unicode' => [self::class, 'isUnicode'],
60
                'upper' => 'ctype_upper',
61
                'xdigit' => 'ctype_xdigit',
62

63
                // syntax validation
64
                'email' => [self::class, 'isEmail'],
65
                'identifier' => [self::class, 'isPhpIdentifier'],
66
                'uri' => [self::class, 'isUri'],
67
                'url' => [self::class, 'isUrl'],
68

69
                // environment validation
70
                'class' => 'class_exists',
71
                'interface' => 'interface_exists',
72
                'directory' => 'is_dir',
73
                'file' => 'is_file',
74
                'type' => [self::class, 'isType'],
75
        ];
76

77
        /** @var array<string, callable(mixed): int> */
78
        protected static $counters = [
79
                'string' => 'strlen',
80
                'unicode' => [Strings::class, 'length'],
81
                'array' => 'count',
82
                'list' => 'count',
83
                'alnum' => 'strlen',
84
                'alpha' => 'strlen',
85
                'digit' => 'strlen',
86
                'lower' => 'strlen',
87
                'space' => 'strlen',
88
                'upper' => 'strlen',
89
                'xdigit' => 'strlen',
90
        ];
91

92

93
        /**
94
         * Verifies that the value is of expected types separated by pipe.
95
         * @throws AssertionException
96
         */
97
        public static function assert(mixed $value, string $expected, string $label = 'variable'): void
1✔
98
        {
99
                if (!static::is($value, $expected)) {
1✔
100
                        $expected = str_replace(['|', ':'], [' or ', ' in range '], $expected);
1✔
101
                        $translate = ['boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'NULL' => 'null'];
1✔
102
                        $type = $translate[gettype($value)] ?? gettype($value);
1✔
103
                        if (is_int($value) || is_float($value) || (is_string($value) && strlen($value) < 40)) {
1✔
104
                                $type .= ' ' . var_export($value, return: true);
1✔
105
                        } elseif (is_object($value)) {
1✔
106
                                $type .= ' ' . $value::class;
1✔
107
                        }
108

109
                        throw new AssertionException("The $label expects to be $expected, $type given.");
1✔
110
                }
111
        }
112

113

114
        /**
115
         * Verifies that element $key in array is of expected types separated by pipe.
116
         * @param  mixed[]  $array
117
         * @throws AssertionException
118
         */
119
        public static function assertField(
1✔
120
                array $array,
121
                int|string $key,
122
                ?string $expected = null,
123
                string $label = "item '%' in array",
124
        ): void
125
        {
126
                if (!array_key_exists($key, $array)) {
1✔
127
                        throw new AssertionException('Missing ' . str_replace('%', (string) $key, $label) . '.');
1✔
128

129
                } elseif ($expected) {
1✔
130
                        static::assert($array[$key], $expected, str_replace('%', (string) $key, $label));
1✔
131
                }
132
        }
1✔
133

134

135
        /**
136
         * Verifies that the value is of expected types separated by pipe.
137
         */
138
        public static function is(mixed $value, string $expected): bool
1✔
139
        {
140
                foreach (explode('|', $expected) as $item) {
1✔
141
                        if (str_ends_with($item, '[]')) {
1✔
142
                                if (is_iterable($value) && self::everyIs($value, substr($item, 0, -2))) {
1✔
143
                                        return true;
1✔
144
                                }
145

146
                                continue;
1✔
147
                        } elseif (str_starts_with($item, '?')) {
1✔
148
                                $item = substr($item, 1);
1✔
149
                                if ($value === null) {
1✔
150
                                        return true;
1✔
151
                                }
152
                        }
153

154
                        [$type] = $item = explode(':', $item, 2);
1✔
155
                        if (isset(static::$validators[$type])) {
1✔
156
                                try {
157
                                        if (!static::$validators[$type]($value)) {
1✔
158
                                                continue;
1✔
159
                                        }
160
                                } catch (\TypeError) {
1✔
161
                                        continue;
1✔
162
                                }
163
                        } elseif ($type === 'pattern') {
1✔
164
                                if (Strings::match($value, '|^' . ($item[1] ?? '') . '$|D')) {
1✔
165
                                        return true;
1✔
166
                                }
167

168
                                continue;
1✔
169
                        } elseif (!$value instanceof $type) {
1✔
170
                                continue;
1✔
171
                        }
172

173
                        if (isset($item[1])) {
1✔
174
                                $length = $value;
1✔
175
                                if (isset(static::$counters[$type])) {
1✔
176
                                        $length = static::$counters[$type]($value);
1✔
177
                                }
178

179
                                $range = explode('..', $item[1]);
1✔
180
                                if (!isset($range[1])) {
1✔
181
                                        $range[1] = $range[0];
1✔
182
                                }
183

184
                                if (($range[0] !== '' && $length < $range[0]) || ($range[1] !== '' && $length > $range[1])) {
1✔
185
                                        continue;
1✔
186
                                }
187
                        }
188

189
                        return true;
1✔
190
                }
191

192
                return false;
1✔
193
        }
194

195

196
        /**
197
         * Finds whether all values are of expected types separated by pipe.
198
         * @param  mixed[]  $values
199
         */
200
        public static function everyIs(iterable $values, string $expected): bool
1✔
201
        {
202
                foreach ($values as $value) {
1✔
203
                        if (!static::is($value, $expected)) {
1✔
204
                                return false;
1✔
205
                        }
206
                }
207

208
                return true;
1✔
209
        }
210

211

212
        /**
213
         * Checks if the value is an integer or a float.
214
         * @return ($value is int|float ? true : false)
215
         */
216
        public static function isNumber(mixed $value): bool
1✔
217
        {
218
                return is_int($value) || is_float($value);
1✔
219
        }
220

221

222
        /**
223
         * Checks if the value is an integer or a integer written in a string.
224
         * @return ($value is non-empty-string ? bool : ($value is int ? true : false))
225
         */
226
        public static function isNumericInt(mixed $value): bool
1✔
227
        {
228
                return is_int($value) || (is_string($value) && preg_match('#^[+-]?[0-9]+$#D', $value));
1✔
229
        }
230

231

232
        /**
233
         * Checks if the value is a number or a number written in a string.
234
         * @return ($value is non-empty-string ? bool : ($value is int|float ? true : false))
235
         */
236
        public static function isNumeric(mixed $value): bool
1✔
237
        {
238
                return is_float($value) || is_int($value) || (is_string($value) && preg_match('#^[+-]?([0-9]++\.?[0-9]*|\.[0-9]+)$#D', $value));
1✔
239
        }
240

241

242
        /**
243
         * Checks if the value is a syntactically correct callback.
244
         */
245
        public static function isCallable(mixed $value): bool
1✔
246
        {
247
                return $value && is_callable($value, syntax_only: true);
1✔
248
        }
249

250

251
        /**
252
         * Checks if the value is a valid UTF-8 string.
253
         */
254
        public static function isUnicode(mixed $value): bool
1✔
255
        {
256
                return is_string($value) && preg_match('##u', $value);
1✔
257
        }
258

259

260
        /**
261
         * Checks if the value is 0, '', false or null.
262
         * @return ($value is 0|0.0|''|false|null ? true : false)
263
         */
264
        public static function isNone(mixed $value): bool
1✔
265
        {
266
                return $value == null; // intentionally ==
1✔
267
        }
268

269

270
        /** @internal */
271
        public static function isMixed(): bool
272
        {
273
                return true;
1✔
274
        }
275

276

277
        /**
278
         * Checks if a variable is a zero-based integer indexed array.
279
         * @deprecated  use Nette\Utils\Arrays::isList
280
         * @return ($value is list ? true : false)
281
         */
282
        public static function isList(mixed $value): bool
283
        {
284
                return Arrays::isList($value);
×
285
        }
286

287

288
        /**
289
         * Checks if the value is in the given range [min, max], where the upper or lower limit can be omitted (null).
290
         * Numbers, strings and DateTime objects can be compared.
291
         * @param  array{int|float|string|\DateTimeInterface|null, int|float|string|\DateTimeInterface|null}  $range
292
         */
293
        public static function isInRange(mixed $value, array $range): bool
1✔
294
        {
295
                if ($value === null || !(isset($range[0]) || isset($range[1]))) {
1✔
296
                        return false;
1✔
297
                }
298

299
                $limit = $range[0] ?? $range[1];
1✔
300
                if (is_string($limit)) {
1✔
301
                        $value = (string) $value;
1✔
302
                } elseif ($limit instanceof \DateTimeInterface) {
1✔
303
                        if (!$value instanceof \DateTimeInterface) {
1✔
304
                                return false;
×
305
                        }
306
                } elseif (is_numeric($value)) {
1✔
307
                        $value *= 1;
1✔
308
                } else {
309
                        return false;
1✔
310
                }
311

312
                return (!isset($range[0]) || ($value >= $range[0])) && (!isset($range[1]) || ($value <= $range[1]));
1✔
313
        }
314

315

316
        /**
317
         * Checks if the value is a valid email address. It does not verify that the domain actually exists, only the syntax is verified.
318
         */
319
        public static function isEmail(string $value): bool
1✔
320
        {
321
                $atom = "[-a-z0-9!#$%&'*+/=?^_`{|}~]"; // RFC 5322 unquoted characters in local-part
1✔
322
                $alpha = "a-z\x80-\xFF"; // superset of IDN
1✔
323
                return (bool) preg_match(<<<XX
1✔
324
                        (^(?n)
1✔
325
                                ("([ !#-[\\]-~]*|\\\\[ -~])+"|$atom+(\\.$atom+)*)  # quoted or unquoted
1✔
326
                                @
327
                                ([0-9$alpha]([-0-9$alpha]{0,61}[0-9$alpha])?\\.)+  # domain - RFC 1034
1✔
328
                                [$alpha]([-0-9$alpha]{0,17}[$alpha])?              # top domain
1✔
329
                        $)Dix
330
                        XX, $value);
1✔
331
        }
332

333

334
        /**
335
         * Checks if the value is a valid URL address.
336
         */
337
        public static function isUrl(string $value): bool
1✔
338
        {
339
                $alpha = "a-z\x80-\xFF";
1✔
340
                return (bool) preg_match(<<<XX
1✔
341
                        (^(?n)
1✔
342
                                https?://(
343
                                        (([-_0-9$alpha]+\\.)*                       # subdomain
1✔
344
                                                [0-9$alpha]([-0-9$alpha]{0,61}[0-9$alpha])?\\.)?  # domain
1✔
345
                                                [$alpha]([-0-9$alpha]{0,17}[$alpha])?   # top domain
1✔
346
                                        |\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}  # IPv4
347
                                        |\\[[0-9a-f:]{3,39}\\]                      # IPv6
348
                                )(:\\d{1,5})?                                   # port
349
                                (/\\S*)?                                        # path
350
                                (\\?\\S*)?                                      # query
351
                                (\\#\\S*)?                                      # fragment
352
                        $)Dix
353
                        XX, $value);
1✔
354
        }
355

356

357
        /**
358
         * Checks if the value is a valid URI address, that is, actually a string beginning with a syntactically valid schema.
359
         */
360
        public static function isUri(string $value): bool
1✔
361
        {
362
                return (bool) preg_match('#^[a-z\d+\.-]+:\S+$#Di', $value);
1✔
363
        }
364

365

366
        /**
367
         * Checks whether the input is a class, interface or trait.
368
         * @deprecated
369
         */
370
        public static function isType(string $type): bool
1✔
371
        {
372
                return class_exists($type) || interface_exists($type) || trait_exists($type);
1✔
373
        }
374

375

376
        /**
377
         * Checks whether the input is a valid PHP identifier.
378
         */
379
        public static function isPhpIdentifier(string $value): bool
1✔
380
        {
381
                return preg_match('#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$#D', $value) === 1;
1✔
382
        }
383

384

385
        /**
386
         * Determines if type is PHP built-in type. Otherwise, it is the class name.
387
         */
388
        public static function isBuiltinType(string $type): bool
1✔
389
        {
390
                return isset(self::BuiltinTypes[strtolower($type)]);
1✔
391
        }
392

393

394
        /**
395
         * Determines if type is special class name self/parent/static.
396
         */
397
        public static function isClassKeyword(string $name): bool
1✔
398
        {
399
                return (bool) preg_match('#^(self|parent|static)$#Di', $name);
1✔
400
        }
401

402

403
        /**
404
         * Checks whether the given type declaration is syntactically valid.
405
         */
406
        public static function isTypeDeclaration(string $type): bool
1✔
407
        {
408
                return (bool) preg_match(<<<'XX'
1✔
409
                        ~((?n)
1✔
410
                                \?? (?<type> \\? (?<name> [a-zA-Z_\x7f-\xff][\w\x7f-\xff]*) (\\ (?&name))* ) |
411
                                (?<intersection> (?&type) (& (?&type))+ ) |
412
                                (?<upart> (?&type) | \( (?&intersection) \) )  (\| (?&upart))+
413
                        )$~xAD
414
                        XX, $type);
1✔
415
        }
416
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc