1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
// 15.02.23 |
5
|
|
|
|
6
|
|
|
namespace AlecRabbit\Spinner\Helper; |
7
|
|
|
|
8
|
|
|
use AlecRabbit\Spinner\Contract\ColorMode; |
9
|
|
|
use AlecRabbit\Spinner\Exception\InvalidArgumentException; |
10
|
|
|
use AlecRabbit\Spinner\Exception\RuntimeException; |
11
|
|
|
use Traversable; |
12
|
|
|
|
13
|
|
|
use function class_exists; |
14
|
|
|
use function extension_loaded; |
15
|
|
|
|
16
|
|
|
final class Asserter |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param object|class-string $c |
|
|
|
|
20
|
|
|
* @param class-string $i |
21
|
|
|
* @param string|null $callerMethod |
22
|
|
|
* @param bool $allowString |
23
|
|
|
* @throws InvalidArgumentException |
24
|
|
|
*/ |
25
|
|
|
public static function isSubClass( |
26
|
|
|
object|string $c, |
27
|
|
|
string $i, |
28
|
|
|
?string $callerMethod = null, |
29
|
|
|
bool $allowString = true |
30
|
|
|
): void { |
31
|
|
|
if (!is_subclass_of($c, $i, $allowString)) { |
32
|
|
|
throw new InvalidArgumentException( |
33
|
|
|
sprintf( |
34
|
|
|
'Class "%s" must be a subclass of "%s"%s.', |
35
|
|
|
is_object($c) ? get_class($c) : $c, |
36
|
|
|
$i, |
37
|
|
|
$callerMethod ? sprintf(', see "%s()"', $callerMethod) : '', |
38
|
|
|
) |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @throws InvalidArgumentException |
45
|
|
|
*/ |
46
|
|
|
public static function assertStream(mixed $stream): void |
47
|
|
|
{ |
48
|
|
|
/** @psalm-suppress DocblockTypeContradiction */ |
49
|
|
|
if (!is_resource($stream) || 'stream' !== get_resource_type($stream)) { |
50
|
|
|
throw new InvalidArgumentException( |
51
|
|
|
sprintf('Argument is expected to be a stream(resource), "%s" given.', get_debug_type($stream)) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @throws InvalidArgumentException |
58
|
|
|
*/ |
59
|
|
|
public static function assertColorModes(Traversable $colorModes): void |
60
|
|
|
{ |
61
|
|
|
if (0 === count(iterator_to_array($colorModes))) { |
62
|
|
|
throw new InvalidArgumentException('Color modes must not be empty.'); |
63
|
|
|
} |
64
|
|
|
/** @var ColorMode $colorMode */ |
65
|
|
|
foreach ($colorModes as $colorMode) { |
66
|
|
|
if (!$colorMode instanceof ColorMode) { |
67
|
|
|
throw new InvalidArgumentException( |
68
|
|
|
sprintf( |
69
|
|
|
'Unsupported color mode of type "%s".', |
70
|
|
|
get_debug_type($colorMode) |
71
|
|
|
) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @throws RuntimeException |
79
|
|
|
*/ |
80
|
|
|
public static function assertExtensionLoaded(string $extensionName, ?string $message = null): void |
81
|
|
|
{ |
82
|
|
|
if (!extension_loaded($extensionName)) { |
83
|
|
|
throw new RuntimeException( |
84
|
|
|
$message ?? sprintf('Extension "%s" is not loaded.', $extensionName) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @throws InvalidArgumentException |
91
|
|
|
*/ |
92
|
|
|
public static function assertClassExists(string $class, ?string $callerMethod = null): void |
93
|
|
|
{ |
94
|
|
|
if (!class_exists($class)) { |
95
|
|
|
throw new InvalidArgumentException( |
96
|
|
|
sprintf( |
97
|
|
|
'Class "%s" does not exist%s.', |
98
|
|
|
$class, |
99
|
|
|
$callerMethod ? sprintf(', see "%s()"', $callerMethod) : '' |
100
|
|
|
) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|