Elgg  Version 3.0
InMemory.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
13 final class InMemory implements Collection {
15  private $items;
16 
22  private function __construct(array $items = []) {
23  $this->items = $items;
24  }
25 
29  public function contains($item) {
30  return in_array($item, $this->items, true);
31  }
32 
36  public function count() {
37  return count($this->items);
38  }
39 
43  public function current() {
44  return current($this->items);
45  }
46 
50  public function filter(callable $filter) {
51  $results = [];
52 
53  foreach ($this->items as $item) {
54  if ($filter($item)) {
55  $results[] = $item;
56  }
57  }
58 
59  return new self($results);
60  }
61 
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  return self::fromArray($results);
78  }
79 
83  public function next() {
84  return next($this->items);
85  }
86 
90  public function rewind() {
91  reset($this->items);
92  }
93 
97  public function valid() {
98  return key($this->items) !== null;
99  }
100 
108  public static function fromArray(array $items) {
109  return new self($items);
110  }
111 }
static fromArray(array $items)
Factory function for converting from an array to a ton of items.
Definition: InMemory.php:108
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
if(!$items) $item
Definition: delete.php:13
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:29
filter(callable $filter)
Returns a new collection only containing the elements which pass the filter.Receives an item...
Definition: InMemory.php:50
A read-only interface to a (possibly mutable) group of items.
Definition: Collection.php:33
$filter
Layout body.
Definition: body.php:18