Elgg  Version 6.1
Collection.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Collections;
4 
7 
12  \ArrayAccess,
13  \SeekableIterator,
14  \Countable {
15 
19  protected $items = [];
20 
24  protected $item_class;
25 
29  protected $position;
30 
38  public function __construct($items = [], $item_class = null) {
39  if ($item_class) {
40  if (!is_subclass_of($item_class, CollectionItemInterface::class)) {
41  throw new InvalidArgumentException('Item class must implement ' . CollectionItemInterface::class);
42  }
43 
44  $this->item_class = $item_class;
45  }
46 
47  foreach ($items as $item) {
48  $this->add($item);
49  }
50  }
51 
60  protected function assertValidItem($item) {
61  $class = $this->item_class ?? CollectionItemInterface::class;
62 
63  if (!$item instanceof $class) {
64  throw new InvalidArgumentException('Collection ' . __CLASS__ . ' only accepts instances of ' . $class);
65  }
66  }
67 
71  public function all() {
72  return $this->items;
73  }
74 
78  #[\ReturnTypeWillChange]
79  public function count() {
80  return count($this->items);
81  }
82 
86  public function add($item) {
87  $this->assertValidItem($item);
88 
89  $this->items[$item->getID()] = $item;
90 
91  return $this;
92  }
93 
97  public function get($id) {
98  return elgg_extract($id, $this->items);
99  }
100 
104  public function has($id) {
105  return array_key_exists($id, $this->items);
106  }
107 
111  public function remove($id) {
112  unset($this->items[$id]);
113  }
114 
118  public function fill($items) {
119  $this->items = [];
120  foreach ($items as $item) {
121  $this->add($item);
122  }
123 
124  return $this;
125  }
126 
130  public function merge($items) {
131  foreach ($items as $item) {
132  $this->add($item);
133  }
134 
135  return $this;
136  }
137 
141  public function filter(callable $callback = null) {
142  if ($callback) {
143  $items = array_filter($this->items, $callback);
144  } else {
145  $items = array_values($this->items);
146  }
147 
148  return new static($items, $this->item_class);
149  }
150 
154  public function sort(callable $callback = null) {
155  if (!$callback) {
156  $callback = function (CollectionItemInterface $f1, CollectionItemInterface $f2) {
157  $p1 = $f1->getPriority() ?: 500;
158  $p2 = $f2->getPriority() ?: 500;
159  if ($p1 === $p2) {
160  return 0;
161  }
162 
163  return $p1 < $p2 ? -1 : 1;
164  };
165  }
166 
167  uasort($this->items, $callback);
168 
169  return $this;
170  }
171 
185  public function walk(callable $callback) {
186  foreach ($this->items as $id => $item) {
187  call_user_func($callback, $item, $id);
188  }
189 
190  return $this;
191  }
192 
196  public function map(callable $callback) {
197  $map = [];
198 
199  $items = $this->filter()->all();
200  foreach ($items as $id => &$item) {
201  $map[$id] = call_user_func($callback, $item, $id);
202  }
203 
204  return $map;
205  }
206 
214  #[\ReturnTypeWillChange]
215  public function offsetExists($offset) {
216  return $this->has($offset);
217  }
218 
222  #[\ReturnTypeWillChange]
223  public function offsetGet($offset) {
224  return $this->get($offset);
225  }
226 
230  #[\ReturnTypeWillChange]
231  public function offsetSet($offset, $value) {
232  $this->assertValidItem($value);
233 
234  $key = $value->getID();
235  $this->items[$key] = $value;
236  }
237 
241  #[\ReturnTypeWillChange]
242  public function offsetUnset($offset) {
243  if (isset($this->items[$offset]) && isset($this->position)) {
244  // handle unset during iteration
245  $this->position--;
246  }
247 
248  unset($this->items[$offset]);
249  }
250 
258  #[\ReturnTypeWillChange]
259  public function current() {
260  $keys = array_keys($this->items);
261 
262  return $this->get($keys[$this->position]);
263  }
264 
268  #[\ReturnTypeWillChange]
269  public function next() {
270  $this->position++;
271  }
272 
276  #[\ReturnTypeWillChange]
277  public function key() {
278  $keys = array_keys($this->items);
279 
280  return $keys[$this->position];
281  }
282 
286  #[\ReturnTypeWillChange]
287  public function valid() {
288  $keys = array_keys($this->items);
289 
290  return isset($keys[$this->position]);
291  }
292 
296  #[\ReturnTypeWillChange]
297  public function rewind() {
298  $this->position = 0;
299  }
300 
304  #[\ReturnTypeWillChange]
305  public function seek($offset) {
306  $keys = array_keys($this->items);
307 
308  if (!isset($keys[$offset])) {
309  throw new OutOfBoundsException();
310  }
311 
312  $this->position = $offset;
313  }
314 }
offsetSet($offset, $value)
{}
Definition: Collection.php:231
if(!$items) $item
Definition: delete.php:13
A collection of unique items.
Definition: Collection.php:11
Exception thrown if an argument is not of the expected type.
getPriority()
Get priority (weight) of the item within a collection.
if(empty($count)) $offset
Definition: pagination.php:26
has($id)
{Check if collection has an item with a given ID.IDbool}
Definition: Collection.php:104
assertValidItem($item)
Validate if item is a valid collection item.
Definition: Collection.php:60
$keys
Definition: access.php:31
map(callable $callback)
{Walk through all items in the collection and apply a callback.Mappermixed}
Definition: Collection.php:196
Exception thrown if a value is not a valid key.
$value
Definition: generic.php:51
$class
Definition: summary.php:44
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:256
walk(callable $callback)
Walk through members of the collection and apply a callback.
Definition: Collection.php:185
current()
SeekableIterator interface functions.
Definition: Collection.php:259
__construct($items=[], $item_class=null)
Constructor.
Definition: Collection.php:38
count()
{Count collection items.int}
Definition: Collection.php:79
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
An interface for organizing items into collections.
add($item)
{Add a new item to collection.Itemstatic}
Definition: Collection.php:86
fill($items)
{Replace collection items.$items Itemsstatic}
Definition: Collection.php:118
all()
{Returns all collection items by reference.CollectionItemInterface[]}
Definition: Collection.php:71
filter(callable $callback=null)
{Filter collection items using a custom filter Returns a new collection instance.Filterstatic} ...
Definition: Collection.php:141
sort(callable $callback=null)
{Sort fields using custom callable If not provided, will sort items by priority.Sorterstatic} ...
Definition: Collection.php:154
$id
Generic annotation delete action.
Definition: delete.php:6
offsetExists($offset)
ArrayAccess interface functions.
Definition: Collection.php:215
merge($items)
{Add new items to collection, replacing items with matching IDs.$items Itemsstatic} ...
Definition: Collection.php:130