1
|
|
|
<?php |
2
|
|
|
namespace nstdio\svg; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class ChildStorage |
6
|
|
|
* |
7
|
|
|
* @package nstdio\svg |
8
|
|
|
* @author Edgar Asatryan <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class ElementStorage implements \Countable, \ArrayAccess, \Iterator |
11
|
|
|
{ |
12
|
|
|
private $data = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @inheritdoc |
16
|
|
|
*/ |
17
|
71 |
|
public function count() |
18
|
|
|
{ |
19
|
71 |
|
return count($this->data); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @inheritdoc |
24
|
|
|
*/ |
25
|
71 |
|
public function offsetExists($offset) |
26
|
|
|
{ |
27
|
71 |
|
return isset($this->data[$offset]); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
*/ |
33
|
70 |
|
public function offsetGet($offset) |
34
|
|
|
{ |
35
|
70 |
|
if ($this->offsetExists($offset)) { |
36
|
70 |
|
return $this->data[$offset]; |
37
|
|
|
} |
38
|
3 |
|
return null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
150 |
|
public function offsetSet($offset, $value) |
45
|
|
|
{ |
46
|
150 |
|
if (!($value instanceof ElementInterface)) { |
47
|
6 |
|
throw new \InvalidArgumentException( |
48
|
6 |
|
sprintf("The value must implement ElementInterface, %s given.", gettype($value)) |
49
|
6 |
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
150 |
|
if (gettype($offset) !== 'integer' && $offset !== null) { |
53
|
5 |
|
throw new \InvalidArgumentException( |
54
|
5 |
|
sprintf("The offset must be integer, %s given.", gettype($offset)) |
55
|
5 |
|
); |
56
|
|
|
} |
57
|
150 |
|
if (!in_array($value, $this->data, true)) { |
58
|
150 |
|
if ($offset === null) { |
59
|
150 |
|
$this->data[] = $value; |
60
|
150 |
|
} |
61
|
150 |
|
} |
62
|
150 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
14 |
|
public function offsetUnset($offset) |
68
|
|
|
{ |
69
|
14 |
|
if ($this->offsetExists($offset)) { |
70
|
14 |
|
unset($this->data[$offset]); |
71
|
14 |
|
$this->data = array_values($this->data); |
72
|
14 |
|
} |
73
|
14 |
|
} |
74
|
|
|
|
75
|
13 |
|
public function remove(ElementInterface $element) |
76
|
|
|
{ |
77
|
13 |
|
$key = array_search($element, $this->data, true); |
78
|
13 |
|
if ($key !== false) { |
79
|
13 |
|
$this->offsetUnset($key); |
80
|
13 |
|
} |
81
|
13 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
86
|
8 |
|
public function current() |
87
|
|
|
{ |
88
|
8 |
|
return current($this->data); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
8 |
|
public function next() |
95
|
|
|
{ |
96
|
8 |
|
return next($this->data); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritdoc |
101
|
|
|
*/ |
102
|
1 |
|
public function key() |
103
|
|
|
{ |
104
|
1 |
|
return key($this->data); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritdoc |
109
|
|
|
*/ |
110
|
8 |
|
public function valid() |
111
|
|
|
{ |
112
|
8 |
|
return key($this->data) !== null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritdoc |
117
|
|
|
*/ |
118
|
8 |
|
public function rewind() |
119
|
|
|
{ |
120
|
8 |
|
return reset($this->data); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|