Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | 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
 
23
	/** @var mixed[] */
24
	private $list = [];
25
 
26
 
27
	/**
28
	 * Transforms array to ArrayList.
29
	 * @param  array<T>  $array
30
	 * @return static
31
	 */
32
	public static function from(array $array)
33
	{
34
		if (!Arrays::isList($array)) {
35
			throw new Nette\InvalidArgumentException('Array is not valid list.');
36
		}
37
 
38
		$obj = new static;
39
		$obj->list = $array;
40
		return $obj;
41
	}
42
 
43
 
44
	/**
45
	 * Returns an iterator over all items.
46
	 * @return \ArrayIterator<int, T>
47
	 */
48
	public function getIterator(): \ArrayIterator
49
	{
50
		return new \ArrayIterator($this->list);
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
	 */
89
	#[\ReturnTypeWillChange]
90
	public function offsetGet($index)
91
	{
92
		if (!is_int($index) || $index < 0 || $index >= count($this->list)) {
93
			throw new Nette\OutOfRangeException('Offset invalid or out of range');
94
		}
95
 
96
		return $this->list[$index];
97
	}
98
 
99
 
100
	/**
101
	 * Determines whether a item exists.
102
	 * @param  int  $index
103
	 */
104
	public function offsetExists($index): bool
105
	{
106
		return is_int($index) && $index >= 0 && $index < count($this->list);
107
	}
108
 
109
 
110
	/**
111
	 * Removes the element at the specified position in this list.
112
	 * @param  int  $index
113
	 * @throws Nette\OutOfRangeException
114
	 */
115
	public function offsetUnset($index): void
116
	{
117
		if (!is_int($index) || $index < 0 || $index >= count($this->list)) {
118
			throw new Nette\OutOfRangeException('Offset invalid or out of range');
119
		}
120
 
121
		array_splice($this->list, $index, 1);
122
	}
123
 
124
 
125
	/**
126
	 * Prepends a item.
127
	 * @param  T  $value
128
	 */
129
	public function prepend($value): void
130
	{
131
		$first = array_slice($this->list, 0, 1);
132
		$this->offsetSet(0, $value);
133
		array_splice($this->list, 1, 0, $first);
134
	}
135
}