EscapeAttribute::equals()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 1
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Matks\Vivian\Output;
4
5
use Exception;
6
7
/**
8
 * ANSI/VT100 Escape attribute
9
 */
10
abstract class EscapeAttribute
11
{
12
    const ANSI_ESCAPE_CODE_BEGIN = "\033[";
13
    const ANSI_ESCAPE_CODE_END   = 'm';
14
15
    /**
16
     * @var string
17
     */
18
    private $escapeCharacter;
19
20
    /**
21
     * @param string $code
22
     */
23
    public function __construct($code)
24
    {
25 1
        if (!in_array($code, $this->getAllowedEscapeCodes())) {
26 1
            throw new Exception("Forbidden code $code");
27
        }
28
29 1
        $escapeCharacter       = $this->buildEscapeCharacter($code);
30 1
        $this->escapeCharacter = $escapeCharacter;
31 1
    }
32
33
    /**
34
     * @return string
35
     */
36
    public function getEscapeCharacter()
37
    {
38 1
        return $this->escapeCharacter;
39
    }
40
41
    /**
42
     * @param EscapeAttribute $escapeAttribute
43
     *
44
     * @return bool
45
     */
46
    public function equals(EscapeAttribute $escapeAttribute)
47
    {
48
        return ($escapeAttribute->getEscapeCharacter() === $this->getEscapeCharacter());
49
    }
50
51
    /**
52
     * Build escape character to be print
53
     *
54
     * @param string $escapeCode
55
     *
56
     * @return string
57
     */
58
    protected function buildEscapeCharacter($escapeCode)
59
    {
60 1
        $result = static::ANSI_ESCAPE_CODE_BEGIN . $escapeCode . static::ANSI_ESCAPE_CODE_END;
61
62 1
        return $result;
63
    }
64
65
    /**
66
     * Get allowed escape codes
67
     *
68
     * @return string[]
69
     */
70
    abstract protected function getAllowedEscapeCodes();
71
}
72