Color::get()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 5
cts 6
cp 0.8333
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.0416
1
<?php
2
3
namespace League\CLImate\Decorator\Component;
4
5
class Color extends BaseDecorator
6
{
7
    /**
8
     * The available colors
9
     *
10
     * @var array
11
     */
12
    protected $colors = [];
13
14
    /**
15
     * An array of default colors
16
     *
17
     * @var array $defaults
18
     */
19
    protected $defaults = [
20
            'default'       => 39,
21
            'black'         => 30,
22
            'red'           => 31,
23
            'green'         => 32,
24
            'yellow'        => 33,
25
            'blue'          => 34,
26
            'magenta'       => 35,
27
            'cyan'          => 36,
28
            'light_gray'    => 37,
29
            'dark_gray'     => 90,
30
            'light_red'     => 91,
31
            'light_green'   => 92,
32
            'light_yellow'  => 93,
33
            'light_blue'    => 94,
34
            'light_magenta' => 95,
35
            'light_cyan'    => 96,
36
            'white'         => 97,
37
        ];
38
39
    /**
40
     * Add a color into the mix
41
     *
42
     * @param string  $key
43
     * @param integer $value
44
     */
45 948
    public function add($key, $value)
46
    {
47 948
        $this->colors[$key] = (int) $value;
48 948
    }
49
50
    /**
51
     * Retrieve all of available colors
52
     *
53
     * @return array
54
     */
55 576
    public function all()
56
    {
57 576
        return $this->colors;
58
    }
59
60
    /**
61
     * Get the code for the color
62
     *
63
     * @param  string  $val
64
     *
65
     * @return string
66
     */
67 576
    public function get($val)
68
    {
69
        // If we already have the code, just return that
70 576
        if (is_numeric($val)) {
71
            return $val;
72
        }
73
74 576
        if (array_key_exists($val, $this->colors)) {
75 576
            return $this->colors[$val];
76
        }
77
78 56
        return null;
79
    }
80
81
    /**
82
     * Set the current color
83
     *
84
     * @param  string   $val
85
     *
86
     * @return boolean
87
     */
88 92
    public function set($val)
89
    {
90 92
        $code = $this->get($val);
91
92 92
        if ($code) {
93 92
            $this->current = [$code];
94
95 92
            return true;
96
        }
97
98 52
        return false;
99
    }
100
}
101