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