dotink/flourish-collection

A rudimentary collection class

This package's canonical repository appears to be gone and the package has been frozen as a result.

Installs: 905

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 0

Open Issues: 0

Type:opus-package

1.1.6 2015-05-07 06:18 UTC

This package is auto-updated.

Last update: 2024-02-29 02:29:02 UTC


README

Build Status

This is a super simple collection class which is designed to be extended. It is mostely useful where you need selective collection querying or default values with ease.

Basic Usage

$collection = new Dotink\Flourish\Collection([
	'a' => 'apple',
	'b' => 'banana',
	'c' => 'carrot'
]);

Get a Value

$collection->get('a'); // returns 'apple'

Get a Value + Default

$collection->get('d', 'date'); // returns 'date'

Get Multiple Values

$collection->get(['a', 'c']); // returns ['a' => 'apple', 'c' => 'carrot']

Set a Value

$collection->set('d', 'date');

Set Multiple Values

$collection->set([
	'e' => 'eggplant',
	'f' => 'fig'
]);

Iterate Over the Collection

foreach ($collection as $letter => $fruit_or_vegetable) {
	echo sprintf(
		'The letter "%s" is for "%s"',
		$letter,
		$fruit_or_vegetable
	);
}

Compound Keys

It is possible to use compound keys to access data (both getting and setting) in nested arrays.

Set Nested Data

$collection->set('foo.bar', 'foobar'); // $collection->get('foo') returns ['bar' => 'foobar']

Get Nested Data

$collection->get('foo.bar'); // return 'foobar'