Elgg  Version 6.1
InMemory.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
13 final class InMemory implements Collection {
14 
20  private function __construct(protected array $items = []) {
21  }
22 
26  public function contains($item) {
27  return in_array($item, $this->items, true);
28  }
29 
33  #[\ReturnTypeWillChange]
34  public function count() {
35  return count($this->items);
36  }
37 
41  #[\ReturnTypeWillChange]
42  public function current() {
43  return current($this->items);
44  }
45 
49  public function filter(callable $filter) {
50  $results = [];
51 
52  foreach ($this->items as $item) {
53  if ($filter($item)) {
54  $results[] = $item;
55  }
56  }
57 
58  return new self($results);
59  }
60 
64  #[\ReturnTypeWillChange]
65  public function key() {
66  return key($this->items);
67  }
68 
72  public function map(callable $mapper) {
73  $results = [];
74  foreach ($this->items as $item) {
75  $results[] = $mapper($item);
76  }
77 
78  return self::fromArray($results);
79  }
80 
84  #[\ReturnTypeWillChange]
85  public function next() {
86  return next($this->items);
87  }
88 
92  #[\ReturnTypeWillChange]
93  public function rewind() {
94  reset($this->items);
95  }
96 
100  #[\ReturnTypeWillChange]
101  public function valid() {
102  return key($this->items) !== null;
103  }
104 
112  public static function fromArray(array $items) {
113  return new self($items);
114  }
115 }
if(!$items) $item
Definition: delete.php:13
static fromArray(array $items)
Factory function for converting from an array to a ton of items.
Definition: InMemory.php:112
map(callable $mapper)
Take items of the collection and return a new collection with all the items having the $mapper applie...
Definition: InMemory.php:72
Uses native PHP array to implement the Collection interface.
Definition: InMemory.php:13
contains($item)
Returns true iff the item is in this collection at least once.The object or value to check forboolean...
Definition: InMemory.php:26
filter(callable $filter)
Returns a new collection only containing the elements which pass the filter.Receives an item...
Definition: InMemory.php:49
$items
Definition: delete.php:8
$filter
Layout content filter.
Definition: filter.php:18
$results
Definition: content.php:22
A read-only interface to a (possibly mutable) group of items.
Definition: Collection.php:28