|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
namespace AlecRabbit\Spinner\Extras\Pattern; |
|
7
|
|
|
|
|
8
|
|
|
use AlecRabbit\Spinner\Contract\Option\OptionStyleMode; |
|
9
|
|
|
use AlecRabbit\Spinner\Core\Pattern\A\AStylePattern; |
|
10
|
|
|
use AlecRabbit\Spinner\Exception\InvalidArgumentException; |
|
11
|
|
|
use Traversable; |
|
12
|
|
|
|
|
13
|
|
|
/** @psalm-suppress UnusedClass */ |
|
14
|
|
|
final class CustomStylePattern extends AStylePattern |
|
15
|
|
|
{ |
|
16
|
|
|
public function __construct( |
|
17
|
|
|
protected array $pattern, |
|
18
|
|
|
?int $interval = null, |
|
19
|
|
|
bool $reversed = false, |
|
20
|
|
|
) { |
|
21
|
|
|
self::assertPattern($pattern); |
|
22
|
|
|
parent::__construct( |
|
23
|
|
|
interval: $interval, |
|
24
|
|
|
reversed: $reversed, |
|
25
|
|
|
); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @throws InvalidArgumentException |
|
30
|
|
|
*/ |
|
31
|
|
|
private static function assertPattern(array $pattern): void |
|
32
|
|
|
{ |
|
33
|
|
|
if (empty($pattern)) { |
|
34
|
|
|
throw new InvalidArgumentException('Pattern is empty.'); |
|
35
|
|
|
} |
|
36
|
|
|
if (!array_key_exists(OptionStyleMode::ANSI4->value, $pattern)) { |
|
37
|
|
|
throw new InvalidArgumentException('Pattern does not contain ANSI4 key.'); |
|
38
|
|
|
} |
|
39
|
|
|
if (!array_key_exists(OptionStyleMode::ANSI8->value, $pattern)) { |
|
40
|
|
|
throw new InvalidArgumentException('Pattern does not contain ANSI8 key.'); |
|
41
|
|
|
} |
|
42
|
|
|
if (!array_key_exists(OptionStyleMode::ANSI24->value, $pattern)) { |
|
43
|
|
|
throw new InvalidArgumentException('Pattern does not contain ANSI24 key.'); |
|
44
|
|
|
} |
|
45
|
|
|
self::assertPatternSection($pattern[OptionStyleMode::ANSI4->value]); |
|
46
|
|
|
self::assertPatternSection($pattern[OptionStyleMode::ANSI8->value]); |
|
47
|
|
|
self::assertPatternSection($pattern[OptionStyleMode::ANSI24->value]); |
|
48
|
|
|
// match (true) { |
|
49
|
|
|
// empty($pattern) => throw new InvalidArgumentException('Pattern is empty.'), |
|
50
|
|
|
// !array_key_exists(OptionStyleMode::ANSI4->value, $pattern) |
|
51
|
|
|
// => |
|
52
|
|
|
// throw new InvalidArgumentException('Pattern does not contain ANSI4 key.'), |
|
53
|
|
|
// !array_key_exists(OptionStyleMode::ANSI8->value, $pattern) |
|
54
|
|
|
// => |
|
55
|
|
|
// throw new InvalidArgumentException('Pattern does not contain ANSI8 key.'), |
|
56
|
|
|
// !array_key_exists(OptionStyleMode::ANSI24->value, $pattern) |
|
57
|
|
|
// => |
|
58
|
|
|
// throw new InvalidArgumentException('Pattern does not contain ANSI24 key.'), |
|
59
|
|
|
// }; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private static function assertPatternSection(mixed $value): void |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getEntries(OptionStyleMode $styleMode = OptionStyleMode::ANSI8): Traversable |
|
67
|
|
|
{ |
|
68
|
|
|
yield from $this->pattern[$styleMode->value]; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.