ArrayHash.php
2 years ago
ArrayList.php
2 years ago
Arrays.php
2 years ago
Callback.php
2 years ago
DateTime.php
2 years ago
FileSystem.php
2 years ago
Floats.php
2 years ago
Helpers.php
2 years ago
Html.php
2 years ago
Image.php
2 years ago
Json.php
2 years ago
ObjectHelpers.php
2 years ago
ObjectMixin.php
2 years ago
Paginator.php
2 years ago
Random.php
2 years ago
Reflection.php
2 years ago
Strings.php
2 years ago
Type.php
2 years ago
Validators.php
2 years ago
exceptions.php
2 years ago
ArrayList.php
112 lines
| 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 | declare (strict_types=1); |
| 8 | namespace FapiMember\Library\Nette\Utils; |
| 9 | |
| 10 | use FapiMember\Library\Nette; |
| 11 | /** |
| 12 | * Provides the base class for a generic list (items can be accessed by index). |
| 13 | * @template T |
| 14 | * @implements \IteratorAggregate<int, T> |
| 15 | * @implements \ArrayAccess<int, T> |
| 16 | */ |
| 17 | class ArrayList implements \ArrayAccess, \Countable, \IteratorAggregate |
| 18 | { |
| 19 | use Nette\SmartObject; |
| 20 | /** @var mixed[] */ |
| 21 | private $list = []; |
| 22 | /** |
| 23 | * Transforms array to ArrayList. |
| 24 | * @param list<T> $array |
| 25 | * @return static |
| 26 | */ |
| 27 | public static function from(array $array) |
| 28 | { |
| 29 | if (!Arrays::isList($array)) { |
| 30 | throw new Nette\InvalidArgumentException('Array is not valid list.'); |
| 31 | } |
| 32 | $obj = new static(); |
| 33 | $obj->list = $array; |
| 34 | return $obj; |
| 35 | } |
| 36 | /** |
| 37 | * Returns an iterator over all items. |
| 38 | * @return \ArrayIterator<int, T> |
| 39 | */ |
| 40 | public function getIterator(): \ArrayIterator |
| 41 | { |
| 42 | return new \ArrayIterator($this->list); |
| 43 | } |
| 44 | /** |
| 45 | * Returns items count. |
| 46 | */ |
| 47 | public function count(): int |
| 48 | { |
| 49 | return count($this->list); |
| 50 | } |
| 51 | /** |
| 52 | * Replaces or appends a item. |
| 53 | * @param int|null $index |
| 54 | * @param T $value |
| 55 | * @throws Nette\OutOfRangeException |
| 56 | */ |
| 57 | public function offsetSet($index, $value): void |
| 58 | { |
| 59 | if ($index === null) { |
| 60 | $this->list[] = $value; |
| 61 | } elseif (!is_int($index) || $index < 0 || $index >= count($this->list)) { |
| 62 | throw new Nette\OutOfRangeException('Offset invalid or out of range'); |
| 63 | } else { |
| 64 | $this->list[$index] = $value; |
| 65 | } |
| 66 | } |
| 67 | /** |
| 68 | * Returns a item. |
| 69 | * @param int $index |
| 70 | * @return T |
| 71 | * @throws Nette\OutOfRangeException |
| 72 | */ |
| 73 | #[\ReturnTypeWillChange] |
| 74 | public function offsetGet($index) |
| 75 | { |
| 76 | if (!is_int($index) || $index < 0 || $index >= count($this->list)) { |
| 77 | throw new Nette\OutOfRangeException('Offset invalid or out of range'); |
| 78 | } |
| 79 | return $this->list[$index]; |
| 80 | } |
| 81 | /** |
| 82 | * Determines whether a item exists. |
| 83 | * @param int $index |
| 84 | */ |
| 85 | public function offsetExists($index): bool |
| 86 | { |
| 87 | return is_int($index) && $index >= 0 && $index < count($this->list); |
| 88 | } |
| 89 | /** |
| 90 | * Removes the element at the specified position in this list. |
| 91 | * @param int $index |
| 92 | * @throws Nette\OutOfRangeException |
| 93 | */ |
| 94 | public function offsetUnset($index): void |
| 95 | { |
| 96 | if (!is_int($index) || $index < 0 || $index >= count($this->list)) { |
| 97 | throw new Nette\OutOfRangeException('Offset invalid or out of range'); |
| 98 | } |
| 99 | array_splice($this->list, $index, 1); |
| 100 | } |
| 101 | /** |
| 102 | * Prepends a item. |
| 103 | * @param T $value |
| 104 | */ |
| 105 | public function prepend($value): void |
| 106 | { |
| 107 | $first = array_slice($this->list, 0, 1); |
| 108 | $this->offsetSet(0, $value); |
| 109 | array_splice($this->list, 1, 0, $first); |
| 110 | } |
| 111 | } |
| 112 |