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

HtmlRenderer::indent()   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 1
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 Spiral\Debug\RendererInterface;
15
16
/**
17
 * HTML renderer with switchable color schemas.
18
 */
19
final class HtmlRenderer implements RendererInterface
20
{
21
    /**
22
     * Default coloring schema.
23
     */
24
    public const DEFAULT = [
25
        'body'     => '<pre style="background-color: white; font-family: monospace;">%s</pre>',
26
        'element'  => '<span style="%s;">%s</span>',
27
        'indent'   => '&middot;    ',
28
        'common'   => 'color: black',
29
        'name'     => 'color: black',
30
        'dynamic'  => 'color: purple;',
31
        'maxLevel' => 'color: #ff9900',
32
        'syntax'   => [
33
            'common' => 'color: #666',
34
            '['      => 'color: black',
35
            ']'      => 'color: black',
36
            '('      => 'color: black',
37
            ')'      => 'color: black',
38
        ],
39
        'value'    => [
40
            'string'  => 'color: green',
41
            'integer' => 'color: red',
42
            'double'  => 'color: red',
43
            'boolean' => 'color: purple; font-weight: bold;',
44
        ],
45
        'type'     => [
46
            'common'   => 'color: #666',
47
            'object'   => 'color: #333',
48
            'array'    => 'color: #333',
49
            'null'     => 'color: #666; font-weight: bold;',
50
            'resource' => 'color: #666; font-weight: bold;',
51
        ],
52
        'access'   => [
53
            'common'    => 'color: #666',
54
            'public'    => 'color: #8dc17d',
55
            'private'   => 'color: #c18c7d',
56
            'protected' => 'color: #7d95c1',
57
        ],
58
    ];
59
60
    /**
61
     * Inverted coloring schema.
62
     */
63
    public const INVERTED = [
64
        'body'     => '<pre style="background-color: #232323; font-family: Monospace;">%s</pre>',
65
        'element'  => '<span style="%s;">%s</span>',
66
        'indent'   => '&middot;    ',
67
        'common'   => 'color: #E6E1DC',
68
        'name'     => 'color: #E6E1DC',
69
        'dynamic'  => 'color: #7d95c1;',
70
        'maxLevel' => 'color: #ff9900',
71
        'syntax'   => [
72
            'common' => 'color: gray',
73
            '['      => 'color: #E6E1DC',
74
            ']'      => 'color: #E6E1DC',
75
            '('      => 'color: #E6E1DC',
76
            ')'      => 'color: #E6E1DC',
77
        ],
78
        'value'    => [
79
            'string'  => 'color: #A5C261',
80
            'integer' => 'color: #A5C261',
81
            'double'  => 'color: #A5C261',
82
            'boolean' => 'color: #C26230; font-weight: bold;',
83
        ],
84
        'type'     => [
85
            'common'   => 'color: #E6E1DC',
86
            'object'   => 'color: #E6E1DC',
87
            'array'    => 'color: #E6E1DC',
88
            'null'     => 'color: #C26230; font-weight: bold',
89
            'resource' => 'color: #C26230; font-weight: bold',
90
        ],
91
        'access'   => [
92
            'common'    => 'color: #666',
93
            'public'    => 'color: #8dc17d',
94
            'private'   => 'color: #c18c7d',
95
            'protected' => 'color: #7d95c1',
96
        ],
97
    ];
98
99
    /**
100
     * Set of styles associated with different dumping properties.
101
     *
102
     * @var array
103
     */
104
    protected $style = self::DEFAULT;
105
106
    /**
107
     * @param array $style
108
     */
109
    public function __construct(array $style = self::DEFAULT)
110
    {
111
        $this->style = $style;
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117
    public function apply($element, string $type, string $context = ''): string
118
    {
119
        if (!empty($style = $this->getStyle($type, $context))) {
0 ignored issues
show
Unused Code introduced by
The assignment to $style is dead and can be removed.
Loading history...
120
            if (!empty($style = $this->getStyle($type, $context))) {
121
                return sprintf($this->style['element'], $style, $element);
122
            }
123
        }
124
125
        return $element;
126
    }
127
128
    /**
129
     * @inheritdoc
130
     */
131
    public function wrapContent(string $body): string
132
    {
133
        return sprintf($this->style['body'], $body);
134
    }
135
136
    /**
137
     * @inheritdoc
138
     */
139
    public function indent(int $level): string
140
    {
141
        if ($level == 0) {
142
            return '';
143
        }
144
145
        return $this->apply(str_repeat($this->style['indent'], $level), 'indent');
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    public function escapeStrings(): bool
152
    {
153
        return true;
154
    }
155
156
    /**
157
     * Get valid stype based on type and context/.
158
     *
159
     * @param string $type
160
     * @param string $context
161
     *
162
     * @return string
163
     */
164
    private function getStyle(string $type, string $context): string
165
    {
166
        if (isset($this->style[$type][$context])) {
167
            return $this->style[$type][$context];
168
        }
169
170
        if (isset($this->style[$type]['common'])) {
171
            return $this->style[$type]['common'];
172
        }
173
174
        if (isset($this->style[$type]) && is_string($this->style[$type])) {
175
            return $this->style[$type];
176
        }
177
178
        return $this->style['common'];
179
    }
180
}
181