-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClassStyle.php
84 lines (66 loc) · 2.33 KB
/
ClassStyle.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
<?php
declare(strict_types=1);
namespace Here\Styles;
use Here\Abstracts\VarPrinter;
use Here\Config;
use System\Console\Style\Style;
class ClassStyle extends VarPrinter
{
/**
* @param Style $style
*/
public function __construct($style)
{
parent::__construct($style);
$this->max_line = (int) Config::get('print.var.max', 5);
}
public function render(): Style
{
$obj = (object) $this->var;
$class = new \ReflectionClass($obj);
$this->style->push($class->name)->textBlue();
foreach ($class->getDefaultProperties() as $name => $value) {
$this->current_line++;
if ($this->current_line > $this->max_line) {
$this->style->newLines()->repeat(' ', $this->tab_size * 2)->push('...')->textDim();
break;
}
$visible = '';
$property = $class->getProperty($name);
$property->setAccessible(true);
$value = $property->getValue($obj);
switch ($property->getModifiers()) {
case \ReflectionProperty::IS_PUBLIC:
$visible = '+';
break;
case \ReflectionProperty::IS_PRIVATE:
$visible = '-';
break;
case \ReflectionProperty::IS_PROTECTED:
$visible = '#';
break;
}
$this->style->newLines();
$this->style->repeat(' ', $this->tab_size * 2);
$this->style->push($visible)->textYellow();
$this->style->push($name);
$this->style->push(': ')->textYellow();
$style = is_array($value)
? new ArrayStyle($this->style)
: new VarStyle($this->style);
$style
->ref($value)
->tabSize($this->tab_size + 1)
;
$this->style = $style->render();
}
if ($class->hasMethod('__tostring')) {
$this->style->newLines();
$this->style->repeat(' ', $this->tab_size * 2);
$this->style->push('__toString');
$this->style->push(': ')->textYellow();
$this->style = (new StringStyle($this->style))->ref($obj->{'__toString'}())->render();
}
return $this->style;
}
}