Passed
Push — main ( 56908f...4059b3 )
by ANDREY
02:30
created

Symbol::setMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace VPA\Console;
4
5
class Symbol
6
{
7
    private string $code;
8
    private int $mode = SymbolMode::DEFAULT;
9
    private int $color = Color::WHITE;
10
    private int $backgroundColor = Color::BLACK;
11
12 58
    public function __construct(protected string $string)
13
    {
14 58
        $this->code = dechex(ord($string));
15
    }
16
17 32
    public function setMode(int $mode = SymbolMode::DEFAULT): void
18
    {
19 32
        $this->mode = $mode;
20
    }
21
22 32
    public function setColor(int $color = Color::WHITE): void
23
    {
24 32
        $this->color = $color;
25
    }
26
27 32
    public function setBackgroundColor(int $color = Color::BLACK): void
28
    {
29 32
        $this->backgroundColor = $color;
30
    }
31
32 32
    public function setConfig(array $config): Symbol
33
    {
34 32
        $this->setMode($config['mode'] ?? $this->mode);
35 32
        $this->setColor($config['color'] ?? $this->color);
36 32
        $this->setBackgroundColor($config['backgroundColor'] ?? $this->backgroundColor);
37 32
        return $this;
38
    }
39
40 9
    public function getCode(): string
41
    {
42 9
        return $this->code;
43
    }
44
45 14
    public function getAlias(): string
46
    {
47 14
        return '';
48
    }
49
50 5
    public function __toString(): string
51
    {
52 5
        list($start, $end) = $this->colorizeText();
53 5
        return sprintf("%s%s%s", $start, $this->string, $end);
54
    }
55
56 3
    public function equalsCode(string $hexCode): bool
57
    {
58 3
        return $this->code === $hexCode;
59
    }
60
61 4
    public function equals(string $symbol): bool
62
    {
63 4
        return $this->string === $symbol;
64
    }
65
66 8
    public function is(Symbol $symbol): bool
67
    {
68 8
        return $this->code === $symbol->getCode();
69
    }
70
71 6
    protected function colorizeText(): array
72
    {
73 6
        $start = $end = "";
74
        if (
75 6
            $this->mode != SymbolMode::DEFAULT
76 6
            || $this->color != Color::WHITE
77 6
            || $this->backgroundColor != Color::BLACK
78
        ) {
79
            $start = sprintf("\033[%s", $this->mode);
80
            $end = "\033[0m";
81
        }
82 6
        if ($this->backgroundColor != Color::BLACK) {
83
            $start .= sprintf(";3%s;4%sm", $this->color, $this->backgroundColor);
84 6
        } elseif ($start) {
85
            $start .= sprintf(";3%sm", $this->color);
86
        }
87 6
        return [$start, $end];
88
    }
89
}
90