SyntaxHighlighterConfig::__construct()   B
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 11
nc 7
nop 1
1
<?php
2
3
namespace PhpSchool\PSX;
4
5
/**
6
 * Class SyntaxHighlighterConfig
7
 * @package PhpSchool\PSX
8
 * @author Aydin Hassan <[email protected]>
9
 */
10
class SyntaxHighlighterConfig
11
{
12
    const TYPE_KEYWORD          = 'keyword';
13
    const TYPE_BRACE            = 'brace';
14
    const TYPE_STRING           = 'string';
15
    const TYPE_CONSTRUCT        = 'construct';
16
    const TYPE_RETURN_NEW       = 'return_new';
17
    const TYPE_VAR_DEREF        = 'var_deref';
18
    const TYPE_CALL_PARENTHESIS = 'call_parenthesis';
19
    const TYPE_LHS              = 'lhs';
20
    const TYPE_CLASS            = 'class';
21
    const TYPE_OPEN_TAG         = 'open_tag';
22
23
    /**
24
     * @var array
25
     */
26
    private $types = [
27
        self::TYPE_KEYWORD,
28
        self::TYPE_BRACE,
29
        self::TYPE_STRING,
30
        self::TYPE_CONSTRUCT,
31
        self::TYPE_RETURN_NEW ,
32
        self::TYPE_VAR_DEREF,
33
        self::TYPE_CALL_PARENTHESIS,
34
        self::TYPE_LHS,
35
        self::TYPE_CLASS,
36
        self::TYPE_OPEN_TAG,
37
    ];
38
39
    /**
40
     * @var array
41
     */
42
    private $colors = [
43
        self::TYPE_KEYWORD          => Colours::BLUE,
44
        self::TYPE_BRACE            => Colours::YELLOW,
45
        self::TYPE_STRING           => Colours::GREEN,
46
        self::TYPE_CONSTRUCT        => Colours::YELLOW,
47
        self::TYPE_RETURN_NEW       => Colours::LIGHT_MAGENTA,
48
        self::TYPE_VAR_DEREF        => Colours::GREEN,
49
        self::TYPE_CALL_PARENTHESIS => Colours::LIGHT_GRAY,
50
        self::TYPE_LHS              => Colours::YELLOW,
51
        self::TYPE_CLASS            => Colours::LIGHT_GRAY,
52
        self::TYPE_OPEN_TAG         => Colours::CYAN,
53
    ];
54
55
    /**
56
     * @param array|null $colors
57
     */
58
    public function __construct(array $colors = null)
59
    {
60
        if (null !== $colors) {
61
            $types  = array_keys($colors);
62
            $diff   = array_diff($types, $this->types);
63
            if (count($diff)) {
64
                throw new \InvalidArgumentException(sprintf('Types: "%s" are not supported', implode('", "', $diff)));
65
            }
66
67
            $this->colors = array_merge($this->colors, $colors);
68
        }
69
70
        foreach ($this->colors as $colour) {
71
            if (!defined(Colours::class . "::" . strtoupper($colour))) {
72
                throw new \InvalidArgumentException(
73
                    sprintf('Colour: "%s" is not valid. Check: "%s"', $colour, Colours::class)
74
                );
75
            }
76
        }
77
    }
78
79
    /**
80
     * @param string $type
81
     *
82
     * @return string
83
     */
84
    public function getColorForType($type)
85
    {
86
        // if we don't have a type return default color
87
        if (!isset($this->colors[$type])) {
88
            return Colours::DEFAULT_;
89
        }
90
91
        return $this->colors[$type];
92
    }
93
}
94