Elgg  Version 5.1
InMemory.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
13 final class InMemory implements Collection {
14 
16  private $items;
17 
23  private function __construct(array $items = []) {
24  $this->items = $items;
25  }
26 
30  public function contains($item) {
31  return in_array($item, $this->items, true);
32  }
33 
37  #[\ReturnTypeWillChange]
38  public function count() {
39  return count($this->items);
40  }
41 
45  #[\ReturnTypeWillChange]
46  public function current() {
47  return current($this->items);
48  }
49 
53  public function filter(callable $filter) {
54  $results = [];
55 
56  foreach ($this->items as $item) {
57  if ($filter($item)) {
58  $results[] = $item;
59  }
60  }
61 
62  return new self($results);
63  }
64 
68  #[\ReturnTypeWillChange]
69  public function key() {
70  return key($this->items);
71  }
72 
76  public function map(callable $mapper) {
77  $results = [];
78  foreach ($this->items as $item) {
79  $results[] = $mapper($item);
80  }
81 
82  return self::fromArray($results);
83  }
84 
88  #[\ReturnTypeWillChange]
89  public function next() {
90  return next($this->items);
91  }
92 
96  #[\ReturnTypeWillChange]
97  public function rewind() {
98  reset($this->items);
99  }
100 
104  #[\ReturnTypeWillChange]
105  public function valid() {
106  return key($this->items) !== null;
107  }
108 
116  public static function fromArray(array $items) {
117  return new self($items);
118  }
119 }
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:116
map(callable $mapper)
Take items of the collection and return a new collection with all the items having the $mapper applie...
Definition: InMemory.php:76
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:30
filter(callable $filter)
Returns a new collection only containing the elements which pass the filter.Receives an item...
Definition: InMemory.php:53
$results
Definition: content.php:22
A read-only interface to a (possibly mutable) group of items.
Definition: Collection.php:28
$filter
Layout body.
Definition: body.php:10