1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheIconic\Synopsis; |
4
|
|
|
|
5
|
|
|
use Countable; |
6
|
|
|
use Traversable; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Represents a synopsis of a value (or object), that is a description of the value useful |
10
|
|
|
* for debugging purposes |
11
|
|
|
* |
12
|
|
|
* @package TheIconic\Synopsis |
13
|
|
|
*/ |
14
|
|
|
abstract class AbstractSynopsis |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string the type |
18
|
|
|
*/ |
19
|
|
|
protected $type; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var mixed the value - as in a presentable string |
23
|
|
|
*/ |
24
|
|
|
protected $value; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int the length or count |
28
|
|
|
*/ |
29
|
|
|
protected $length; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array the kiddos |
33
|
|
|
*/ |
34
|
|
|
protected $children; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Factory the synopsis factory (used for cascaded creation of synopsises) |
38
|
|
|
*/ |
39
|
|
|
protected $factory; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* processes passed values to properties |
43
|
|
|
* |
44
|
|
|
* @param $value |
45
|
|
|
* @param $depth |
46
|
|
|
*/ |
47
|
|
|
public function process($value, $depth) |
48
|
|
|
{ |
49
|
|
|
$parts = explode('\\', get_class($this)); |
50
|
|
|
$this->type = strtolower(str_replace('Synopsis', '', end($parts))); |
51
|
|
|
|
52
|
|
|
if (is_scalar($value)) { |
53
|
|
|
$this->value = (string) $value; |
54
|
|
|
$this->length = strlen((string) $value); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($value instanceof Countable) { |
58
|
|
|
$this->length = count($value); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($value instanceof Traversable) { |
62
|
|
|
$this->handleTraversable($value, $depth); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function handleTraversable(Traversable $value, $depth) |
67
|
|
|
{ |
68
|
|
|
if ($depth) { |
69
|
|
|
$this->children = []; |
70
|
|
|
foreach ($value as $k => $v) { |
71
|
|
|
$this->addChild($this->getFactory()->synopsize($v, $depth), $k); |
72
|
|
|
} |
73
|
|
|
$this->length = count($this->children); |
74
|
|
|
} else { |
75
|
|
|
$this->length = 0; |
76
|
|
|
foreach ($value as $k => $v) { |
77
|
|
|
$this->length++; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function getDetails() |
86
|
|
|
{ |
87
|
|
|
return []; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* get the type |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function getType() |
96
|
|
|
{ |
97
|
|
|
return $this->type; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* get the length |
102
|
|
|
* |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
|
|
public function getLength() |
106
|
|
|
{ |
107
|
|
|
if (null !== $this->length) { |
108
|
|
|
return $this->length; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($this->hasChildren()) { |
112
|
|
|
return count($this->children); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return 0; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* get the value |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getValue() |
124
|
|
|
{ |
125
|
|
|
return (string) $this->value; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* get the children |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
public function getChildren() |
134
|
|
|
{ |
135
|
|
|
return $this->children; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* check if we have children |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
public function hasChildren() |
144
|
|
|
{ |
145
|
|
|
return (bool) count($this->children); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* add a child |
150
|
|
|
* |
151
|
|
|
* @param $child |
152
|
|
|
* @param $key |
153
|
|
|
*/ |
154
|
|
|
public function addChild($child, $key) |
155
|
|
|
{ |
156
|
|
|
$this->children[$key] = $child; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* get the synopsis factory |
161
|
|
|
* |
162
|
|
|
* @return Factory |
163
|
|
|
*/ |
164
|
|
|
public function getFactory() |
165
|
|
|
{ |
166
|
|
|
if (!$this->factory) { |
167
|
|
|
$this->factory = new Factory(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $this->factory; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* set a synopsis factory |
175
|
|
|
* |
176
|
|
|
* @param Factory $factory |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
public function setFactory(Factory $factory) |
180
|
|
|
{ |
181
|
|
|
$this->factory = $factory; |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|