Passed
Push — master ( f51ce1...bbe18b )
by Sebastian
03:35
created

VariableInfo::isArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 1
c 3
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * File containing the {@see AppUtils\VariableInfo} class.
4
 *
5
 * @package Application Utils
6
 * @subpackage VariableInfo
7
 * @see AppUtils\VariableInfo
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils;
13
14
/**
15
 * Class used to retrieve information on variable types
16
 * in an object oriented way, with way to convert these 
17
 * to human readable formats.
18
 *
19
 * @package Application Utils
20
 * @subpackage VariableInfo
21
 * @author Sebastian Mordziol <[email protected]>
22
 */
23
class VariableInfo implements Interface_Optionable
24
{
25
    use Traits_Optionable;
26
    
27
    const TYPE_DOUBLE = 'double';
28
    const TYPE_INTEGER = 'integer';
29
    const TYPE_BOOLEAN = 'boolean';
30
    const TYPE_STRING = 'string';
31
    const TYPE_ARRAY = 'array';
32
    const TYPE_OBJECT = 'object';
33
    const TYPE_RESOURCE = 'resource';
34
    const TYPE_NULL = 'null';
35
    const TYPE_UNKNOWN = 'unknown type';
36
    const TYPE_CALLABLE = 'callable';
37
38
    const ERROR_INVALID_SERIALIZED_DATA = 56301;
39
    
40
   /**
41
    * @var string
42
    */
43
    protected $string;
44
    
45
   /**
46
    * @var mixed
47
    */
48
    protected $value;
49
    
50
   /**
51
    * @var string
52
    */
53
    protected $type;
54
55
    /**
56
     * @var string[]
57
     */
58
    protected $callableTypes = array(
59
        'string',
60
        'object',
61
        'array',
62
        'closure'
63
    );
64
65
    /**
66
     * @param mixed $value
67
     * @param array|null $serialized
68
     * @throws BaseException
69
     */
70
    public function __construct($value, ?array $serialized=null)
71
    {
72
        if(is_array($serialized))
73
        {
74
            $this->parseSerialized($serialized);
75
        }
76
        else
77
        {
78
            $this->parseValue($value);
79
        }
80
    }
81
82
    /**
83
     * Creates a new variable info instance from a PHP variable
84
     * of any type.
85
     *
86
     * @param mixed $variable
87
     * @return VariableInfo
88
     * @throws BaseException
89
     */
90
    public static function fromVariable($variable) : VariableInfo
91
    {
92
        return new VariableInfo($variable);
93
    }
94
95
    /**
96
     * Restores a variable info instance using a previously serialized
97
     * array using the serialize() method.
98
     *
99
     * @param array $serialized
100
     * @return VariableInfo
101
     * @throws BaseException
102
     * @see VariableInfo::serialize()
103
     */
104
    public static function fromSerialized(array $serialized) : VariableInfo
105
    {
106
        return new VariableInfo(null, $serialized);
107
    }
108
    
109
   /**
110
    * Parses a previously serialized data set to restore the 
111
    * variable information from it.
112
    * 
113
    * @param array $serialized
114
    * @throws BaseException
115
    * 
116
    * @see VariableInfo::ERROR_INVALID_SERIALIZED_DATA
117
    */
118
    protected function parseSerialized(array $serialized) : void
119
    {
120
        if(!isset($serialized['string']) || !isset($serialized['type']) || !isset($serialized['options']))
121
        {
122
            throw new BaseException(
123
                'Invalid variable info serialized data.',
124
                'The serialized data does not contain the expected keys.',
125
                self::ERROR_INVALID_SERIALIZED_DATA
126
            );
127
        }
128
        
129
        $this->string = $serialized['string'];
130
        $this->type = $serialized['type'];
131
        
132
        $this->setOptions($serialized['options']);
133
    }
134
135
    protected function parseValue($value)
136
    {
137
        $this->value = $value;
138
        $this->type = strtolower(gettype($value));
139
        
140
        // Gettype will return a string like "Resource(closed)" when
141
        // working with a resource that has already been closed.
142
        if(strstr($this->type, 'resource'))
143
        {
144
            $this->type = self::TYPE_RESOURCE;
145
        }
146
147
        if(in_array($this->type, $this->callableTypes) && is_callable($value)) {
148
            $this->type = self::TYPE_CALLABLE;
149
        }
150
        
151
        $this->string = $this->_toString();
152
    }
153
    
154
    public function getValue()
155
    {
156
        return $this->value;
157
    }
158
    
159
   /**
160
    * The variable type - this is the same string that
161
    * is returned by the PHP function `gettype`.
162
    * 
163
    * @return string
164
    */
165
    public function getType() : string
166
    {
167
        return $this->type;
168
    }
169
    
170
    public function getDefaultOptions() : array
171
    {
172
        return array(
173
            'prepend-type' => false,
174
            'cut-length' => 1000
175
        );
176
    }
177
    
178
   /**
179
    * Whether to prepend the variable type before the value, 
180
    * like the var_dump function. Example: <code>string "Some text"</code>.
181
    * 
182
    * @param bool $enable
183
    * @return VariableInfo
184
    */
185
    public function enableType(bool $enable=true) : VariableInfo
186
    {
187
        return $this->setOption('prepend-type', $enable);
188
    }
189
    
190
    public function toString() : string
191
    {
192
        $converted = $this->string;
193
        
194
        if($this->getOption('prepend-type') === true && !$this->isNull())
195
        {
196
            if($this->isString())
197
            {
198
                $converted = '"'.$converted.'"';
199
            }
200
            
201
            $converted = $this->type.' '.$converted;
202
        }
203
        
204
        return $converted;
205
    }
206
    
207
    protected function _toString() : string
208
    {
209
        return $this->createRenderer('String')->render();
210
    }
211
    
212
    protected function _toHTML() : string
213
    {
214
        $type = str_replace(' ', '_', $this->type);
215
        $varMethod = 'toHTML_'.$type;
216
        return $this->$varMethod();
217
    }
218
    
219
    public function __toString()
220
    {
221
        return $this->toString();
222
    }
223
    
224
    public function isInteger() : bool
225
    {
226
        return $this->isType(self::TYPE_INTEGER);
227
    }
228
    
229
    public function isString() : bool
230
    {
231
        return $this->isType(self::TYPE_STRING);
232
    }
233
234
    public function isCallable() : bool
235
    {
236
        return $this->isType(self::TYPE_CALLABLE);
237
    }
238
    
239
    public function isBoolean() : bool
240
    {
241
        return $this->isType(self::TYPE_BOOLEAN);
242
    }
243
    
244
    public function isDouble() : bool
245
    {
246
        return $this->isType(self::TYPE_DOUBLE);
247
    }
248
    
249
    public function isArray() : bool
250
    {
251
        return $this->isType(self::TYPE_ARRAY);
252
    }
253
    
254
    public function isNull() : bool
255
    {
256
        return $this->isType(self::TYPE_NULL);
257
    }
258
    
259
    public function isResource() : bool
260
    {
261
        return $this->isType(self::TYPE_RESOURCE);
262
    }
263
    
264
    public function isObject() : bool
265
    {
266
        return $this->isType(self::TYPE_OBJECT);
267
    }
268
    
269
    public function isType(string $type) : bool
270
    {
271
        return $this->type === $type;
272
    }
273
274
    public function toHTML() : string
275
    {
276
        return $this->createRenderer('HTML')->render();
277
    }
278
    
279
    protected function createRenderer(string $format) : VariableInfo_Renderer
280
    {
281
        $name = ucfirst(str_replace(' ', '', $this->type));
282
        $class = '\AppUtils\VariableInfo_Renderer_'.$format.'_'.$name;
283
        
284
        return new $class($this);
285
    }
286
    
287
    public function serialize()
288
    {
289
        return array(
290
            'type' => $this->type,
291
            'string' => $this->toString(), 
292
            'options' => $this->getOptions()
293
        );
294
    }
295
}
296