Skip to content

Latest commit

 

History

History
898 lines (431 loc) · 12.7 KB

Collection.md

File metadata and controls

898 lines (431 loc) · 12.7 KB

Clean\Data\Collection

Implements:

Countable, Serializable, SeekableIterator, ArrayAccess, Traversable, Iterator

Extend:

ArrayIterator

Methods

Name Description
__call
__get Returns direct value from entity when collection has only one element
__isset Returns isset on entity property when collection has only one element
bindCollection Bind two collections
chunk Splits collection into chunks
clear Remove all entities form collection
distinctOn Eliminates entities that contains the same value in given property
extend Extend collection with entities from another one
filter Filter collection from entities not matching criteria given in callback
first Returns first entity from collection
getAllValuesForProperty Return values from all entities from given property
getBy Get collection of entities matching criteria given in callback
getKeys Returns collection keys
getNewCollection Returns new instance of collection of the same type
getNext Returns next entity from collection
getPrevious Returns previous entity from collection
getRandom returns random entity from collection
has Checks if collection has entity with field equals to given value
isEmpty Returns true if collection is empty
isNotEmpty Returns true if collection is not empty
last Returns last entity from collection
prepend Prepand entity to the begining of collection
reindex Renumber collection keys (from zero to n), keeping values in the same place
reverse Return an collection with elements in reverse order
search Search for an element with given property and value
shift Shift an entity off the begining of collection
slice Extract a slice of the collection
split Returns collection of collections created by spliting first Collection to a parts
toArray Tranform collection to array
walk Apply a user supplied function to every member of an Collection

Inherited methods

Name Description
__construct Construct an ArrayIterator
append Append an element
asort Sort array by values
count Count elements
current Return current array entry
getArrayCopy Get array copy
getFlags Get flags
key Return current array key
ksort Sort array by keys
natcasesort Sort an array naturally, case insensitive
natsort Sort an array naturally
next Move to next entry
offsetExists Check if offset exists
offsetGet Get value for an offset
offsetSet Set value for an offset
offsetUnset Unset value for an offset
rewind Rewind array back to the start
seek Seek to position
serialize Serialize
setFlags Set behaviour flags
uasort Sort with a user-defined comparison function and maintain index association
uksort Sort by keys using a user-defined comparison function
unserialize Unserialize
valid Check whether array contains more entries

Collection::__call

Description

 __call (void)

Parameters

This function has no parameters.

Return Values

void


Collection::__get

Description

public __get (string $name)

Returns direct value from entity when collection has only one element

Parameters

  • (string) $name : property name

Return Values

mixed

Throws Exceptions

\LogicException

when collection has more then one element


Collection::__isset

Description

public __isset (string $name)

Returns isset on entity property when collection has only one element

Parameters

  • (string) $name : property name

Return Values

mixed

Throws Exceptions

\LogicException

when collection has more then one element


Collection::bindCollection

Description

public bindCollection (\Collection $collection, array $compareKeys, string $propertyName)

Bind two collections

Parameters

  • (\Collection) $collection : collection
  • (array) $compareKeys : The name of the key to compare with from target Collection
  • (string) $propertyName : The nae of new property that will be created in source Collection

Return Values

static


Collection::chunk

Description

public chunk (int $size)

Splits collection into chunks

Parameters

  • (int) $size

Return Values

static


Collection::clear

Description

public clear (void)

Remove all entities form collection

Parameters

This function has no parameters.

Return Values

static


Collection::distinctOn

Description

public distinctOn (string $propertyName)

Eliminates entities that contains the same value in given property

Parameters

  • (string) $propertyName : Name of the property

Return Values

static


Collection::extend

Description

public extend (\Collection $data)

Extend collection with entities from another one

Parameters

  • (\Collection) $data

Return Values

static


Collection::filter

Description

public filter (\Closure $callback)

Filter collection from entities not matching criteria given in callback

Parameters

  • (\Closure) $callback : callback

Return Values

static


Collection::first

Description

public first (void)

Returns first entity from collection

Parameters

This function has no parameters.

Return Values

\Entity


Collection::getAllValuesForProperty

Description

public getAllValuesForProperty (string $name)

Return values from all entities from given property

Parameters

  • (string) $name : name

Return Values

array


Collection::getBy

Description

public getBy (\Closure $callback)

Get collection of entities matching criteria given in callback

Usage example:

$colleciton->getBy(function($entity) {
return $entity->name == 'John';
});

Parameters

  • (\Closure) $callback

Return Values

static


Collection::getKeys

Description

public getKeys (void)

Returns collection keys

Parameters

This function has no parameters.

Return Values

array


Collection::getNewCollection

Description

public getNewCollection (void)

Returns new instance of collection of the same type

Parameters

This function has no parameters.

Return Values

static


Collection::getNext

Description

public getNext (void)

Returns next entity from collection

Parameters

This function has no parameters.

Return Values

\Entity


Collection::getPrevious

Description

public getPrevious (void)

Returns previous entity from collection

Parameters

This function has no parameters.

Return Values

\Entity


Collection::getRandom

Description

public getRandom (void)

returns random entity from collection

Parameters

This function has no parameters.

Return Values

\Entity


Collection::has

Description

public has (string $field, mixed $value)

Checks if collection has entity with field equals to given value

Parameters

  • (string) $field : field
  • (mixed) $value : value

Return Values

bool


Collection::isEmpty

Description

public isEmpty (void)

Returns true if collection is empty

Parameters

This function has no parameters.

Return Values

bool


Collection::isNotEmpty

Description

public isNotEmpty (void)

Returns true if collection is not empty

Parameters

This function has no parameters.

Return Values

bool


Collection::last

Description

public last (void)

Returns last entity from collection

Parameters

This function has no parameters.

Return Values

\Entity


Collection::prepend

Description

public prepend (\Entity $entity)

Prepand entity to the begining of collection

Parameters

  • (\Entity) $entity

Return Values

static


Collection::reindex

Description

public reindex (void)

Renumber collection keys (from zero to n), keeping values in the same place

Parameters

This function has no parameters.

Return Values

static


Collection::reverse

Description

public reverse (void)

Return an collection with elements in reverse order

Parameters

This function has no parameters.

Return Values

static


Collection::search

Description

public search (string $field, mixed $value, bool $strict)

Search for an element with given property and value

Parameters

  • (string) $field : name of property
  • (mixed) $value : value to compare
  • (bool) $strict : compare value and type of property

Return Values

int|string|bool


Collection::shift

Description

public shift (void)

Shift an entity off the begining of collection

Parameters

This function has no parameters.

Return Values

\Entity


Collection::slice

Description

public slice (int $offset, int|null $length)

Extract a slice of the collection

Parameters

  • (int) $offset
  • (int|null) $length

Return Values

static


Collection::split

Description

public split (int $parts)

Returns collection of collections created by spliting first Collection to a parts

Example:

When collection has 10 element and we would like to split to 3 separate collections:

$splitted = $collection->split(3);
$splitted->count(); // = 3
$splitted[0]->count(); // = 4
$splitted[1]->count(); // = 4
$splitted[3]->count(); // = 2

Parameters

  • (int) $parts

Return Values

static


Collection::toArray

Description

public toArray (void)

Tranform collection to array

Parameters

This function has no parameters.

Return Values

array


Collection::walk

Description

public walk (\Closure $callback)

Apply a user supplied function to every member of an Collection

Parameters

  • (\Closure) $callback : user supplied function

Return Values

static