jmf/collection

Eases common operations with collections (arrays, iterables, etc).

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/jmf/collection

1.0.0 2025-12-15 01:42 UTC

This package is auto-updated.

Last update: 2025-12-15 01:45:49 UTC


README

Simple utilities to interact with collections (arrays, iterables, etc).

Installation

With composer:

composer require jmf/collection

Usage

Retrieving a single item from a collection

When you need to retrieve the only value from a collection which MUST contain exactly one item:

use Jmf\Collection\Collection;

$array = ['foo'];

// Will return 'foo'
$item = Collection::one($array);


$array = ['foo', 'bar'];

// Will throw an exception
$item = Collection::one($array);

If the collection does not contain exactly one item, an exception will be thrown.

When you need to retrieve the only value from a collection which CAN contain exactly one item:

use Jmf\Collection\Collection;

$array = ['foo'];

// Will return 'foo'
$item = Collection::oneOrNull($array);


$array = ['foo', 'bar'];

// Will return null
$item = Collection::oneOrNull($array);

If the collection does not contain exactly one item, null is returned.

Counting items in a collection

use Jmf\Collection\Collection;

$list = ['foo', 'bar', 'baz'];

// Will return 3
$count = Collection::count($list);

Retrieving items from a nested array structure

use Jmf\Collection\Collection;

$array = [
    'foo' => [
        'bar' => [
            'baz' => 'qux',
        ],
    ],
];

// Will return 'qux'
$item = Collection::deepGet($array, ['foo', 'bar', 'baz']);

Testing item existence from a nested array structure

use Jmf\Collection\Collection;

$array = [
    'foo' => [
        'bar' => [
            'baz' => 'qux',
        ],
    ],
];

// Will return true
$exists = Collection::deepHas($array, ['foo', 'bar', 'baz']);