Skip to content

Commit 19633f7

Browse files
committed
add iterable, iterable|null, array|object, array|object|null
1 parent 6e1be7a commit 19633f7

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

bin/methods.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ types:
1616
assertions: '!is_scalar($value)'
1717
callable:
1818
assertions: '!is_callable($value)'
19+
iterable:
20+
assertions: '!is_iterable($value)'
1921
null:
2022
assertions: '$value !== null'
2123

@@ -50,6 +52,8 @@ generate:
5052
- array|null
5153
- object
5254
- object|null
55+
- array|object
56+
- array|object|null
5357
- string
5458
- string|null
5559
- int
@@ -62,6 +66,8 @@ generate:
6266
- bool|null
6367
- callable
6468
- callable|null
69+
- iterable
70+
- iterable|null
6571
special:
6672
- scalar
6773
- scalar|null

src/Mixins/ArrayTypeAssertTrait.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ public static function objectOrNull(mixed $array, int|string $key): ?object
3030
return TypeAssert::objectOrNull(self::get($array, $key));
3131
}
3232

33+
public static function arrayOrObject(mixed $array, int|string $key): array|object
34+
{
35+
return TypeAssert::arrayOrObject(self::get($array, $key));
36+
}
37+
38+
public static function arrayOrObjectOrNull(mixed $array, int|string $key): array|object|null
39+
{
40+
return TypeAssert::arrayOrObjectOrNull(self::get($array, $key));
41+
}
42+
3343
public static function string(mixed $array, int|string $key): string
3444
{
3545
return TypeAssert::string(self::get($array, $key));
@@ -90,6 +100,16 @@ public static function callableOrNull(mixed $array, int|string $key): ?callable
90100
return TypeAssert::callableOrNull(self::get($array, $key));
91101
}
92102

103+
public static function iterable(mixed $array, int|string $key): iterable
104+
{
105+
return TypeAssert::iterable(self::get($array, $key));
106+
}
107+
108+
public static function iterableOrNull(mixed $array, int|string $key): ?iterable
109+
{
110+
return TypeAssert::iterableOrNull(self::get($array, $key));
111+
}
112+
93113
public static function scalar(mixed $array, int|string $key): int|float|string|bool
94114
{
95115
return TypeAssert::scalar(self::get($array, $key));

src/Mixins/TypeAssertTrait.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ public static function objectOrNull(mixed $value): ?object
4646
return $value;
4747
}
4848

49+
public static function arrayOrObject(mixed $value): array|object
50+
{
51+
if (!is_array($value) && !is_object($value)) {
52+
throw new AssertionFailedException(self::createErrorMessage($value, 'array|object'));
53+
}
54+
55+
return $value;
56+
}
57+
58+
public static function arrayOrObjectOrNull(mixed $value): array|object|null
59+
{
60+
if (!is_array($value) && !is_object($value) && $value !== null) {
61+
throw new AssertionFailedException(self::createErrorMessage($value, 'array|object|null'));
62+
}
63+
64+
return $value;
65+
}
66+
4967
public static function string(mixed $value): string
5068
{
5169
if (!is_string($value)) {
@@ -154,6 +172,24 @@ public static function callableOrNull(mixed $value): ?callable
154172
return $value;
155173
}
156174

175+
public static function iterable(mixed $value): iterable
176+
{
177+
if (!is_iterable($value)) {
178+
throw new AssertionFailedException(self::createErrorMessage($value, 'iterable'));
179+
}
180+
181+
return $value;
182+
}
183+
184+
public static function iterableOrNull(mixed $value): ?iterable
185+
{
186+
if (!is_iterable($value) && $value !== null) {
187+
throw new AssertionFailedException(self::createErrorMessage($value, 'iterable|null'));
188+
}
189+
190+
return $value;
191+
}
192+
157193
public static function scalar(mixed $value): int|float|string|bool
158194
{
159195
if (!is_scalar($value)) {

tests/TypeAssert.test.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function hasType(array $array, string $type): bool
1515
return Arrays::some($array, fn (string $name) => $name === $type);
1616
}
1717

18-
function type(mixed $value, bool $callable) {
18+
function type(mixed $value, bool $callable, bool $iterable) {
1919
static $converts = [
2020
'boolean' => 'bool',
2121
'integer' => 'int',
@@ -32,6 +32,10 @@ function type(mixed $value, bool $callable) {
3232
return 'callable';
3333
}
3434

35+
if ($iterable && is_iterable($value)) {
36+
return 'iterable';
37+
}
38+
3539
return $converts[gettype($value)];
3640
}
3741

@@ -50,7 +54,7 @@ function testVariants(callable $callback, string $type) {
5054
$type = Type::fromString($type);
5155

5256
foreach ($values as $value) {
53-
$valueType = type($value, hasType($type->getNames(), 'callable'));
57+
$valueType = type($value, hasType($type->getNames(), 'callable'), hasType($type->getNames(), 'iterable'));
5458
if (!$type->allows($valueType)) {
5559
Assert::exception(fn () => $callback($value), AssertionFailedException::class);
5660
} else {

0 commit comments

Comments
 (0)