ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilBuddySystemArrayCollection.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
30{
35 public function __construct(private array $elements = [])
36 {
37 }
38
39 public function getIterator(): ArrayIterator
40 {
41 return new ArrayIterator($this->elements);
42 }
43
44 public function offsetExists(mixed $offset): bool
45 {
46 return $this->containsKey($offset);
47 }
48
49 public function offsetGet(mixed $offset): mixed
50 {
51 return $this->get($offset);
52 }
53
54 public function offsetSet(mixed $offset, mixed $value): void
55 {
56 if (!isset($offset)) {
57 $this->add($value);
58
59 return;
60 }
61
62 $this->set($offset, $value);
63 }
64
65 public function offsetUnset(mixed $offset): void
66 {
67 $this->remove($offset);
68 }
69
70 public function count(): int
71 {
72 return count($this->elements);
73 }
74
75 public function add(mixed $element): void
76 {
77 $this->elements[] = $element;
78 }
79
80 public function remove(string|int $key): void
81 {
82 if (!$this->containsKey($key)) {
83 throw new InvalidArgumentException(sprintf('Could not find an element for key: %s', $key));
84 }
85 unset($this->elements[$key]);
86 }
87
88 public function removeElement(mixed $element): void
89 {
90 $key = array_search($element, $this->elements, true);
91 if (false === $key) {
92 throw new InvalidArgumentException('Could not find an key for the passed element.');
93 }
94 unset($this->elements[$key]);
95 }
96
103 public function containsKey(string|int $key): bool
104 {
105 return isset($this->elements[$key]) || array_key_exists($key, $this->elements);
106 }
107
108 public function getKey($element): string|int
109 {
110 return array_search($element, $this->elements, true);
111 }
112
113 public function clear(): void
114 {
115 $this->elements = [];
116 }
117
118 public function contains(mixed $element): bool
119 {
120 return in_array($element, $this->elements, true);
121 }
122
123 public function get(string|int $key): mixed
124 {
125 return $this->elements[$key] ?? null;
126 }
127
128 public function set(string|int $key, mixed $value): void
129 {
130 $this->elements[$key] = $value;
131 }
132
133 public function isEmpty(): bool
134 {
135 return empty($this->elements);
136 }
137
138 public function getKeys(): array
139 {
140 return array_keys($this->elements);
141 }
142
143 public function getValues(): array
144 {
145 return array_values($this->elements);
146 }
147
148 public function filter(callable $callable): self
149 {
150 return new static(array_filter($this->elements, $callable));
151 }
152
153 public function slice(int $offset, ?int $length = null): self
154 {
155 return new static(array_slice($this->elements, $offset, $length, true));
156 }
157
158 public function toArray(): array
159 {
160 return $this->elements;
161 }
162
163 public function equals(mixed $other): bool
164 {
165 if (!($other instanceof self)) {
166 return false;
167 }
168
169 $self = $this->toArray();
170 $other = $other->toArray();
171
172 sort($self);
173 sort($other);
174
175 return $self == $other;
176 }
177}
Class ilBuddySystemArrayCollection A generic array based collection class.
containsKey(string|int $key)
isset is used for performance reasons (array_key_exists is much slower).
contains(mixed $element)
@phpstan-param T $element
filter(callable $callable)
Returns all the elements of this collection that satisfy the predicate $callable.
slice(int $offset, ?int $length=null)
Extracts a slice of $length elements starting at position $offset from the Collection.
getKeys()
Gets all indices of the collection.
getValues()
Gets all values of the collection.
add(mixed $element)
Adds an element at the end of the collection.
Interface ilBuddySystemCollection.