| 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 |
*/ |
| 14 |
class ArrayList implements \ArrayAccess, \Countable, \IteratorAggregate |
| 15 |
{ |
| 16 |
use \Packetery\Nette\SmartObject; |
| 17 |
/** @var mixed[] */ |
| 18 |
private $list = []; |
| 19 |
/** |
| 20 |
* Returns an iterator over all items. |
| 21 |
*/ |
| 22 |
public function getIterator() : \ArrayIterator |
| 23 |
{ |
| 24 |
return new \ArrayIterator($this->list); |
| 25 |
} |
| 26 |
/** |
| 27 |
* Returns items count. |
| 28 |
*/ |
| 29 |
public function count() : int |
| 30 |
{ |
| 31 |
return \count($this->list); |
| 32 |
} |
| 33 |
/** |
| 34 |
* Replaces or appends a item. |
| 35 |
* @param int|null $index |
| 36 |
* @param mixed $value |
| 37 |
* @throws \Packetery\Nette\OutOfRangeException |
| 38 |
*/ |
| 39 |
public function offsetSet($index, $value) : void |
| 40 |
{ |
| 41 |
if ($index === null) { |
| 42 |
$this->list[] = $value; |
| 43 |
} elseif (!\is_int($index) || $index < 0 || $index >= \count($this->list)) { |
| 44 |
throw new \Packetery\Nette\OutOfRangeException('Offset invalid or out of range'); |
| 45 |
} else { |
| 46 |
$this->list[$index] = $value; |
| 47 |
} |
| 48 |
} |
| 49 |
/** |
| 50 |
* Returns a item. |
| 51 |
* @param int $index |
| 52 |
* @return mixed |
| 53 |
* @throws \Packetery\Nette\OutOfRangeException |
| 54 |
*/ |
| 55 |
public function offsetGet($index) |
| 56 |
{ |
| 57 |
if (!\is_int($index) || $index < 0 || $index >= \count($this->list)) { |
| 58 |
throw new \Packetery\Nette\OutOfRangeException('Offset invalid or out of range'); |
| 59 |
} |
| 60 |
return $this->list[$index]; |
| 61 |
} |
| 62 |
/** |
| 63 |
* Determines whether a item exists. |
| 64 |
* @param int $index |
| 65 |
*/ |
| 66 |
public function offsetExists($index) : bool |
| 67 |
{ |
| 68 |
return \is_int($index) && $index >= 0 && $index < \count($this->list); |
| 69 |
} |
| 70 |
/** |
| 71 |
* Removes the element at the specified position in this list. |
| 72 |
* @param int $index |
| 73 |
* @throws \Packetery\Nette\OutOfRangeException |
| 74 |
*/ |
| 75 |
public function offsetUnset($index) : void |
| 76 |
{ |
| 77 |
if (!\is_int($index) || $index < 0 || $index >= \count($this->list)) { |
| 78 |
throw new \Packetery\Nette\OutOfRangeException('Offset invalid or out of range'); |
| 79 |
} |
| 80 |
\array_splice($this->list, $index, 1); |
| 81 |
} |
| 82 |
/** |
| 83 |
* Prepends a item. |
| 84 |
* @param mixed $value |
| 85 |
*/ |
| 86 |
public function prepend($value) : void |
| 87 |
{ |
| 88 |
$first = \array_slice($this->list, 0, 1); |
| 89 |
$this->offsetSet(0, $value); |
| 90 |
\array_splice($this->list, 1, 0, $first); |
| 91 |
} |
| 92 |
} |
| 93 |
|