# wpide/3.4.2/App/Utils/Collection.php

WPIDE – File Manager &amp; Code Editor, version 3.4.2. 77 lines.

- Page: https://pluginprobe.com/plugins/wpide/3.4.2/code/App/Utils/Collection.php
- Raw: https://pluginprobe.com/plugins/wpide/3.4.2/raw/App/Utils/Collection.php
- Modified: 2022-07-30T20:33:10+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/wpide/3.4.2/code/App/Utils/Collection.php#L10-L20`.

```php
<?php

namespace WPIDE\App\Utils;

trait Collection
{
    protected $items = [];

    public function add($obj)
    {
        return $this->items[] = $obj;
    }

    public function delete($obj)
    {
        foreach ($this->items as $key => $item) {
            if ($item === $obj) {
                unset($this->items[$key]);
            }
        }
    }

    public function all(): array
    {
        return $this->items;
    }

    public function get($id, $key = 'id'): array
    {
        return array_filter( $this->items, function($item) use (&$id, &$key) {
            return $item[$key] == $id;
        });
    }

    public function length(): int
    {
        return count($this->items);
    }

    public function jsonSerialize(): array
    {
        return $this->items;
    }

    public function filter(callable $callback)
    {
        $this->items = array_filter($this->items, $callback);

        return $this;
    }

    public function map(callable $callback)
    {
        $this->items = array_map($callback, $this->items);

        return $this;
    }

    public function merge($collection) {

        $this->items = array_merge($this->items, $collection->items);
    }

    public function sortByValue($value, $desc = false)
    {
        usort($this->items, function ($a, $b) use ($value) {
            return $a[$value] <=> $b[$value];
        });

        if ($desc) {
            $this->items = array_reverse($this->items);
        }

        return $this;
    }
}

```
