1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
6: * @package Nette
7: */
8:
9:
10:
11: /**
12: * Provides the base class for a generic list (items can be accessed by index).
13: *
14: * @author David Grudl
15: *
16: * @property-read ArrayIterator $iterator
17: * @package Nette
18: */
19: class ArrayList extends Object implements ArrayAccess, Countable, IteratorAggregate
20: {
21: private $list = array();
22:
23:
24: /**
25: * Returns an iterator over all items.
26: * @return ArrayIterator
27: */
28: public function getIterator()
29: {
30: return new ArrayIterator($this->list);
31: }
32:
33:
34: /**
35: * Returns items count.
36: * @return int
37: */
38: public function count()
39: {
40: return count($this->list);
41: }
42:
43:
44: /**
45: * Replaces or appends a item.
46: * @param int
47: * @param mixed
48: * @return void
49: * @throws OutOfRangeException
50: */
51: public function offsetSet($index, $value)
52: {
53: if ($index === NULL) {
54: $this->list[] = $value;
55:
56: } elseif ($index < 0 || $index >= count($this->list)) {
57: throw new OutOfRangeException('Offset invalid or out of range');
58:
59: } else {
60: $this->list[(int) $index] = $value;
61: }
62: }
63:
64:
65: /**
66: * Returns a item.
67: * @param int
68: * @return mixed
69: * @throws OutOfRangeException
70: */
71: public function offsetGet($index)
72: {
73: if ($index < 0 || $index >= count($this->list)) {
74: throw new OutOfRangeException('Offset invalid or out of range');
75: }
76: return $this->list[(int) $index];
77: }
78:
79:
80: /**
81: * Determines whether a item exists.
82: * @param int
83: * @return bool
84: */
85: public function offsetExists($index)
86: {
87: return $index >= 0 && $index < count($this->list);
88: }
89:
90:
91: /**
92: * Removes the element at the specified position in this list.
93: * @param int
94: * @return void
95: * @throws OutOfRangeException
96: */
97: public function offsetUnset($index)
98: {
99: if ($index < 0 || $index >= count($this->list)) {
100: throw new OutOfRangeException('Offset invalid or out of range');
101: }
102: array_splice($this->list, (int) $index, 1);
103: }
104:
105: }
106: