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)) {
248 unset($this->items[$offset]);
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);
offsetSet($offset, $value)
{}
A collection of unique items.
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}
Collection item interface.
assertValidItem($item)
Validate if item is a valid collection item.
map(callable $callback)
{Walk through all items in the collection and apply a callback.Mappermixed}
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.
walk(callable $callback)
Walk through members of the collection and apply a callback.
current()
SeekableIterator interface functions.
__construct($items=[], $item_class=null)
Constructor.
count()
{Count collection items.int}
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
An interface for organizing items into collections.
add($item)
{Add a new item to collection.Itemstatic}
fill($items)
{Replace collection items.$items Itemsstatic}
all()
{Returns all collection items by reference.CollectionItemInterface[]}
filter(callable $callback=null)
{Filter collection items using a custom filter Returns a new collection instance.Filterstatic} ...
sort(callable $callback=null)
{Sort fields using custom callable If not provided, will sort items by priority.Sorterstatic} ...
$id
Generic annotation delete action.
offsetExists($offset)
ArrayAccess interface functions.
merge($items)
{Add new items to collection, replacing items with matching IDs.$items Itemsstatic} ...