|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace AlecRabbit\Spinner\Core; |
|
4
|
|
|
|
|
5
|
|
|
use AlecRabbit\Spinner\Core\Contracts\OutputInterface; |
|
6
|
|
|
use AlecRabbit\Spinner\Settings\Contracts\Defaults; |
|
7
|
|
|
use AlecRabbit\Spinner\Settings\Settings; |
|
8
|
|
|
use function AlecRabbit\typeOf; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Sentinel. |
|
12
|
|
|
* Contains data asserts |
|
13
|
|
|
* @internal |
|
14
|
|
|
*/ |
|
15
|
|
|
class Sentinel |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param array $frames |
|
19
|
|
|
*/ |
|
20
|
39 |
|
public static function assertFrames(array $frames): void |
|
21
|
|
|
{ |
|
22
|
39 |
|
if (Defaults::MAX_FRAMES_COUNT < $count = count($frames)) { |
|
23
|
3 |
|
throw new \InvalidArgumentException( |
|
24
|
3 |
|
sprintf('MAX_SYMBOLS_COUNT limit [%s] exceeded: [%s].', Defaults::MAX_FRAMES_COUNT, $count) |
|
25
|
|
|
); |
|
26
|
|
|
} |
|
27
|
36 |
|
foreach ($frames as $frame) { |
|
28
|
30 |
|
self::assertFrame($frame); |
|
29
|
|
|
} |
|
30
|
27 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param mixed $frame |
|
34
|
|
|
*/ |
|
35
|
30 |
|
public static function assertFrame($frame): void |
|
36
|
|
|
{ |
|
37
|
30 |
|
if (!\is_string($frame)) { |
|
38
|
7 |
|
throw new \InvalidArgumentException('All frames should be of string type.'); |
|
39
|
|
|
} |
|
40
|
29 |
|
self::assertFrameLength($frame); |
|
41
|
29 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $frame |
|
45
|
|
|
*/ |
|
46
|
29 |
|
public static function assertFrameLength(string $frame): void |
|
47
|
|
|
{ |
|
48
|
29 |
|
if (Defaults::MAX_FRAME_LENGTH < $length = mb_strwidth($frame)) { |
|
49
|
2 |
|
throw new \InvalidArgumentException( |
|
50
|
2 |
|
sprintf( |
|
51
|
2 |
|
'Single frame max length [%s] exceeded [%s]', |
|
52
|
2 |
|
Defaults::MAX_FRAME_LENGTH, |
|
53
|
2 |
|
$length |
|
54
|
|
|
) |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
29 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param array $styles |
|
61
|
|
|
* @param array $against |
|
62
|
|
|
*/ |
|
63
|
27 |
|
public static function assertStyles(array $styles, array $against): void |
|
64
|
|
|
{ |
|
65
|
27 |
|
$keys = array_keys($against); |
|
66
|
27 |
|
foreach ($keys as $index) { |
|
67
|
27 |
|
if (!\array_key_exists($index, $styles)) { |
|
68
|
|
|
// @codeCoverageIgnoreStart |
|
69
|
|
|
throw new \InvalidArgumentException( |
|
70
|
|
|
'Styles array does not have [' . $index . '] key.' |
|
71
|
|
|
); |
|
72
|
|
|
// @codeCoverageIgnoreEnd |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
27 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param mixed $output |
|
79
|
|
|
*/ |
|
80
|
29 |
|
public static function assertOutput($output): void |
|
81
|
|
|
{ |
|
82
|
29 |
|
if (null !== $output && false !== $output && !$output instanceof OutputInterface) { |
|
83
|
|
|
$typeOrValue = |
|
84
|
2 |
|
true === $output ? 'true' : typeOf($output); |
|
85
|
2 |
|
throw new \InvalidArgumentException( |
|
86
|
|
|
'Incorrect parameter: ' . |
|
87
|
|
|
'[null|false|' . OutputInterface::class . '] expected' |
|
88
|
2 |
|
. ' "' . $typeOrValue . '" given.' |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
27 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param mixed $settings |
|
95
|
|
|
*/ |
|
96
|
27 |
|
public static function assertSettings($settings): void |
|
97
|
|
|
{ |
|
98
|
27 |
|
if (null !== $settings && !\is_string($settings) && !$settings instanceof Settings) { |
|
99
|
1 |
|
throw new \InvalidArgumentException( |
|
100
|
1 |
|
'Instance of [' . Settings::class . '] or string expected ' . typeOf($settings) . ' given.' |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
26 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param array $order |
|
107
|
|
|
*/ |
|
108
|
3 |
|
public static function assertJugglersOrder(array $order): void |
|
109
|
|
|
{ |
|
110
|
3 |
|
if (Defaults::NUMBER_OF_ORDER_DIRECTIVES !== $count = count($order)) { |
|
111
|
1 |
|
throw new \InvalidArgumentException( |
|
112
|
1 |
|
sprintf( |
|
113
|
1 |
|
'Incorrect count of order directives [%s] when exactly %s was expected', |
|
114
|
1 |
|
$count, |
|
115
|
1 |
|
Defaults::NUMBER_OF_ORDER_DIRECTIVES |
|
116
|
|
|
) |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
2 |
|
foreach (Defaults::DEFAULT_ORDER_DIRECTIVES as $directive) { |
|
120
|
2 |
|
if (!in_array($directive, $order, true)) { |
|
121
|
1 |
|
throw new \InvalidArgumentException( |
|
122
|
1 |
|
sprintf( |
|
123
|
1 |
|
'Directive for %s position not found', |
|
124
|
1 |
|
Defaults::DIRECTIVES_NAMES[$directive] |
|
125
|
|
|
) |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
1 |
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|