3 class HTMLPurifier_Array
implements ArrayAccess
6 * @param HTMLPurifier_ArrayNode
18 protected $offset = 0;
21 * @var HTMLPurifier_ArrayNode
23 protected $offsetItem = null;
26 public function __construct(array $array = array())
29 * @var HTMLPurifier_ArrayNode $temp
34 foreach ($array as &$v) {
35 $item = new HTMLPurifier_ArrayNode($v);
37 if ($this->head
== null) {
40 if ($temp instanceof HTMLPurifier_ArrayNode
) {
53 $this->offsetItem
= &$this->head
;
56 protected function findIndex($offset)
58 if ($this->head
== null) {
65 $current = &$this->head
;
68 if ($this->offset
<= $offset && $this->offsetItem
instanceof HTMLPurifier_ArrayNode
) {
69 $current = &$this->offsetItem
;
70 $index = $this->offset
;
73 while ($current->next
instanceof HTMLPurifier_ArrayNode
&& $index != $offset) {
74 $current = &$current->next
;
78 if ($index == $offset) {
79 $this->offset
= $offset;
80 $this->offsetItem
= &$current;
93 public function insertBefore($offset, $value)
95 $result = $this->findIndex($offset);
98 $item = new HTMLPurifier_ArrayNode($value);
99 if ($result['correct'] == false) {
100 if ($result['value'] instanceof HTMLPurifier_ArrayNode
) {
101 $result['value']->next
= &$item;
102 $item->prev
= &$result['value'];
105 if ($result['value'] instanceof HTMLPurifier_ArrayNode
) {
106 $item->prev
= &$result['value']->prev
;
107 $item->next
= &$result['value'];
109 if ($item->prev
instanceof HTMLPurifier_ArrayNode
) {
110 $item->prev
->next
= &$item;
112 if ($result['value'] instanceof HTMLPurifier_ArrayNode
) {
113 $result['value']->prev
= &$item;
117 $this->head
= &$item;
119 if ($offset <= $this->offset
&& $this->offsetItem
instanceof HTMLPurifier_ArrayNode
) {
120 $this->offsetItem
= &$this->offsetItem
->prev
;
124 public function remove($offset)
126 $result = $this->findIndex($offset);
128 if ($result['correct']) {
130 $item = $result['value'];
131 $item->prev
->next
= &$result['value']->next
;
132 $item->next
->prev
= &$result['value']->prev
;
134 $this->head
= &$item->next
;
136 if ($offset < $this->offset
) {
138 } elseif ($offset == $this->offset
) {
139 $this->offsetItem
= &$item->next
;
144 public function getArray()
149 while ($head instanceof HTMLPurifier_ArrayNode
) {
150 $return[] = $head->value
;
151 $head = &$head->next
;
157 public function offsetExists($offset)
159 return $offset >= 0 && $offset < $this->count
;
162 public function offsetGet($offset)
164 $result = $this->findIndex($offset);
165 if ($result['correct']) {
166 return $result['value']->value
;
172 public function offsetSet($offset, $value)
174 $result = $this->findIndex($offset);
175 if ($result['correct']) {
176 $result['value']->value
= &$value;
180 public function offsetUnset($offset)
182 $this->remove($offset);