ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
AutoresponderArrayCollection.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use InvalidArgumentException;
24use ArrayIterator;
25
27{
31 public function __construct(private array $elements = [])
32 {
33 }
34
35 public function getIterator(): ArrayIterator
36 {
37 return new ArrayIterator($this->elements);
38 }
39
40 public function offsetExists(int $offset): bool
41 {
42 return $this->containsKey($offset);
43 }
44
45 public function offsetGet(int $offset): AutoresponderDto
46 {
47 return $this->get($offset);
48 }
49
50 public function offsetSet(int $offset, AutoresponderDto $value): void
51 {
52 if (!isset($offset)) {
53 $this->add($value);
54
55 return;
56 }
57
58 $this->set($offset, $value);
59 }
60
61 public function offsetUnset(int $offset): void
62 {
63 $this->remove($offset);
64 }
65
66 public function count(): int
67 {
68 return \count($this->elements);
69 }
70
71 public function add(AutoresponderDto $element): void
72 {
73 $this->elements[] = $element;
74 }
75
76 public function remove($key): void
77 {
78 if (!$this->containsKey($key)) {
79 throw new InvalidArgumentException("Key $key does not exist.");
80 }
81 unset($this->elements[$key]);
82 }
83
84 public function removeElement(AutoresponderDto $element): void
85 {
86 $key = array_search($element, $this->elements, true);
87 if ($key === false) {
88 throw new InvalidArgumentException('Could not find an key for the passed element.');
89 }
90 unset($this->elements[$key]);
91 }
92
93 public function containsKey($key): bool
94 {
95 return isset($this->elements[$key]);
96 }
97
98 public function getKey(AutoresponderDto $element): int
99 {
100 $key = array_search($element, $this->elements, true);
101 if ($key === false) {
102 throw new InvalidArgumentException('Could not find an key for the passed element.');
103 }
104 return $key;
105 }
106
107 public function clear(): void
108 {
109 $this->elements = [];
110 }
111
112 public function contains(AutoresponderDto $element): bool
113 {
114 return \in_array($element, $this->elements, true);
115 }
116
117 public function get($key): ?AutoresponderDto
118 {
119 return $this->elements[$key] ?? null;
120 }
121
122 public function set($key, AutoresponderDto $value): void
123 {
124 $this->elements[$key] = $value;
125 }
126
127 public function isEmpty(): bool
128 {
129 return empty($this->elements);
130 }
131
132 public function getKeys(): array
133 {
134 return array_keys($this->elements);
135 }
136
137 public function getValues(): array
138 {
139 return array_values($this->elements);
140 }
141
142 public function filter(callable $callable): AutoresponderCollection
143 {
144 $filtered = array_filter($this->elements, $callable);
145 return new self($filtered);
146 }
147
148 public function slice(int $offset, ?int $length = null): AutoresponderCollection
149 {
150 $sliced = \array_slice($this->elements, $offset, $length, true);
151 return new self($sliced);
152 }
153
154 public function toArray(): array
155 {
156 return $this->elements;
157 }
158
159 public function equals($other): bool
160 {
161 if (!$other instanceof self) {
162 return false;
163 }
164
165 $self = $this->toArray();
166 $other = $other->toArray();
167
168 sort($self);
169 sort($other);
170
171 return $self === $other;
172 }
173}