ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilBuddySystemArrayCollection.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2/* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3
10{
12 private $elements = [];
13
18 public function __construct(array $elements = [])
19 {
20 $this->elements = $elements;
21 }
22
26 public function getIterator()
27 {
28 return new ArrayIterator($this->elements);
29 }
30
34 public function offsetExists($offset)
35 {
36 return $this->containsKey($offset);
37 }
38
42 public function offsetGet($offset)
43 {
44 return $this->get($offset);
45 }
46
50 public function offsetSet($offset, $value)
51 {
52 if (!isset($offset)) {
53 $this->add($value);
54 return;
55 }
56
57 $this->set($offset, $value);
58 }
59
63 public function offsetUnset($offset)
64 {
65 $this->remove($offset);
66 }
67
71 public function count()
72 {
73 return count($this->elements);
74 }
75
79 public function add($element) : void
80 {
81 $this->elements[] = $element;
82 }
83
87 public function remove($key) : void
88 {
89 if (!isset($this->elements[$key]) && !array_key_exists($key, $this->elements)) {
90 throw new InvalidArgumentException(sprintf("Could not find an element for key: %s", $key));
91 }
92 unset($this->elements[$key]);
93 }
94
98 public function removeElement($element) : void
99 {
100 $key = array_search($element, $this->elements, true);
101 if (false === $key) {
102 throw new InvalidArgumentException("Could not find an key for the passed element.");
103 }
104 unset($this->elements[$key]);
105 }
106
110 public function containsKey($key) : bool
111 {
112 return isset($this->elements[$key]) || array_key_exists($key, $this->elements);
113 }
114
118 public function getKey($element)
119 {
120 return array_search($element, $this->elements, true);
121 }
122
126 public function clear() : void
127 {
128 $this->elements = [];
129 }
130
134 public function contains($element) : bool
135 {
136 return in_array($element, $this->elements, true);
137 }
138
142 public function get($key)
143 {
144 return isset($this->elements[$key]) ? $this->elements[$key] : null;
145 }
146
150 public function set($key, $value) : void
151 {
152 $this->elements[$key] = $value;
153 }
154
158 public function isEmpty() : bool
159 {
160 return empty($this->elements);
161 }
162
166 public function getKeys() : array
167 {
168 return array_keys($this->elements);
169 }
170
174 public function getValues() : array
175 {
176 return array_values($this->elements);
177 }
178
182 public function filter(callable $callable)
183 {
184 return new static(array_filter($this->elements, $callable));
185 }
186
190 public function slice($offset, $length = null)
191 {
192 return new static(array_slice($this->elements, $offset, $length, true));
193 }
194
198 public function toArray() : array
199 {
200 return $this->elements;
201 }
202}
An exception for terminatinating execution or to throw for unit testing.
Class ilBuddySystemArrayCollection A collection which contains all entries of a buddy list.
slice($offset, $length=null)
@inheritDoc
__construct(array $elements=[])
ilBuddySystemArrayCollection constructor.
Interface ilBuddySystemCollection.