PluginProbe
Age Gate / 3.7.3
Age Gate v3.7.3
3.7.3 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 All 113 releases
age-gate / vendor / nette / utils / src / Utils / ArrayList.php

ArrayList.php in Age Gate 3.7.3, at vendor/nette/utils/src/Utils/ArrayList.php

138 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 declare(strict_types=1);
9
10 namespace Nette\Utils;
11
12 use Nette;
13
14
15 /**
16 * Provides the base class for a generic list (items can be accessed by index).
17 * @template T
18 * @implements \IteratorAggregate<int, T>
19 * @implements \ArrayAccess<int, T>
20 */
21 class ArrayList implements \ArrayAccess, \Countable, \IteratorAggregate
22 {
23 use Nette\SmartObject;
24
25 /** @var mixed[] */
26 private $list = [];
27
28
29 /**
30 * Transforms array to ArrayList.
31 * @param list<T> $array
32 * @return static
33 */
34 public static function from(array $array)
35 {
36 if (!Arrays::isList($array)) {
37 throw new Nette\InvalidArgumentException('Array is not valid list.');
38 }
39
40 $obj = new static;
41 $obj->list = $array;
42 return $obj;
43 }
44
45
46 /**
47 * Returns an iterator over all items.
48 * @return \ArrayIterator<int, T>
49 */
50 public function getIterator(): \ArrayIterator
51 {
52 return new \ArrayIterator($this->list);
53 }
54
55
56 /**
57 * Returns items count.
58 */
59 public function count(): int
60 {
61 return count($this->list);
62 }
63
64
65 /**
66 * Replaces or appends a item.
67 * @param int|null $index
68 * @param T $value
69 * @throws Nette\OutOfRangeException
70 */
71 public function offsetSet($index, $value): void
72 {
73 if ($index === null) {
74 $this->list[] = $value;
75
76 } elseif (!is_int($index) || $index < 0 || $index >= count($this->list)) {
77 throw new Nette\OutOfRangeException('Offset invalid or out of range');
78
79 } else {
80 $this->list[$index] = $value;
81 }
82 }
83
84
85 /**
86 * Returns a item.
87 * @param int $index
88 * @return T
89 * @throws Nette\OutOfRangeException
90 */
91 #[\ReturnTypeWillChange]
92 public function offsetGet($index)
93 {
94 if (!is_int($index) || $index < 0 || $index >= count($this->list)) {
95 throw new Nette\OutOfRangeException('Offset invalid or out of range');
96 }
97
98 return $this->list[$index];
99 }
100
101
102 /**
103 * Determines whether a item exists.
104 * @param int $index
105 */
106 public function offsetExists($index): bool
107 {
108 return is_int($index) && $index >= 0 && $index < count($this->list);
109 }
110
111
112 /**
113 * Removes the element at the specified position in this list.
114 * @param int $index
115 * @throws Nette\OutOfRangeException
116 */
117 public function offsetUnset($index): void
118 {
119 if (!is_int($index) || $index < 0 || $index >= count($this->list)) {
120 throw new Nette\OutOfRangeException('Offset invalid or out of range');
121 }
122
123 array_splice($this->list, $index, 1);
124 }
125
126
127 /**
128 * Prepends a item.
129 * @param T $value
130 */
131 public function prepend($value): void
132 {
133 $first = array_slice($this->list, 0, 1);
134 $this->offsetSet(0, $value);
135 array_splice($this->list, 1, 0, $first);
136 }
137 }
138