An open API service indexing awesome lists of open source software.

https://github.com/kuria/collections

Object-oriented collection structures
https://github.com/kuria/collections

collection map php

Last synced: 12 months ago
JSON representation

Object-oriented collection structures

Awesome Lists containing this project

README

          

Collections
###########

Object-oriented collection structures.

.. image:: https://travis-ci.com/kuria/collections.svg?branch=master
:target: https://travis-ci.com/kuria/collections

.. contents::

Features
********

- ``Collection`` - list of values with sequential integer indexes
- ``Map`` - key-value map

Requirements
************

- PHP 7.1+

Collection
**********

The ``Collection`` class implements a list of values with sequential integer indexes.

It also implements ``Countable``, ``ArrayAccess`` and ``IteratorAggregate``.

Creating a new collection
=========================

Empty collection
----------------

.. code:: php

`_
------------------------------------------------------------------------------------

.. code:: php

setValues($collection->sort());

Array access and iteration
==========================

``Collection`` instances can be accessed and iterated as regular arrays.

.. code:: php

$value) {
echo $index, ': ', $value, "\n";
}

Output:

::

Value at index 1 is new bar
Value at index 2 does not exist
There are 2 values in total
0: foo
1: new bar

Map
***

The ``Map`` class implements a key value map.

It also implements ``Countable``, ``ArrayAccess`` and ``IteratorAggregate``.

Creating a new map
==================

Empty map
---------

.. code:: php

`_
------------------------------------------------------------------------------------

.. code:: php

'bar', 'bar' => 'baz']);

Map method overview
===================

Refer to doc comments of the respective methods for more information.

Static methods
--------------

- ``create($pairs = null): self`` - create a map from an iterable
- ``map($iterable, $mapper): self`` - map values of the given iterable using a callback
- ``build($iterable, $mapper): self`` - build a map from an iterable using a callback
- ``combine($keys, $values): self`` - combine a list of keys and a list of values to create a map

Instance methods
----------------

- ``setPairs($pairs): void`` - replace all pairs with the given iterable
- ``toArray(): array`` - get all pairs as an array
- ``isEmpty(): bool`` - see if the map is empty
- ``count(): int`` - count pairs
- ``has($key): bool`` - see if the given key exists
- ``contains($value, $strict = true): bool`` - see if the given value exists
- ``find($value, $strict = true): string|int|null`` - try to find the first occurence of a value
- ``get($key): mixed`` - get value for the given key
- ``values(): Collection`` - get all values
- ``keys(): Collection`` - get all keys
- ``set($key, $value): void`` - define a pair
- ``add(...$iterables): void`` - add pairs from other iterables to this map
- ``fill($keys, $value): void`` - fill specific keys with a value
- ``remove(...$keys): void`` - remove pairs with the given keys
- ``clear(): void`` - remove all pairs
- ``reduce($reducer, $initial = null): mixed`` - reduce the map to a single value
- ``flip(): self`` - swap keys and values
- ``shuffle(): self`` - randomize pair order
- ``column($key, $indexBy = null): self`` - gather values from properties or array keys of all object or array values
- ``filter($filter): self`` - filter pairs using the given callback
- ``apply($callback): self`` - apply the callback to all pairs
- ``map($mapper): self`` - remap pairs using the given callback
- ``intersect(...$iterables): self`` - compute an intersection with the given iterables
- ``uintersect($comparator, ...$iterables): self`` - compute an intersection with the given iterables using a custom comparator
- ``intersectKeys(...$iterables): self`` - compute a key intersection with the given iterables
- ``uintersectKeys($comparator, ...$iterables): self`` - compute a key intersection with the given iterables using a custom comparator
- ``diff(...$iterables): self`` - compute a difference between this map and the given iterables
- ``udiff($comparator, ...$iterables): self`` - compute a difference between this map and the given iterables using a custom comparator
- ``diffKeys(...$iterables): self`` - compute a key difference between this map and the given iterables
- ``udiffKeys($comparator, ...$iterables): self`` - compute a key difference between this map and the given iterables using a custom comparator
- ``sort($flags = SORT_REGULAR, $reverse = false): self`` - sort the map using its values
- ``usort($comparator): self`` - sort the map using its values and a custom comparator
- ``ksort($flags = SORT_REGULAR, $reverse = false): self`` - sort the map using its keys
- ``uksort(): self`` - sort the map using its keys and a custom comparator

.. NOTE::

Any method that returns ``self`` returns a new map instance with the selected or modified pairs.
The original map is not changed.

If updating the original map is needed, use ``setPairs()`` to do so, e.g.:

.. code:: php

setPairs($map->sort());

Array access and iteration
==========================

``Map`` instances can be accessed and iterated as regular arrays.

.. code:: php

$value) {
echo $key, ': ', $value, "\n";
}

Output:

::

Value with key "foo" is bar
Value with key "baz" does not exist
There are 2 pairs in total
foo: bar
quux: quuz