Skip to content

Commit ac90a28

Browse files
committed
Validators: added 'file', 'directory', 'class', 'interface' & mixed
1 parent 2f751f1 commit ac90a28

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/Utils/Validators.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Validators
3737
'callable' => [__CLASS__, 'isCallable'],
3838
'iterable' => 'is_iterable',
3939
'list' => [Arrays::class, 'isList'],
40+
'mixed' => [__CLASS__, 'isMixed'],
4041
'none' => [__CLASS__, 'isNone'],
4142
'number' => [__CLASS__, 'isNumber'],
4243
'numeric' => [__CLASS__, 'isNumeric'],
@@ -60,6 +61,10 @@ class Validators
6061
'url' => [__CLASS__, 'isUrl'],
6162

6263
// environment validation
64+
'class' => 'class_exists',
65+
'interface' => 'interface_exists',
66+
'directory' => 'is_dir',
67+
'file' => 'is_file',
6368
'type' => [__CLASS__, 'isType'],
6469
];
6570

@@ -236,6 +241,13 @@ public static function isNone($value): bool
236241
}
237242

238243

244+
/** @internal */
245+
public static function isMixed(): bool
246+
{
247+
return true;
248+
}
249+
250+
239251
/**
240252
* Finds whether a variable is a zero-based integer indexed array.
241253
*/

tests/Utils/Validators.is().phpt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ test(function () {
170170
});
171171

172172

173+
test(function () {
174+
Assert::true(Validators::is([], 'mixed'));
175+
Assert::true(Validators::is(null, 'mixed'));
176+
});
177+
178+
173179
test(function () {
174180
Assert::false(Validators::is('', 'email'));
175181
Assert::false(Validators::is(false, 'email'));
@@ -321,6 +327,32 @@ test(function () {
321327
});
322328

323329

330+
test(function () {
331+
Assert::true(Validators::is('rimmer', 'class'));
332+
Assert::false(Validators::is('kryton', 'class'));
333+
Assert::false(Validators::is('1', 'class'));
334+
});
335+
336+
337+
test(function () {
338+
Assert::false(Validators::is('rimmer', 'interface'));
339+
Assert::true(Validators::is('kryton', 'interface'));
340+
Assert::false(Validators::is('1', 'interface'));
341+
});
342+
343+
344+
test(function () {
345+
Assert::true(Validators::is(__FILE__, 'file'));
346+
Assert::false(Validators::is(__FILE__ . 'xx', 'class'));
347+
});
348+
349+
350+
test(function () {
351+
Assert::true(Validators::is(__DIR__, 'directory'));
352+
Assert::false(Validators::is(__DIR__ . 'xx', 'directory'));
353+
});
354+
355+
324356
test(function () {
325357
Assert::true(Validators::is('Item', 'identifier'));
326358
Assert::false(Validators::is('0Item', 'identifier'));

0 commit comments

Comments
 (0)