Gusev's proposed patch
[htmlpurifier.git] / library / HTMLPurifier / Array.php
blob4a40fa527104609d7e56cb4057516a7adeb97cd0
1 <?php
3 class HTMLPurifier_Array implements ArrayAccess
5 /**
6 * @param HTMLPurifier_ArrayNode
7 */
8 public $head = null;
10 /**
11 * @var int
13 protected $count = 0;
15 /**
16 * @var int
18 protected $offset = 0;
20 /**
21 * @var HTMLPurifier_ArrayNode
23 protected $offsetItem = null;
26 public function __construct(array $array = array())
28 /**
29 * @var HTMLPurifier_ArrayNode $temp
31 $temp = null;
32 $i = 0;
34 foreach ($array as &$v) {
35 $item = new HTMLPurifier_ArrayNode($v);
37 if ($this->head == null) {
38 $this->head = &$item;
40 if ($temp instanceof HTMLPurifier_ArrayNode) {
41 $item->prev = &$temp;
42 $temp->next = &$item;
44 unset($temp);
45 $temp = &$item;
47 $i ++;
49 unset($item, $v);
51 $this->count = $i;
52 $this->offset = 0;
53 $this->offsetItem = &$this->head;
56 protected function findIndex($offset)
58 if ($this->head == null) {
59 return array(
60 'correct' => false,
61 'value' => null
65 $current = &$this->head;
66 $index = 0;
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;
75 $index ++;
78 if ($index == $offset) {
79 $this->offset = $offset;
80 $this->offsetItem = &$current;
81 return array(
82 'correct' => true,
83 'value' => &$current
87 return array(
88 'correct' => false,
89 'value' => &$current
93 public function insertBefore($offset, $value)
95 $result = $this->findIndex($offset);
97 $this->count ++;
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'];
104 } else {
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;
116 if ($offset == 0) {
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']) {
129 $this->count --;
130 $item = $result['value'];
131 $item->prev->next = &$result['value']->next;
132 $item->next->prev = &$result['value']->prev;
133 if ($offset == 0) {
134 $this->head = &$item->next;
136 if ($offset < $this->offset) {
137 $this->offset --;
138 } elseif ($offset == $this->offset) {
139 $this->offsetItem = &$item->next;
144 public function getArray()
146 $return = array();
147 $head = $this->head;
149 while ($head instanceof HTMLPurifier_ArrayNode) {
150 $return[] = $head->value;
151 $head = &$head->next;
154 return $return;
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;
169 return null;
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);