Elgg  Version 4.3
Collection.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Collections;
4 
5 use ArrayAccess;
6 use Countable;
11 
18  Countable {
19 
23  protected $items = [];
24 
28  protected $item_class;
29 
33  protected $position;
34 
42  public function __construct($items = [], $item_class = null) {
43  if ($item_class) {
44  if (!is_subclass_of($item_class, CollectionItemInterface::class)) {
45  throw new InvalidArgumentException('Item class must implement ' . CollectionItemInterface::class);
46  }
47 
48  $this->item_class = $item_class;
49  }
50 
51  foreach ($items as $item) {
52  $this->add($item);
53  }
54  }
55 
64  protected function assertValidItem($item) {
65  $class = $this->item_class ? : CollectionItemInterface::class;
66 
67  if (!$item instanceof $class) {
68  throw new InvalidParameterException('Collection ' . __CLASS__ . ' only accepts instances of ' . $class);
69  }
70  }
71 
75  public function all() {
76  return $this->items;
77  }
78 
82  #[\ReturnTypeWillChange]
83  public function count() {
84  return count($this->items);
85  }
86 
90  public function add($item) {
91  $this->assertValidItem($item);
92 
93  $this->items[$item->getID()] = $item;
94 
95  return $this;
96  }
97 
101  public function get($id) {
102  return elgg_extract($id, $this->items);
103  }
104 
108  public function has($id) {
109  return array_key_exists($id, $this->items);
110  }
111 
115  public function remove($id) {
116  unset($this->items[$id]);
117  }
118 
122  public function fill($items) {
123  $this->items = [];
124  foreach ($items as $item) {
125  $this->add($item);
126  }
127 
128  return $this;
129  }
130 
134  public function merge($items) {
135  foreach ($items as $item) {
136  $this->add($item);
137  }
138 
139  return $this;
140  }
141 
145  public function filter(callable $callback = null) {
146  if ($callback) {
147  $items = array_filter($this->items, $callback);
148  } else {
149  $items = array_values($this->items);
150  }
151 
152  return new static($items, $this->item_class);
153  }
154 
158  public function sort(callable $callback = null) {
159  if (!$callback) {
160  $callback = function (CollectionItemInterface $f1, CollectionItemInterface $f2) {
161  $p1 = $f1->getPriority() ? : 500;
162  $p2 = $f2->getPriority() ? : 500;
163  if ($p1 === $p2) {
164  return 0;
165  }
166 
167  return $p1 < $p2 ? -1 : 1;
168  };
169  }
170 
171  uasort($this->items, $callback);
172 
173  return $this;
174  }
175 
189  public function walk(callable $callback) {
190  foreach ($this->items as $id => $item) {
191  call_user_func($callback, $item, $id);
192  }
193 
194  return $this;
195  }
196 
200  public function map(callable $callback) {
201  $map = [];
202 
203  $items = $this->filter()->all();
204  foreach ($items as $id => &$item) {
205  $map[$id] = call_user_func($callback, $item, $id);
206  }
207 
208  return $map;
209  }
210 
218  #[\ReturnTypeWillChange]
219  public function offsetExists($offset) {
220  return $this->has($offset);
221  }
222 
226  #[\ReturnTypeWillChange]
227  public function offsetGet($offset) {
228  return $this->get($offset);
229  }
230 
234  #[\ReturnTypeWillChange]
235  public function offsetSet($offset, $value) {
236  $this->assertValidItem($value);
237 
238  $key = $value->getID();
239  $this->items[$key] = $value;
240  }
241 
245  #[\ReturnTypeWillChange]
246  public function offsetUnset($offset) {
247  if (isset($this->items[$offset]) && isset($this->position)) {
248  // handle unset during iteration
249  $this->position--;
250  }
251 
252  unset($this->items[$offset]);
253  }
254 
262  #[\ReturnTypeWillChange]
263  public function current() {
264  $keys = array_keys($this->items);
265 
266  return $this->get($keys[$this->position]);
267  }
268 
272  #[\ReturnTypeWillChange]
273  public function next() {
274  $this->position++;
275  }
276 
280  #[\ReturnTypeWillChange]
281  public function key() {
282  $keys = array_keys($this->items);
283 
284  return $keys[$this->position];
285  }
286 
290  #[\ReturnTypeWillChange]
291  public function valid() {
292  $keys = array_keys($this->items);
293 
294  return isset($keys[$this->position]);
295  }
296 
300  #[\ReturnTypeWillChange]
301  public function rewind() {
302  $this->position = 0;
303  }
304 
308  #[\ReturnTypeWillChange]
309  public function seek($position) {
310  $keys = array_keys($this->items);
311 
312  if (!isset($keys[$position])) {
313  throw new OutOfBoundsException();
314  }
315 
316  $this->position = $position;
317  }
318 }
offsetSet($offset, $value)
{}
Definition: Collection.php:235
if(!$items) $item
Definition: delete.php:13
A collection of unique items.
Definition: Collection.php:15
Exception thrown if an argument is not of the expected type.
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:108
if(!$count) $offset
Definition: pagination.php:26
assertValidItem($item)
Validate if item is a valid collection item.
Definition: Collection.php:64
$keys
Definition: access.php:31
map(callable $callback)
{Walk through all items in the collection and apply a callback.Mappermixed}
Definition: Collection.php:200
Exception thrown if a value is not a valid key.
$value
Definition: generic.php:51
$class
Definition: summary.php:44
walk(callable $callback)
Walk through members of the collection and apply a callback.
Definition: Collection.php:189
current()
SeekableIterator interface functions.
Definition: Collection.php:263
__construct($items=[], $item_class=null)
Constructor.
Definition: Collection.php:42
count()
{Count collection items.int}
Definition: Collection.php:83
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:547
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:90
fill($items)
{Replace collection items.$items Itemsstatic}
Definition: Collection.php:122
all()
{Returns all collection items by reference.CollectionItemInterface[]}
Definition: Collection.php:75
filter(callable $callback=null)
{Filter collection items using a custom filter Returns a new collection instance.Filterstatic} ...
Definition: Collection.php:145
sort(callable $callback=null)
{Sort fields using custom callable If not provided, will sort items by priority.Sorterstatic} ...
Definition: Collection.php:158
$id
Generic annotation delete action.
Definition: delete.php:6
offsetExists($offset)
ArrayAccess interface functions.
Definition: Collection.php:219
merge($items)
{Add new items to collection, replacing items with matching IDs.$items Itemsstatic} ...
Definition: Collection.php:134