<?php /** * This file is part of the Nette Framework (https://nette.org) * Copyright (c) 2004 David Grudl (https://davidgrudl.com) */ declare(strict_types=1); namespace Nette\Utils; use Nette; /** * Provides the base class for a generic list (items can be accessed by index). * @template T */ class ArrayList implements \ArrayAccess, \Countable, \IteratorAggregate { use Nette\SmartObject; private array $list = []; /** * Transforms array to ArrayList. * @param array<T> $array */ public static function from(array $array): static { if (!Arrays::isList($array)) { throw new Nette\InvalidArgumentException('Array is not valid list.'); } $obj = new static; $obj->list = $array; return $obj; } /** * Returns an iterator over all items. * @return \Iterator<int, T> */ public function &getIterator(): \Iterator { foreach ($this->list as &$item) { yield $item; } } /** * Returns items count. */ public function count(): int { return count($this->list); } /** * Replaces or appends a item. * @param int|null $index * @param T $value * @throws Nette\OutOfRangeException */ public function offsetSet($index, $value): void { if ($index === null) { $this->list[] = $value; } elseif (!is_int($index) || $index < 0 || $index >= count($this->list)) { throw new Nette\OutOfRangeException('Offset invalid or out of range'); } else { $this->list[$index] = $value; } } /** * Returns a item. * @param int $index * @return T * @throws Nette\OutOfRangeException */ public function offsetGet($index): mixed { if (!is_int($index) || $index < 0 || $index >= count($this->list)) { throw new Nette\OutOfRangeException('Offset invalid or out of range'); } return $this->list[$index]; } /** * Determines whether a item exists. * @param int $index */ public function offsetExists($index): bool { return is_int($index) && $index >= 0 && $index < count($this->list); } /** * Removes the element at the specified position in this list. * @param int $index * @throws Nette\OutOfRangeException */ public function offsetUnset($index): void { if (!is_int($index) || $index < 0 || $index >= count($this->list)) { throw new Nette\OutOfRangeException('Offset invalid or out of range'); } array_splice($this->list, $index, 1); } /** * Prepends a item. * @param T $value */ public function prepend(mixed $value): void { $first = array_slice($this->list, 0, 1); $this->offsetSet(0, $value); array_splice($this->list, 1, 0, $first); } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
ArrayHash.php | File | 1.78 KB | 0644 |
|
ArrayList.php | File | 2.54 KB | 0644 |
|
Arrays.php | File | 10.53 KB | 0644 |
|
Callback.php | File | 3.45 KB | 0644 |
|
DateTime.php | File | 2.89 KB | 0644 |
|
FileInfo.php | File | 1.26 KB | 0644 |
|
FileSystem.php | File | 9.09 KB | 0644 |
|
Finder.php | File | 12.81 KB | 0644 |
|
Floats.php | File | 2.07 KB | 0644 |
|
Helpers.php | File | 2.5 KB | 0644 |
|
Html.php | File | 19.02 KB | 0644 |
|
Image.php | File | 21.27 KB | 0644 |
|
Json.php | File | 2.22 KB | 0644 |
|
ObjectHelpers.php | File | 6.85 KB | 0644 |
|
Paginator.php | File | 4.04 KB | 0644 |
|
Random.php | File | 1.07 KB | 0644 |
|
Reflection.php | File | 8.35 KB | 0644 |
|
Strings.php | File | 21.45 KB | 0644 |
|
Type.php | File | 6.2 KB | 0644 |
|
Validators.php | File | 10.08 KB | 0644 |
|
exceptions.php | File | 775 B | 0644 |
|