Elgg  Version 3.0
Collection.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Collections;
4 
5 use ArrayAccess;
6 use Countable;
10 
17  Countable {
18 
22  protected $items = [];
23 
27  protected $item_class;
28 
36  public function __construct($items = [], $item_class = null) {
37  if ($item_class) {
38  if (!is_subclass_of($item_class, CollectionItemInterface::class)) {
39  throw new InvalidArgumentException('Item class must implement ' . CollectionItemInterface::class);
40  }
41 
42  $this->item_class = $item_class;
43  }
44 
45  foreach ($items as $item) {
46  $this->add($item);
47  }
48  }
49 
57  protected function assertValidItem($item) {
58  $class = $this->item_class ? : CollectionItemInterface::class;
59 
60  if (!$item instanceof $class) {
61  throw new InvalidParameterException('Collection ' . __CLASS__ . ' only accepts instances of ' . $class);
62  }
63  }
64 
68  public function all() {
69  return $this->items;
70  }
71 
75  public function count() {
76  return count($this->items);
77  }
78 
82  public function add($item) {
83  $this->assertValidItem($item);
84 
85  $this->items[$item->getId()] = $item;
86 
87  return $this;
88  }
89 
93  public function get($id) {
94  return elgg_extract($id, $this->items);
95  }
96 
100  public function has($id) {
101  return array_key_exists($id, $this->items);
102  }
103 
107  public function remove($id) {
108  unset($this->items[$id]);
109  }
110 
114  public function fill($items) {
115  $this->items = [];
116  foreach ($items as $item) {
117  $this->add($item);
118  }
119 
120  return $this;
121  }
122 
126  public function merge($items) {
127  foreach ($items as $item) {
128  $this->add($item);
129  }
130 
131  return $this;
132  }
133 
137  public function filter(callable $callback = null) {
138  if ($callback) {
139  $items = array_filter($this->items, $callback);
140  } else {
141  $items = array_values($this->items);
142  }
143 
144  return new static($items, $this->item_class);
145  }
146 
150  public function sort(callable $callback = null) {
151  if (!$callback) {
152  $callback = function (CollectionItemInterface $f1, CollectionItemInterface $f2) {
153  $p1 = $f1->getPriority() ? : 500;
154  $p2 = $f2->getPriority() ? : 500;
155  if ($p1 === $p2) {
156  return 0;
157  }
158 
159  return $p1 < $p2 ? -1 : 1;
160  };
161  }
162 
163  uasort($this->items, $callback);
164 
165  return $this;
166  }
167 
181  public function walk(callable $callback) {
182  foreach ($this->items as $id => $item) {
183  call_user_func($callback, $item, $id);
184  }
185 
186  return $this;
187  }
188 
192  public function map(callable $callback) {
193  $map = [];
194 
195  $items = $this->filter()->all();
196  foreach ($items as $id => &$item) {
197  $map[$id] = call_user_func($callback, $item, $id);
198  }
199 
200  return $map;
201  }
202 
206  public function offsetExists($offset) {
207  return $this->has($offset);
208  }
209 
213  public function offsetGet($offset) {
214  return $this->get($offset);
215  }
216 
220  public function offsetSet($offset, $value) {
221  $this->assertValidItem($value);
222 
223  $key = $value->getId();
224  $this->items[$key] = $value;
225  }
226 
230  public function offsetUnset($offset) {
231  unset($this->items[$offset]);
232  }
233 
237  public function current() {
238  return current($this->items);
239  }
240 
244  public function next() {
245  next($this->items);
246  }
247 
251  public function key() {
252  return key($this->items);
253  }
254 
258  public function valid() {
259  return key($this->items) !== null;
260  }
261 
265  public function rewind() {
266  reset($this->items);
267  }
268 
272  public function seek($position) {
273  $keys = array_keys($this->items);
274 
275  if (isset($keys[$position])) {
276  throw new \OutOfBoundsException();
277  }
278 
279  $key = $keys[$position];
280 
281  return $this->items[$key];
282  }
283 
284 }
offsetSet($offset, $value)
{}
Definition: Collection.php:220
A collection of unique items.
Definition: Collection.php:14
if(!$items) $item
Definition: delete.php:13
getPriority()
Get priority (weight) of the item within a collection.
has($id)
{Check if collection has an item with a given ID.IDbool}
Definition: Collection.php:100
if(!$count) $offset
Definition: pagination.php:26
assertValidItem($item)
Validate if item is a valid collection item.
Definition: Collection.php:57
$keys
Definition: access.php:34
$position
Definition: move.php:7
map(callable $callback)
{Walk through all items in the collection and apply a callback.Mappermixed}
Definition: Collection.php:192
walk(callable $callback)
Walk through members of the collection and apply a callback.
Definition: Collection.php:181
__construct($items=[], $item_class=null)
Constructor.
Definition: Collection.php:36
$id
River item delete action.
Definition: delete.php:6
$class
Definition: field.php:29
count()
{Count collection items.int}
Definition: Collection.php:75
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1131
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:55
An interface for organizing items into collections.
add($item)
{Add a new item to collection.Itemstatic}
Definition: Collection.php:82
$value
Definition: debugging.php:7
fill($items)
{Replace collection items.$items Itemsstatic}
Definition: Collection.php:114
class
Definition: placeholder.php:21
all()
{Returns all collection items by reference.CollectionItemInterface[]}
Definition: Collection.php:68
filter(callable $callback=null)
{Filter collection items using a custom filter Returns a new collection instance.Filterstatic} ...
Definition: Collection.php:137
sort(callable $callback=null)
{Sort fields using custom callable If not provided, will sort items by priority.Sorterstatic} ...
Definition: Collection.php:150
merge($items)
{Add new items to collection, replacing items with matching IDs.$items Itemsstatic} ...
Definition: Collection.php:126