1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Spinner\Core; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Accessories\Circular; |
6
|
|
|
use AlecRabbit\Spinner\Core\Contracts\StylesInterface; |
7
|
|
|
use AlecRabbit\Spinner\Settings\Contracts\Defaults; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Styles |
11
|
|
|
* |
12
|
|
|
* @internal |
13
|
|
|
*/ |
14
|
|
|
class Style |
15
|
|
|
{ |
16
|
|
|
/** @var Circular */ |
17
|
|
|
protected $spinnerStyles; |
18
|
|
|
/** @var Circular */ |
19
|
|
|
protected $messageStyles; |
20
|
|
|
/** @var Circular */ |
21
|
|
|
protected $percentStyles; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Styles constructor. |
25
|
|
|
* @param array $styles |
26
|
|
|
* @param mixed $colorSupport |
27
|
|
|
*/ |
28
|
28 |
|
public function __construct(array $styles, $colorSupport = null) |
29
|
|
|
{ |
30
|
28 |
|
$styles = $this->mergeStyles($styles); |
31
|
28 |
|
$coloring = new Coloring($styles, $colorSupport); |
32
|
27 |
|
$this->spinnerStyles = $coloring->getSpinnerStyles(); |
33
|
27 |
|
$this->messageStyles = $coloring->getMessageStyles(); |
34
|
27 |
|
$this->percentStyles = $coloring->getPercentStyles(); |
35
|
27 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $styles |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
28 |
|
protected function mergeStyles(array $styles): array |
42
|
|
|
{ |
43
|
28 |
|
$defaultStyles = StylesInterface::DEFAULT_STYLES; |
44
|
28 |
|
foreach ($defaultStyles as $key => $defaults) { |
45
|
28 |
|
if (\array_key_exists($key, $styles)) { |
46
|
|
|
/** @noinspection SlowArrayOperationsInLoopInspection */ |
47
|
25 |
|
$defaultStyles[$key] = array_merge(StylesInterface::DEFAULT_STYLES[$key], $styles[$key]); |
48
|
|
|
} |
49
|
|
|
} |
50
|
28 |
|
return $defaultStyles; |
51
|
|
|
} |
52
|
|
|
|
53
|
18 |
|
public function spinner(string $symbol): string |
54
|
|
|
{ |
55
|
|
|
return |
56
|
18 |
|
sprintf((string)$this->spinnerStyles->value(), $symbol); |
57
|
|
|
} |
58
|
|
|
|
59
|
18 |
|
public function message(string $message): string |
60
|
|
|
{ |
61
|
|
|
return |
62
|
18 |
|
sprintf((string)$this->messageStyles->value(), $message); |
63
|
|
|
} |
64
|
|
|
|
65
|
18 |
|
public function percent(string $percent): string |
66
|
|
|
{ |
67
|
|
|
return |
68
|
18 |
|
sprintf((string)$this->percentStyles->value(), $percent); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|