|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AppUtils; |
|
6
|
|
|
|
|
7
|
|
|
class VariableInfo implements Interface_Optionable |
|
8
|
|
|
{ |
|
9
|
|
|
use Traits_Optionable; |
|
10
|
|
|
|
|
11
|
|
|
const TYPE_DOUBLE = 'double'; |
|
12
|
|
|
const TYPE_INTEGER = 'integer'; |
|
13
|
|
|
const TYPE_BOOLEAN = 'boolean'; |
|
14
|
|
|
const TYPE_STRING = 'string'; |
|
15
|
|
|
const TYPE_ARRAY = 'array'; |
|
16
|
|
|
const TYPE_OBJECT = 'object'; |
|
17
|
|
|
const TYPE_RESOURCE = 'resource'; |
|
18
|
|
|
const TYPE_NULL = 'null'; |
|
19
|
|
|
const TYPE_UNKNOWN = 'unknown type'; |
|
20
|
|
|
const TYPE_CALLABLE = 'callable'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $string; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var mixed |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $value; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $type; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param mixed $value |
|
39
|
|
|
* @param array|null $serialized |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($value, $serialized=null) |
|
42
|
|
|
{ |
|
43
|
|
|
if(is_array($serialized)) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->parseSerialized($serialized); |
|
46
|
|
|
} |
|
47
|
|
|
else |
|
48
|
|
|
{ |
|
49
|
|
|
$this->parseValue($value); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Creates a new variable info instance from a PHP variable |
|
55
|
|
|
* of any type. |
|
56
|
|
|
* |
|
57
|
|
|
* @param mixed $variable |
|
58
|
|
|
* @return VariableInfo |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function fromVariable($variable) : VariableInfo |
|
61
|
|
|
{ |
|
62
|
|
|
return new VariableInfo($variable); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Restores a variable info instance using a previously serialized |
|
67
|
|
|
* array using the serialize() method. |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $serialized |
|
70
|
|
|
* @return VariableInfo |
|
71
|
|
|
* @see VariableInfo::serialize() |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function fromSerialized(array $serialized) : VariableInfo |
|
74
|
|
|
{ |
|
75
|
|
|
return new VariableInfo(null, $serialized); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function parseSerialized(array $serialized) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->string = $serialized['string']; |
|
81
|
|
|
$this->type = $serialized['type']; |
|
82
|
|
|
|
|
83
|
|
|
$this->setOptions($serialized['options']); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function parseValue($value) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->value = $value; |
|
89
|
|
|
$this->type = strtolower(gettype($value)); |
|
90
|
|
|
|
|
91
|
|
|
if(is_array($value) && is_callable($value)) { |
|
92
|
|
|
$this->type = self::TYPE_CALLABLE; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$this->string = $this->_toString(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function getValue() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->value; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* The variable type - this is the same string that |
|
105
|
|
|
* is returned by the PHP function `gettype`. |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getType() : string |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->type; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function getDefaultOptions() : array |
|
115
|
|
|
{ |
|
116
|
|
|
return array( |
|
117
|
|
|
'prepend-type' => false, |
|
118
|
|
|
'cut-length' => 1000 |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Whether to prepend the variable type before the value, |
|
124
|
|
|
* like the var_dump function. Example: <code>string "Some text"</code>. |
|
125
|
|
|
* |
|
126
|
|
|
* @param bool $enable |
|
127
|
|
|
* @return VariableInfo |
|
128
|
|
|
*/ |
|
129
|
|
|
public function enableType(bool $enable=true) : VariableInfo |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->setOption('prepend-type', $enable); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function toString() : string |
|
135
|
|
|
{ |
|
136
|
|
|
$converted = $this->string; |
|
137
|
|
|
|
|
138
|
|
|
if($this->getOption('prepend-type') === true && !$this->isNull()) |
|
139
|
|
|
{ |
|
140
|
|
|
if($this->isString()) |
|
141
|
|
|
{ |
|
142
|
|
|
$converted = '"'.$converted.'"'; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$converted = $this->type.' '.$converted; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $converted; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
protected function _toString() : string |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->createRenderer('String')->render(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
protected function _toHTML() : string |
|
157
|
|
|
{ |
|
158
|
|
|
$type = str_replace(' ', '_', $this->type); |
|
159
|
|
|
$varMethod = 'toHTML_'.$type; |
|
160
|
|
|
return $this->$varMethod(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function __toString() |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->toString(); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function isInteger() : bool |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->isType(self::TYPE_INTEGER); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function isString() : bool |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->isType(self::TYPE_STRING); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function isBoolean() : bool |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->isType(self::TYPE_BOOLEAN); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function isDouble() : bool |
|
184
|
|
|
{ |
|
185
|
|
|
return $this->isType(self::TYPE_DOUBLE); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function isArray() : bool |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->isType(self::TYPE_ARRAY); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public function isNull() : bool |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->isType(self::TYPE_NULL); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function isResource() : bool |
|
199
|
|
|
{ |
|
200
|
|
|
return $this->isType(self::TYPE_RESOURCE); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function isObject() : bool |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->isType(self::TYPE_OBJECT); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
public function isType(string $type) : bool |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->type === $type; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
public function toHTML() : string |
|
214
|
|
|
{ |
|
215
|
|
|
return $this->createRenderer('HTML')->render(); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
protected function createRenderer(string $format) : VariableInfo_Renderer |
|
219
|
|
|
{ |
|
220
|
|
|
$name = ucfirst(str_replace(' ', '', $this->type)); |
|
221
|
|
|
$class = '\AppUtils\VariableInfo_Renderer_'.$format.'_'.$name; |
|
222
|
|
|
|
|
223
|
|
|
return new $class($this); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
public function serialize() |
|
227
|
|
|
{ |
|
228
|
|
|
return array( |
|
229
|
|
|
'type' => $this->type, |
|
230
|
|
|
'string' => $this->toString(), |
|
231
|
|
|
'options' => $this->getOptions() |
|
232
|
|
|
); |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|