Completed
Push — master ( c4e3cd...c5cae6 )
by Anton
05:38
created

Style::style()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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