40 if (!is_subclass_of(
$item_class, CollectionItemInterface::class)) {
61 $class = $this->item_class ?? CollectionItemInterface::class;
71 public function all() {
78 #[\ReturnTypeWillChange]
80 return count($this->items);
97 public function get(
$id) {
105 return array_key_exists(
$id, $this->items);
111 public function remove(
$id) {
112 unset($this->items[
$id]);
141 public function filter(?callable $callback =
null) {
143 $items = array_filter($this->items, $callback);
145 $items = array_values($this->items);
154 public function sort(?callable $callback =
null) {
158 $p2 = $f2->getPriority() ?: 500;
163 return $p1 < $p2 ? -1 : 1;
167 uasort($this->items, $callback);
185 public function walk(callable $callback) {
186 foreach ($this->items as
$id =>
$item) {
187 call_user_func($callback,
$item,
$id);
196 public function map(callable $callback) {
214 #[\ReturnTypeWillChange]
222 #[\ReturnTypeWillChange]
230 #[\ReturnTypeWillChange]
241 #[\ReturnTypeWillChange]
243 if (isset($this->items[
$offset]) && isset($this->position)) {
258 #[\ReturnTypeWillChange]
260 $keys = array_keys($this->items);
268 #[\ReturnTypeWillChange]
276 #[\ReturnTypeWillChange]
278 $keys = array_keys($this->items);
286 #[\ReturnTypeWillChange]
288 $keys = array_keys($this->items);
290 return isset(
$keys[$this->position]);
296 #[\ReturnTypeWillChange]
304 #[\ReturnTypeWillChange]
306 $keys = array_keys($this->items);
$id
Generic annotation delete action.
A collection of unique items.
current()
SeekableIterator interface functions.
map(callable $callback)
{Walk through all items in the collection and apply a callback.Mappermixed}
all()
{Returns all collection items by reference.CollectionItemInterface[]}
offsetExists($offset)
ArrayAccess interface functions.
__construct($items=[], $item_class=null)
Constructor.
merge($items)
{Add new items to collection, replacing items with matching IDs.Itemsstatic}
fill($items)
{Replace collection items.Itemsstatic}
walk(callable $callback)
Walk through members of the collection and apply a callback.
add($item)
{Add a new item to collection.Itemstatic}
has($id)
{Check if collection has an item with a given ID.IDbool}
filter(?callable $callback=null)
{Filter collection items using a custom filter Returns a new collection instance.Filterstatic}
assertValidItem($item)
Validate if item is a valid collection item.
offsetSet($offset, $value)
{}
count()
{Count collection items.int}
sort(?callable $callback=null)
{Sort fields using custom callable If not provided, will sort items by priority.Sorterstatic}
Exception thrown if an argument is not of the expected type.
Exception thrown if a value is not a valid key.
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
An interface for organizing items into collections.
Collection item interface.
getPriority()
Get priority (weight) of the item within a collection.
if($container instanceof ElggGroup && $container->guid !=elgg_get_page_owner_guid()) $key