idimsh/collection

A generic objects/variables holder and iterator

dev-master 2020-08-30 11:25 UTC

This package is auto-updated.

Last update: 2024-04-29 03:57:08 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Software License

Collection: A Generic PHP Objects/Variables Holder and Iterator

A library to store variables in an iterator, support some operations like sorting and swapping elements.

Requirements

  • PHP >= 5.6

Installation

$ composer require idimsh/collection

in composer.json:

"require": {
  "idimsh/collection": "dev-master"
}

Usage

The class \Dimsh\Models\Collections\Collection is meant to be extended by your implementation of the list you want to create, extending the class will provide type hints for autocomplete and it is not a must.
like:

use Dimsh\Models\Collections;

/**
 * Class DefinitionsList
 *
 * @method \Definition offsetGet();
 * @method \Definition current()
 * @method \Definition last()
 * @method \Definition first()
 *
 */
class DefinitionsList extends Collection {
    /**
     * @param \Definition $value
     *
     * @throws \Exception
     */
    protected function preAdd($value) {
        if (! $value instanceof \Definition) {
            throw new \Exception("DefinitionsList can accept items of type Definition only");
        }
    }
}

$list = new \DefinitionsList;
$list[] = new \Definition;
$list['string-index'] = new \Definition;
$list->add(new \Definition);

// Looping
for ($list->rewind(); $list->valid(); $list->next()) {
    $definition = $list->current();
    $index = $list->key();
}

Useful Methods:

/**
 * @var \Dimsh\Models\Collections\Collection $list
 * @var \Dimsh\Models\Collections\Collection $another_list
 */
 
$list->first(); // Get the first item
$list->last(); // Get the last

$list->swap($offset1, $offset2); // swap items
$list->diffKey($another_list); // Get array diff by keys (offsets)
$list->intersectKey($another_list); // get array_intersect_key using offsets.

Why

To find a way to store objects inside an array-like object in order for the collection to be passable by reference, storing objects in PHP Arrays will not provide this feature.

License

MIT

Alternatives

\SplObjectStorage Provides a way to store objects, this package adds the array functions to the collection of objects.