Style   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
dl 0
loc 135
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A wrapContainer() 0 4 1
A indent() 0 8 2
A apply() 0 11 2
A getStyle() 0 16 5
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Debug\Dumper;
10
11
/**
12
 * Dump Styler responsible for dump styling.
13
 */
14
class Style
15
{
16
    /**
17
     * Container element used to inject dump into, usually pre elemnt with some styling.
18
     *
19
     * @var string
20
     */
21
    protected $container = '<pre style="background-color: white; font-family: monospace;">{dump}</pre>';
22
23
    /**
24
     * Every dumped element is wrapped using this pattern.
25
     *
26
     * @var string
27
     */
28
    protected $element = '<span style="{style};">{element}</span>';
29
30
    /**
31
     * Default indent string.
32
     *
33
     * @var string
34
     */
35
    protected $indent = '&middot;    ';
36
37
    /**
38
     * Set of styles associated with different dumping properties.
39
     *
40
     * @var array
41
     */
42
    protected $styles = [
43
        'common'   => 'color: black',
44
        'name'     => 'color: black',
45
        'dynamic'  => 'color: purple;',
46
        'maxLevel' => 'color: #ff9900',
47
        'syntax'   => [
48
            'common' => 'color: #666',
49
            '['      => 'color: black',
50
            ']'      => 'color: black',
51
            '('      => 'color: black',
52
            ')'      => 'color: black',
53
        ],
54
        'value'    => [
55
            'string'  => 'color: green',
56
            'integer' => 'color: red',
57
            'double'  => 'color: red',
58
            'boolean' => 'color: purple; font-weight: bold;',
59
        ],
60
        'type'     => [
61
            'common'   => 'color: #666',
62
            'object'   => 'color: #333',
63
            'array'    => 'color: #333',
64
            'null'     => 'color: #666; font-weight: bold;',
65
            'resource' => 'color: #666; font-weight: bold;',
66
        ],
67
        'access'   => [
68
            'common'    => 'color: #666',
69
            'public'    => 'color: #8dc17d',
70
            'private'   => 'color: #c18c7d',
71
            'protected' => 'color: #7d95c1',
72
        ],
73
    ];
74
75
    /**
76
     * Inject dumped value into dump container.
77
     *
78
     * @param string $dump
79
     *
80
     * @return string
81
     */
82 11
    public function wrapContainer(string $dump): string
83
    {
84 11
        return \Spiral\interpolate($this->container, compact('dump'));
85
    }
86
87
    /**
88
     * Set indent to line based on it's level.
89
     *
90
     * @param int $level
91
     *
92
     * @return string
93
     */
94 11
    public function indent(int $level): string
95
    {
96 11
        if ($level == 0) {
97 11
            return '';
98
        }
99
100 6
        return $this->apply(str_repeat($this->indent, $level), 'indent');
101
    }
102
103
    /**
104
     * Stylize content using pre-defined style.
105
     *
106
     * @param string|null $element
107
     * @param string      $type
108
     * @param string      $context
109
     *
110
     * @return string
111
     */
112 11
    public function apply($element, string $type, string $context = ''): string
113
    {
114 11
        if (!empty($style = $this->getStyle($type, $context))) {
115 11
            return \Spiral\interpolate(
116 11
                $this->element,
117 11
                compact('style', 'element')
118
            );
119
        }
120
121
        return $element;
122
    }
123
124
    /**
125
     * Get valid stype based on type and context/.
126
     *
127
     * @param string $type
128
     * @param string $context
129
     *
130
     * @return string
131
     */
132 11
    private function getStyle(string $type, string $context): string
133
    {
134 11
        if (isset($this->styles[$type][$context])) {
135 11
            return $this->styles[$type][$context];
136
        }
137
138 11
        if (isset($this->styles[$type]['common'])) {
139 11
            return $this->styles[$type]['common'];
140
        }
141
142 6
        if (isset($this->styles[$type]) && is_string($this->styles[$type])) {
143 6
            return $this->styles[$type];
144
        }
145
146 6
        return $this->styles['common'];
147
    }
148
}
149