This library was extracted from the PinkCrab Plugin Framework (Perique)
Give access to a basic collection with all expected functionlaity, filtering, mapping, folding, sorting and comparing. But is also extendable for creating custom collections, which can be expanded and typed. A fairly simple, but extendable Collection.
composer install pink-crab/collection
See
./docs
for more details and examples.
$collection = new Collection(['1',2,'3']);
$collection->push(4);
$collection->apply(fn($e) => (string) $e);
var_dump($collection); // ['1','2','3','4'];
$collection = new Collection([1,2,3,4,5,6,7,8,9,10]);
$collection->filter(fn($e) => $e % 2 == 0);
var_dump($collection); // [2,4,6,8,10];
The Collection package comes with a few Traits which can be used when creating custom collections. These can either be created on the fly using anonymous classes or through defining them as full classes.
$indexed_collection = new class() extends \PinkCrab\Collection\Collection {
use \PinkCrab\Collection\Traits\Indexed;
};
$indexed_collection->set( 'key1', 'value1' );
$indexed_collection->has( 'key1' ); //true
var_dump( $indexed_collection );
// As a full class.
class Indexed_Collection extends \PinkCrab\Collection\Collection {
use \PinkCrab\Collection\Traits\Indexed;
};
$indexed_collection = new Indexed_Collection();
$indexed_collection->set( 'key1', 'value1' );
$indexed_collection->has( 'key1' ); //true
var_dump( $indexed_collection );
<?php
class Post_Collection extends Collection {
// Filter out anything not matching.
protected function map_construct( array $data ): array {
return array_filter(fn($e): bool => is_a($data, \WP_Post::class));
}
}
$posts = Post_Collection::from([$post1, null, $post2, false, WP_Error]);
var_dump($posts->to_array()); // [$post1, $post2];
$collection->each(function($e){
print $e->post_title . PHP_EOL;
});
// Post Title 1
// Post Title 2
- 0.2.0 - Added in option callbacks for diff and intersect, complete with helper functions for checking based on object instance or values/type. Also includes the group_by() method.
- 0.1.0 - Added Has_ArrayAccess and Is_Iterable traits to allow the implementation of the interfaces. Added docs from existing GitBook repo.
- 0.0.0 - Extracted from the PinkCrab Plugin Framework as a standalone package.