ILIAS  release_8 Revision v8.24
class.ilBuddySystemArrayCollection.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 private array $elements;
29
30 public function __construct(array $elements = [])
31 {
32 $this->elements = $elements;
33 }
34
38 public function getIterator()
39 {
40 return new ArrayIterator($this->elements);
41 }
42
46 public function offsetExists($offset): bool
47 {
48 return $this->containsKey($offset);
49 }
50
54 public function offsetGet($offset)
55 {
56 return $this->get($offset);
57 }
58
62 public function offsetSet($offset, $value): void
63 {
64 if (!isset($offset)) {
65 $this->add($value);
66
67 return;
68 }
69
70 $this->set($offset, $value);
71 }
72
76 public function offsetUnset($offset): void
77 {
78 $this->remove($offset);
79 }
80
84 public function count(): int
85 {
86 return count($this->elements);
87 }
88
92 public function add($element): void
93 {
94 $this->elements[] = $element;
95 }
96
100 public function remove($key): void
101 {
102 if (!$this->containsKey($key)) {
103 throw new InvalidArgumentException(sprintf('Could not find an element for key: %s', $key));
104 }
105 unset($this->elements[$key]);
106 }
107
111 public function removeElement($element): void
112 {
113 $key = array_search($element, $this->elements, true);
114 if (false === $key) {
115 throw new InvalidArgumentException('Could not find an key for the passed element.');
116 }
117 unset($this->elements[$key]);
118 }
119
126 public function containsKey($key): bool
127 {
128 return isset($this->elements[$key]) || array_key_exists($key, $this->elements);
129 }
130
134 public function getKey($element)
135 {
136 return array_search($element, $this->elements, true);
137 }
138
142 public function clear(): void
143 {
144 $this->elements = [];
145 }
146
150 public function contains($element): bool
151 {
152 return in_array($element, $this->elements, true);
153 }
154
158 public function get($key)
159 {
160 return $this->elements[$key] ?? null;
161 }
162
166 public function set($key, $value): void
167 {
168 $this->elements[$key] = $value;
169 }
170
174 public function isEmpty(): bool
175 {
176 return empty($this->elements);
177 }
178
182 public function getKeys(): array
183 {
184 return array_keys($this->elements);
185 }
186
190 public function getValues(): array
191 {
192 return array_values($this->elements);
193 }
194
198 public function filter(callable $callable): ilBuddySystemCollection
199 {
200 return new static(array_filter($this->elements, $callable));
201 }
202
206 public function slice(int $offset, int $length = null): ilBuddySystemCollection
207 {
208 return new static(array_slice($this->elements, $offset, $length, true));
209 }
210
214 public function toArray(): array
215 {
216 return $this->elements;
217 }
218
219 public function equals($other): bool
220 {
221 if (!($other instanceof self)) {
222 return false;
223 }
224
225 $self = $this->toArray();
226 $other = $other->toArray();
227
228 sort($self);
229 sort($other);
230
231 return $self == $other;
232 }
233}
Class ilBuddySystemArrayCollection A collection which contains all entries of a buddy list.
slice(int $offset, int $length=null)
@inheritDoc
containsKey($key)
isset is used for performance reasons (array_key_exists is much slower).
Interface ilBuddySystemCollection.
string $key
Consumer key/client ID value.
Definition: System.php:193