Passed
Push — master ( 67aa31...d47bdf )
by Kirill
03:20
created

ConsoleRenderer::apply()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 3
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Debug\Renderer;
13
14
use Codedungeon\PHPCliColors\Color;
15
16
/**
17
 * Colorful styling for CLI dumps.
18
 */
19
final class ConsoleRenderer extends AbstractRenderer
20
{
21
    /**
22
     * Every dumped element is wrapped using this pattern.
23
     *
24
     * @var string
25
     */
26
    protected $element = '%s%s' . Color::RESET;
27
28
    /**
29
     * Set of styles associated with different dumping properties.
30
     *
31
     * @var array
32
     */
33
    protected $styles = [
34
        'common'   => Color::BOLD_WHITE,
35
        'name'     => Color::LIGHT_WHITE,
36
        'dynamic'  => Color::PURPLE,
37
        'maxLevel' => Color::RED,
38
        'syntax'   => [
39
            'common' => Color::WHITE,
40
            '['      => Color::BOLD_WHITE,
41
            ']'      => Color::BOLD_WHITE,
42
            '('      => Color::BOLD_WHITE,
43
            ')'      => Color::BOLD_WHITE,
44
        ],
45
        'value'    => [
46
            'string'  => Color::GREEN,
47
            'integer' => Color::LIGHT_CYAN,
48
            'double'  => Color::LIGHT_CYAN,
49
            'boolean' => Color::LIGHT_PURPLE,
50
        ],
51
        'type'     => [
52
            'common'   => Color::WHITE,
53
            'object'   => Color::LIGHT_BLUE,
54
            'null'     => Color::LIGHT_PURPLE,
55
            'resource' => Color::PURPLE,
56
        ],
57
        'access'   => Color::GRAY
58
    ];
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function apply($element, string $type, string $context = ''): string
64
    {
65
        if (!empty($style = $this->getStyle($type, $context))) {
66
            return sprintf($this->element, $style, $element);
67
        }
68
69
        return $element;
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public function escapeStrings(): bool
76
    {
77
        return false;
78
    }
79
80
    /**
81
     * Get valid style based on type and context/.
82
     *
83
     * @param string $type
84
     * @param string $context
85
     *
86
     * @return string
87
     */
88
    private function getStyle(string $type, string $context): string
89
    {
90
        if (isset($this->styles[$type][$context])) {
91
            return $this->styles[$type][$context];
92
        }
93
94
        if (isset($this->styles[$type]['common'])) {
95
            return $this->styles[$type]['common'];
96
        }
97
98
        if (isset($this->styles[$type]) && is_string($this->styles[$type])) {
99
            return $this->styles[$type];
100
        }
101
102
        return $this->styles['common'];
103
    }
104
}
105