Skip to content

Commit 02d242a

Browse files
committed
add classString, classStringOrNull, classStringOf, classStringOfOrNull
1 parent b41777b commit 02d242a

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

src/Traits/ArrayTypeAssertTrait.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,46 @@
77
trait ArrayTypeAssertTrait
88
{
99

10+
/**
11+
* @template T of object
12+
* @param mixed[] $array
13+
* @param class-string<T> $type
14+
* @return class-string<T>
15+
*/
16+
public static function classStringOf(array $array, string|int $key, string $type): string
17+
{
18+
return TypeAssert::classStringOf(self::get($array, $key), $type);
19+
}
20+
21+
/**
22+
* @template T of object
23+
* @param mixed[] $array
24+
* @param class-string<T> $type
25+
* @return class-string<T>|null
26+
*/
27+
public static function classStringOfOrNull(array $array, string|int $key, string $type): ?string
28+
{
29+
return TypeAssert::classStringOfOrNull(self::get($array, $key), $type);
30+
}
31+
32+
/**
33+
* @param mixed[] $array
34+
* @return class-string
35+
*/
36+
public static function classString(array $array, string|int $key): string
37+
{
38+
return TypeAssert::classString(self::get($array, $key));
39+
}
40+
41+
/**
42+
* @param mixed[] $array
43+
* @return class-string|null
44+
*/
45+
public static function classStringOrNull(array $array, string|int $key): ?string
46+
{
47+
return TypeAssert::classStringOrNull(self::get($array, $key));
48+
}
49+
1050
/**
1151
* @template T of object
1252
* @param mixed[] $array

src/Traits/TypeAssertTrait.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,58 @@
99
trait TypeAssertTrait
1010
{
1111

12+
/**
13+
* @template T of object
14+
* @param class-string<T> $type
15+
* @return class-string<T>
16+
*/
17+
public static function classStringOf(mixed $value, string $type): string
18+
{
19+
if (!is_string($value) || !class_exists($value) || !is_a($value, $type, true)) {
20+
throw new AssertionFailedException(self::createErrorMessage($value, sprintf('class-string<%s>', $type)));
21+
}
22+
23+
return $value;
24+
}
25+
26+
/**
27+
* @template T of object
28+
* @param class-string<T> $type
29+
* @return class-string<T>|null
30+
*/
31+
public static function classStringOfOrNull(mixed $value, string $type): ?string
32+
{
33+
if ($value !== null && (!is_string($value) || !class_exists($value) || !is_a($value, $type, true))) {
34+
throw new AssertionFailedException(self::createErrorMessage($value, sprintf('class-string<%s>', $type)));
35+
}
36+
37+
return $value;
38+
}
39+
40+
/**
41+
* @return class-string
42+
*/
43+
public static function classString(mixed $value): string
44+
{
45+
if (!is_string($value) || !class_exists($value)) {
46+
throw new AssertionFailedException(self::createErrorMessage($value, 'class-string'));
47+
}
48+
49+
return $value;
50+
}
51+
52+
/**
53+
* @return class-string|null
54+
*/
55+
public static function classStringOrNull(mixed $value): ?string
56+
{
57+
if ($value !== null && (!is_string($value) || !class_exists($value))) {
58+
throw new AssertionFailedException(self::createErrorMessage($value, 'class-string|null'));
59+
}
60+
61+
return $value;
62+
}
63+
1264
/**
1365
* @template T of object
1466
* @param class-string<T> $type

tests/TypeAssert.classString.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Tester\Assert;
4+
use Utilitte\Asserts\Exceptions\AssertionFailedException;
5+
use Utilitte\Asserts\TypeAssert;
6+
7+
require __DIR__ . '/_bootstrap.php';
8+
9+
class A {}
10+
11+
class B extends A {}
12+
13+
TypeAssert::classString(A::class);
14+
TypeAssert::classStringOrNull(null);
15+
16+
Assert::exception(fn () => TypeAssert::classString('foo'), AssertionFailedException::class);
17+
18+
TypeAssert::classStringOf(A::class, A::class);
19+
TypeAssert::classStringOfOrNull(null, A::class);
20+
21+
Assert::exception(fn () => TypeAssert::classStringOf(A::class, B::class), AssertionFailedException::class);

0 commit comments

Comments
 (0)