Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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
 */
19
class ArrayList implements \ArrayAccess, \Countable, \IteratorAggregate
20
{
21
	use Nette\SmartObject;
22
 
399 lars 23
	private array $list = [];
148 lars 24
 
25
 
26
	/**
27
	 * Transforms array to ArrayList.
28
	 * @param  array<T>  $array
29
	 */
399 lars 30
	public static function from(array $array): static
148 lars 31
	{
32
		if (!Arrays::isList($array)) {
33
			throw new Nette\InvalidArgumentException('Array is not valid list.');
34
		}
35
 
36
		$obj = new static;
37
		$obj->list = $array;
38
		return $obj;
39
	}
40
 
41
 
42
	/**
43
	 * Returns an iterator over all items.
399 lars 44
	 * @return \Iterator<int, T>
148 lars 45
	 */
399 lars 46
	public function &getIterator(): \Iterator
148 lars 47
	{
399 lars 48
		foreach ($this->list as &$item) {
49
			yield $item;
50
		}
148 lars 51
	}
52
 
53
 
54
	/**
55
	 * Returns items count.
56
	 */
57
	public function count(): int
58
	{
59
		return count($this->list);
60
	}
61
 
62
 
63
	/**
64
	 * Replaces or appends a item.
65
	 * @param  int|null  $index
66
	 * @param  T  $value
67
	 * @throws Nette\OutOfRangeException
68
	 */
69
	public function offsetSet($index, $value): void
70
	{
71
		if ($index === null) {
72
			$this->list[] = $value;
73
 
74
		} elseif (!is_int($index) || $index < 0 || $index >= count($this->list)) {
75
			throw new Nette\OutOfRangeException('Offset invalid or out of range');
76
 
77
		} else {
78
			$this->list[$index] = $value;
79
		}
80
	}
81
 
82
 
83
	/**
84
	 * Returns a item.
85
	 * @param  int  $index
86
	 * @return T
87
	 * @throws Nette\OutOfRangeException
88
	 */
399 lars 89
	public function offsetGet($index): mixed
148 lars 90
	{
91
		if (!is_int($index) || $index < 0 || $index >= count($this->list)) {
92
			throw new Nette\OutOfRangeException('Offset invalid or out of range');
93
		}
94
 
95
		return $this->list[$index];
96
	}
97
 
98
 
99
	/**
100
	 * Determines whether a item exists.
101
	 * @param  int  $index
102
	 */
103
	public function offsetExists($index): bool
104
	{
105
		return is_int($index) && $index >= 0 && $index < count($this->list);
106
	}
107
 
108
 
109
	/**
110
	 * Removes the element at the specified position in this list.
111
	 * @param  int  $index
112
	 * @throws Nette\OutOfRangeException
113
	 */
114
	public function offsetUnset($index): void
115
	{
116
		if (!is_int($index) || $index < 0 || $index >= count($this->list)) {
117
			throw new Nette\OutOfRangeException('Offset invalid or out of range');
118
		}
119
 
120
		array_splice($this->list, $index, 1);
121
	}
122
 
123
 
124
	/**
125
	 * Prepends a item.
126
	 * @param  T  $value
127
	 */
399 lars 128
	public function prepend(mixed $value): void
148 lars 129
	{
130
		$first = array_slice($this->list, 0, 1);
131
		$this->offsetSet(0, $value);
132
		array_splice($this->list, 1, 0, $first);
133
	}
134
}