Variable::offsetSet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
rs 10
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
    public static function initialize($data, Janitor $janitor)
27
    {
28
        $type = gettype($data);
29
        switch ($type) {
30
            case 'object':
31
            case 'string':
32
                return new Variable($janitor, $data);
33
34
            case 'array':
35
                return new Variable($janitor, $data, array_keys($data));
36
37
            case 'NULL':
38
            case 'boolean':
39
            case 'integer':
40
            case 'double':
41
                return $data;
42
43
            default:
44
                throw new HonamException("Cannot handle the $type type in templates");
45
        }
46
    }
47
48
    public function __construct(Janitor $janitor, $data, $keys = array())
49
    {
50
        $this->janitor = $janitor;
51
52
        if ($data instanceof Variable) {
53
            $this->data = $data->getData();
54
            $this->keys = $data->getKeys();
55
        } else {
56
            $this->data = $data;
57
            $this->keys = $keys;
58
        }
59
60
        if ($this->data instanceof Iterator) {
61
            $this->iteratable = true;
62
        }
63
    }
64
65
    public function __toString()
66
    {
67
        return $this->janitor->cleanHtml((string)$this->data);
68
    }
69
70
    public function u()
71
    {
72
        return $this->unescape();
73
    }
74
75
    public function count(): int
76
    {
77
        return count($this->data);
78
    }
79
80
    public function unescape()
81
    {
82
        return $this->data;
83
    }
84
85
    public function rewind(): void
86
    {
87
        if ($this->iteratable) {
88
            $this->data->rewind();
89
        } else {
90
            $this->position = 0;
91
        }
92
    }
93
94
    public function valid(): bool
95
    {
96
        if ($this->iteratable) {
97
            return $this->data->valid();
98
        } else {
99
            return isset($this->keys[$this->position]) && isset($this->data[$this->keys[$this->position]]);
100
        }
101
    }
102
103
    public function current(): mixed
104
    {
105
        if ($this->iteratable) {
106
            return $this->data->current();
107
        } else {
108
            return Variable::initialize($this->data[$this->keys[$this->position]], $this->janitor);
109
        }
110
    }
111
112
    public function key(): mixed
113
    {
114
        if ($this->iteratable) {
115
            return $this->data->key();
116
        } else {
117
            return $this->keys[$this->position];
118
        }
119
    }
120
121
    public function next(): void
122
    {
123
        if ($this->iteratable) {
124
            $this->data->next();
125
        } else {
126
            $this->position++;
127
        }
128
    }
129
130
    public function offsetExists($offset): bool
131
    {
132
        return isset($this->data[$offset]);
133
    }
134
135
    public function offsetGet($offset): mixed
136
    {
137
        if (isset($this->data[$offset])) {
138
            return Variable::initialize($this->data[$offset], $this->janitor);
139
        } else {
140
            return null;
141
        }
142
    }
143
144
    public function offsetSet($offset, $value): void
145
    {
146
        if (is_null($offset)) {
147
            $this->data[] = $value;
148
        } else {
149
            $this->data[$offset] = $value;
150
        }
151
    }
152
153
    public function offsetUnset($offset): void
154
    {
155
        unset($this->data[$offset]);
156
    }
157
158
    /**
159
     * @param $method
160
     * @param $arguments
161
     * @return mixed
162
     * @throws HonamException
163
     */
164
    public function __call($method, $arguments)
165
    {
166
        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
    public function getData()
185
    {
186
        return $this->data;
187
    }
188
189
    public function getKeys()
190
    {
191
        return $this->keys;
192
    }
193
}
194