Completed
Pull Request — master (#4)
by James Ekow Abaka
03:06 queued 36s
created

Variable::initialize()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8.1867

Importance

Changes 0
Metric Value
cc 8
eloc 14
nc 8
nop 2
dl 0
loc 19
ccs 12
cts 14
cp 0.8571
crap 8.1867
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace ntentan\honam\engines\php;
4
5
6
use ArrayAccess;
7
use Countable;
8
use Iterator;
9
use ntentan\honam\exceptions\HonamException;
10
11
class Variable implements ArrayAccess, Iterator, Countable
12
{
13
14
    private $keys;
15
    private $position;
16
    private $data;
17
    private $iteratable = false;
18
    private $janitor;
19
20
    /**
21
     * @param $data
22
     * @param Janitor $janitor
23
     * @return mixed
24
     * @throws HonamException
25
     */
26 24
    public static function initialize($data, Janitor $janitor)
27
    {
28 24
        $type = gettype($data);
29 24
        switch ($type) {
30 24
            case 'object':
31 24
            case 'string':
32 23
                return new Variable($janitor, $data);
33
34 22
            case 'array':
35 22
                return new Variable($janitor, $data, array_keys($data));
36
37 20
            case 'NULL':
38 19
            case 'boolean':
39 1
            case 'integer':
40
            case 'double':
41 20
                return $data;
42
43
            default:
44
                throw new HonamException("Cannot handle the $type type in templates");
45
        }
46
    }
47
48 24
    public function __construct(Janitor $janitor, $data, $keys = array())
49
    {
50 24
        $this->janitor = $janitor;
51
52 24
        if ($data instanceof Variable) {
53 16
            $this->data = $data->getData();
54 16
            $this->keys = $data->getKeys();
55
        } else {
56 24
            $this->data = $data;
57 24
            $this->keys = $keys;
58
        }
59
60 24
        if ($this->data instanceof Iterator) {
61
            $this->iteratable = true;
62
        }
63 24
    }
64
65 23
    public function __toString()
66
    {
67 23
        return $this->janitor->cleanHtml((string)$this->data);
68
    }
69
70 10
    public function u()
71
    {
72 10
        return $this->unescape();
73
    }
74
75 4
    public function count()
76
    {
77 4
        return count($this->data);
78
    }
79
80 18
    public function unescape()
81
    {
82 18
        return $this->data;
83
    }
84
85 22
    public function rewind()
86
    {
87 22
        if ($this->iteratable) {
88
            $this->data->rewind();
89
        } else {
90 22
            $this->position = 0;
91
        }
92 22
    }
93
94 22
    public function valid()
95
    {
96 22
        if ($this->iteratable) {
97
            return $this->data->valid();
98
        } else {
99 22
            return isset($this->keys[$this->position]) && isset($this->data[$this->keys[$this->position]]);
100
        }
101
    }
102
103 21
    public function current()
104
    {
105 21
        if ($this->iteratable) {
106
            return $this->data->current();
107
        } else {
108 21
            return Variable::initialize($this->data[$this->keys[$this->position]], $this->janitor);
109
        }
110
    }
111
112 16
    public function key()
113
    {
114 16
        if ($this->iteratable) {
115
            return $this->data->key();
116
        } else {
117 16
            return $this->keys[$this->position];
118
        }
119
    }
120
121 21
    public function next()
122
    {
123 21
        if ($this->iteratable) {
124
            $this->data->next();
125
        } else {
126 21
            $this->position++;
127
        }
128 21
    }
129
130 5
    public function offsetExists($offset)
131
    {
132 5
        return isset($this->data[$offset]);
133
    }
134
135 9
    public function offsetGet($offset)
136
    {
137 9
        if (isset($this->data[$offset])) {
138 9
            return Variable::initialize($this->data[$offset], $this->janitor);
139
        } else {
140
            return null;
141
        }
142
    }
143
144 1
    public function offsetSet($offset, $value)
145
    {
146 1
        if (is_null($offset)) {
147 1
            $this->data[] = $value;
148
        } else {
149 1
            $this->data[$offset] = $value;
150
        }
151 1
    }
152
153
    public function offsetUnset($offset)
154
    {
155
        unset($this->data[$offset]);
156
    }
157
158
    /**
159
     * @param $method
160
     * @param $arguments
161
     * @return mixed
162
     * @throws HonamException
163
     */
164 11
    public function __call($method, $arguments)
165
    {
166 11
        return Variable::initialize(call_user_func_array([$this->data, $method], $arguments), $this->janitor);
167
    }
168
169
    /**
170
     * @param $name
171
     * @return mixed
172
     * @throws HonamException
173
     */
174
    public function __get($name)
175
    {
176
        return Variable::initialize($this->data->$name, $this->janitor);
177
    }
178
179
    public function __debugInfo()
180
    {
181
        return is_array($this->data) ? $this->data : [$this->data];
182
    }
183
184 16
    public function getData()
185
    {
186 16
        return $this->data;
187
    }
188
189 16
    public function getKeys()
190
    {
191 16
        return $this->keys;
192
    }
193
}
194