/
ValueRenderer.php
197 lines (173 loc) · 4.87 KB
/
ValueRenderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace Concise\Services;
use Closure;
use Concise\Console\Theme\DefaultTheme;
use Colors\Color;
use Concise\TestCase;
class ValueRenderer
{
/**
* @var string
*/
protected $theme;
/**
* @param $value
* @return string
*/
protected function colorizeLines($value)
{
$c = new Color();
$lines = explode("\n", $value);
$string = array();
foreach ($lines as $line) {
$string[] = (string) $c($line)->{$this->theme['value.string']};
}
return implode("\n", $string);
}
/**
* @param mixed $value
* @return string
*/
public function colorize($value)
{
$c = new Color();
if (!$this->theme) {
return (is_null($value) || is_bool($value)) ? json_encode($value) : (string) $value;
}
if (is_null($value)) {
return (string) $c('null')->{$this->theme['value.null']};
}
if (is_bool($value)) {
return (string) $c(json_encode($value))->{$this->theme['value.boolean']};
}
if (is_int($value)) {
return (string) $c($value)->{$this->theme['value.integer']};
}
if (is_float($value)) {
return (string) $c($value)->{$this->theme['value.float']};
}
if (is_resource($value)) {
return (string) $c((string) $value)->{$this->theme['value.string']};
}
return $this->colorizeLines($value);
}
/**
* @param array $values
* @param integer $depth
* @param Closure $callback
* @return string
*/
protected function jsonEncodeCallback(array $values, $depth, Closure $callback)
{
$r = '';
foreach ($values as $k => $v) {
if ($r) {
$r .= ",\n";
}
$r .= $this->createIndent($depth) . $callback($k, $v);
}
return $r;
}
/**
* @param integer $depth
* @return string
*/
protected function createIndent($depth)
{
return str_repeat(' ', $depth);
}
/**
* @param mixed $value
* @param integer $depth
* @return string
*/
protected function jsonEncode($value, $depth)
{
$self = $this;
if (is_object($value) || (is_array($value) && $this->isAssociativeArray($value))) {
$r = $this->jsonEncodeCallback((array) $value, $depth + 1, function ($k, $v) use ($depth, $self) {
return $self->colorize("\"$k\"") . ':' . $self->render($v, false, $depth + 1);
});
return "{\n" . $r . "\n" . $this->createIndent($depth) . "}";
}
/** @noinspection PhpUnusedParameterInspection */
$r = $this->jsonEncodeCallback((array) $value, $depth + 1, function ($k, $v) use ($depth, $self) {
return $self->render($v, false, $depth + 1);
});
return "[\n$r\n" . $this->createIndent($depth) . "]";
}
/**
* @param array $a
* @return bool
*/
protected function isAssociativeArray(array $a)
{
return array_keys($a) !== range(0, count($a) - 1);
}
/**
* @param string $value
* @return string
*/
protected function renderString($value)
{
if ($value === TestCase::ANYTHING) {
return '<ANYTHING>';
}
return $this->colorize('"' . $value . '"');
}
/**
* @return string
*/
protected function renderClosure()
{
$c = new Color();
return ($this->theme ? $c('function')->{$this->theme['value.closure']} : 'function');
}
/**
* @param boolean $showTypeHint
* @param object $value
* @param integer $depth
* @return string
*/
protected function renderObject($showTypeHint, $value, $depth)
{
return ($showTypeHint ? get_class($value) . ':' : '') . $this->jsonEncode($value, $depth);
}
/**
* @param mixed $value
* @param bool $showTypeHint
* @param int $depth
* @return string
*/
public function render($value, $showTypeHint = true, $depth = 0)
{
if ($depth >= $this->getMaximumDepth()) {
return "...";
}
if ($value instanceof Closure) {
return $this->renderClosure($value);
}
if (is_object($value)) {
return $this->renderObject($showTypeHint, $value, $depth);
}
if (is_array($value)) {
return $this->jsonEncode($value, $depth);
}
if (is_string($value)) {
return $this->renderString($value);
}
return $this->colorize($value);
}
public function getMaximumDepth()
{
return 10;
}
public function renderAll(array $items)
{
return implode(', ', array_map(array($this, 'render'), $items));
}
public function setTheme(DefaultTheme $theme)
{
$this->theme = $theme->getTheme();
}
}