yannoff / collections
A simple object implementation for PHP arrays
Requires (Dev)
README
A simple object implementation of PHP arrays.
The concept
Based upon the Decorator design pattern, the aim is to provide a flexible, object-oriented alternative to PHP Arrays.
Installation
Using composer:
$ composer require yannoff/collections
Usage
Example: PHP Array vs Collection
The classic way - native PHP arrays:
<?php // Using PHP native arrays, the classic way: $countries = [ 'France', 'Italia' ]; // Push an element array_push($countries, 'Switzerland'); // Append an element $countries[] = 'Belgium';
The new way - using collections:
<?php use Yannoff\Component\Collections\Collection; // Using collections: $countries = new Collection([ 'France', 'Italia' ]); // Push an element $countries->push('Switzerland'); // Append an element: the same syntax as for arrays $countries[] = 'Belgium'; // Exporting the collection back to a native PHP array: $array = $countries->all(); // As for native arrays, collections can be traversed with a foreach: foreach($countries as $key => $value) { // ... }
Methods
See the full method list section for more details.
Array / Collection method equivalences:
Along with those classical PHP methods wrappers, a few bag methods are provided:
Full method list
Synopsis
use Yannoff\Component\Collections\Collection;
__construct()
Collection constructor.
Arguments
Return value
No return value.
push()
Push one element onto the end of the collection.
Arguments
Return value
pop()
Pop the element of the end of the collection and return it.
Arguments
No arguments.
Return value
slice()
Extract a slice of $length elements from $offset-th element.
Arguments
Return value
shift()
Shift an element off the beginning of the collection and return it.
Arguments
No arguments.
Return value
unshift()
Prepend an element to the beginning of the collection.
NOTE: Prepending multiple elemnts is not supported.
Arguments
Return value
keys()
Return all the keys of the collection.
NOTE: Filtering on a search value to fetch a subset of the keys is not implemented.
Arguments
No arguments.
Return value
search()
Search the collection for a given value and returns the first corresponding key if successful.
NOTE: The strict flag is not implemented for now.
Arguments
Return value
join()
Build a string by concatening all elements with the given glue.
Arguments
Return value
reverse()
Return all elements of the collection in reversed order.
Arguments
No arguments.
Return value
flip()
Flip collection: set value as keys & keys as values.
Arguments
No arguments.
Return value
filter()
Apply a user supplied function to filter elements.
Arguments
Possible values for the $flag parameter:
- ARRAY_FILTER_USE_KEY: pass key as the only argument to callback instead of the value.
- ARRAY_FILTER_USE_BOTH: pass both value and key as arguments to callback instead of the value.
Default is 0 which will pass value as the only argument to callback instead.
Return value
walk()
Apply a user supplied function to each element of the collection.
Arguments
Return value
current()
Returns the element that's currently being pointed to by the internal pointer.
Arguments
No arguments.
Return value
end()
Advances internal pointer to the last element, and returns its value
Arguments
No arguments.
Return value
prev()
Rewinds the internal array pointer one place backward before returning the element
Arguments
No arguments.
Return value
next()
Advances the internal pointer one place forward before returning the element
Arguments
No arguments.
Return value
all()
Return all the collection elements.
Arguments
No arguments.
Return value
has()
Check for existence of the given key in the collection.
Arguments
Return value
get()
Get an element of the collection by its key.
Arguments
Return value
add()
Add an element at the given key in the collection.
Arguments
Return value
No return value.
set()
Set the whole elements of the collection.
Arguments
Return value
No return value.
clear()
Remove all elements from the collection.
Arguments
No arguments.
Return value
No return value.
offsetExists()
Check wether the given offset exists in the collection
Arguments
Return value
offsetGet()
Get the element at the given offset
Arguments
Return value
offsetSet()
Set the element at the given offset
Arguments
Return value
No return value.
offsetUnset()
Unset the element at the given offset
Arguments
Return value
No return value.
count()
Return elements count in the collection
Arguments
No arguments.
Return value
key()
Return the key of the current element
Arguments
No arguments.
Return value
valid()
Checks if current position is valid
Arguments
No arguments.
Return value
rewind()
Rewind the Iterator to the first element
Arguments
No arguments.
Return value
No return value.
ksort()
Sort elements by their key
Arguments
No arguments.
Return value
sort()
Sort elements by their value
Arguments
No arguments.
Return value
asort()
Sort elements by their value, but maintain index association
Arguments
No arguments.
Return value
License
Released under the MIT License