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