1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Spinner\Core; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Spinner\Core\Adapters\EchoOutputAdapter; |
6
|
|
|
use AlecRabbit\Spinner\Core\Contracts\Frames; |
7
|
|
|
use AlecRabbit\Spinner\Core\Contracts\SpinnerInterface; |
8
|
|
|
use AlecRabbit\Spinner\Core\Contracts\SpinnerOutputInterface; |
9
|
|
|
use AlecRabbit\Spinner\Core\Contracts\StylesInterface; |
10
|
|
|
use AlecRabbit\Spinner\Settings\Contracts\Defaults; |
11
|
|
|
use function AlecRabbit\typeOf; |
12
|
|
|
|
13
|
|
|
abstract class SpinnerCore implements SpinnerInterface |
14
|
|
|
{ |
15
|
|
|
protected const EMPTY_STRING = Defaults::EMPTY_STRING; |
16
|
|
|
|
17
|
|
|
protected const INTERVAL = Defaults::DEFAULT_INTERVAL; |
18
|
|
|
protected const FRAMES = Frames::BASE; |
19
|
|
|
protected const STYLES = StylesInterface::STYLING_DISABLED; |
20
|
|
|
|
21
|
|
|
/** @var null|SpinnerOutputInterface */ |
22
|
|
|
protected $output; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param null|false|SpinnerOutputInterface $output |
26
|
|
|
* @return null|SpinnerOutputInterface |
27
|
|
|
*/ |
28
|
29 |
|
protected function refineOutput($output): ?SpinnerOutputInterface |
29
|
|
|
{ |
30
|
29 |
|
$this->assertOutput($output); |
31
|
27 |
|
if (false === $output) { |
32
|
22 |
|
return null; |
33
|
|
|
} |
34
|
5 |
|
return $output ?? new EchoOutputAdapter(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param mixed $output |
39
|
|
|
*/ |
40
|
29 |
|
protected function assertOutput($output): void |
41
|
|
|
{ |
42
|
29 |
|
if (null !== $output && false !== $output && !$output instanceof SpinnerOutputInterface) { |
43
|
|
|
$typeOrValue = |
44
|
2 |
|
true === $output ? 'true' : typeOf($output); |
45
|
2 |
|
throw new \InvalidArgumentException( |
46
|
|
|
'Incorrect parameter: ' . |
47
|
|
|
'[null|false|' . SpinnerOutputInterface::class . '] expected' |
48
|
2 |
|
. ' "' . $typeOrValue . '" given.' |
49
|
|
|
); |
50
|
|
|
} |
51
|
27 |
|
} |
52
|
|
|
} |
53
|
|
|
|