Passed
Push — master ( 08368b...d03553 )
by Sebastian
02:27
created

VariableInfo   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 31
eloc 63
c 5
b 0
f 0
dl 0
loc 225
rs 9.92

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A getDefaultOptions() 0 5 1
A _toString() 0 3 1
A serialize() 0 6 1
A isType() 0 3 1
A getType() 0 3 1
A createRenderer() 0 6 1
A parseValue() 0 10 3
A isNull() 0 3 1
A __construct() 0 9 2
A isObject() 0 3 1
A isString() 0 3 1
A isBoolean() 0 3 1
A parseSerialized() 0 6 1
A enableType() 0 3 1
A fromSerialized() 0 3 1
A _toHTML() 0 5 1
A getValue() 0 3 1
A isInteger() 0 3 1
A isArray() 0 3 1
A toHTML() 0 3 1
A isResource() 0 3 1
A isDouble() 0 3 1
A toString() 0 15 4
A fromVariable() 0 3 1
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