![]() |
VOOZH | about |
You can obtain free community support for example through stackoverflow.
If you think you found a bug, please create a ticket in the bug tracker.
If you take code quality seriously, try out the new continuous inspection service.
scrutinizer-ci.com
This library adds basic collections for PHP.
Collections can be seen as more specialized arrays for which certain contracts are guaranteed.
Supported Collections:
Sequence, SortedSequenceMap, ObjectMap (not yet implemented)SetGeneral Characteristics:
PHP Collection can easily be installed via composer
composer require phpcollection/phpcollection
or add it to your composer.json file.
Collection classes provide a rich API.
In a Set each value is guaranteed to be unique. The Set class supports objects, and scalars as value. Equality
is determined via the following steps.
Equality of Objects
- If an object implements
ObjectBasics, equality is determined by theequals()method.- If an object has an external handler like the
DateTimethat was registered viaObjectBasicsHandlerRegistry::registerHandlerFor, equality is determined by that handler?sequals()method.- If none of the above is applicable, equality is determined by identity
$a === $b.
Equality of Scalars
Scalar are considered equal if$a === $bis true.
$set = new Set(); $set->add(new \DateTime('today')); $set->add(new \DateTime('today')); var_dump(count($set)); // int(1) -> the same date is not added twice foreach ($set as $date) { var_dump($date); } $set->all(); $set->addSet($otherSet); $set->addAll($someElements);
// Read Operations $seq = new Sequence([0, 2, 3, 2]); $seq->get(2); // int(3) $seq->all(); // [0, 2, 3, 2] $seq->first(); // Some(0) $seq->last(); // Some(2) // Write Operations $seq = new Sequence([1, 5]); $seq->get(0); // int(1) $seq->update(0, 4); $seq->get(0); // int(4) $seq->remove(0); $seq->get(0); // int(5) $seq = new Sequence([1, 4]); $seq->add(2); $seq->all(); // [1, 4, 2] $seq->addAll(array(4, 5, 2)); $seq->all(); // [1, 4, 2, 4, 5, 2] // Sort $seq = new Sequence([0, 5, 4, 2]); $seq->sortWith(function($a, $b) { return $a - $b; }); $seq->all(); // [0, 2, 4, 5]
// Read Operations $map = new Map(['foo' => 'bar', 'baz' => 'boo']); $map->get('foo'); // Some('bar') $map->get('foo')->get(); // string('bar') $map->keys(); // ['foo', 'baz'] $map->values(); // ['bar', 'boo'] iterator_to_array($map); // ['foo' => 'bar', 'baz' => 'boo'] $map->first()->get(); // ['foo', 'bar'] $map->last()->get(); // ['baz', 'boo'] // Write Operations $map = new Map(); $map->set('foo', 'bar'); $map->setAll(array('bar' => 'baz', 'baz' => 'boo')); $map->remove('foo'); // Sort $map->sortWith('strcmp');
The code is released under the business-friendly Apache2 license.
Documentation is subject to the Attribution-NonCommercial-NoDerivs 3.0 Unported license.