PHP Class Collection

phynx is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. phynx is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . 2007 - 2012, Rainer Furtmeier - [email protected]
Show file Open project: getkirby/toolkit Class Usage Examples

Public Properties

Property Type Description
$filters

Protected Properties

Property Type Description
$pagination

Public Methods

Method Description
__call ( $key, $arguments )
__debuginfo ( ) : array Improved var_dump() output
__get ( $key )
__set ( $key, $value )
__toString ( ) : string Makes it possible to echo the entire object
append ( string $key, mixed $object ) : Collection Appends an element to the data array
chunk ( integer $size ) : object Creates chunks of the same size The last chunk may be smaller
count ( ) : integer Counts all elements in the array
extractValue ( mixed $item, string $field ) : mixed Makes sure to provide a valid value for each filter method no matter if an object or an array is given
filter ( func $callback ) : Collection Filter the elements in the array by a callback function
filterBy ( ) : Collection Filters the current collection by a field, operator and search value
findBy ( string $key, mixed $value ) : mixed Find a single item by a key and value pair
first ( ) : mixed Returns the first element from the array
flip ( ) : Collection Returns the array in reverse order
get ( $key, $default = null )
group ( callable $callback ) : object Groups the collection by a given callback
groupBy ( string $field, $i = true ) : object Groups the collection by a given field
has ( string $key ) : boolean Checks if an element is in the collection by key.
indexOf ( mixed $needle ) : mixed Tries to find the index number for the given element
keyOf ( mixed $needle ) : mixed Tries to find the key for the given element
keys ( ) : array Returns an array of all keys in the collection
last ( ) : mixed Returns the last element from the array
limit ( integer $limit ) : Collection Returns a new collection with a limited number of elements
map ( function $callback ) : Collection Map a function to each item in the collection
merge ( $collection2 ) : Collection Returns a new combined collection
not ( ) : Collection Returns a new collection without the given element(s)
nth ( $n ) : mixed Returns the nth element from the array
offset ( integer $offset ) : Collection Returns a new collection starting from the given offset
paginate ( integer $limit, array $options = [] ) : object Add pagination
pagination ( ) : object Get the previously added pagination object
pluck ( string $field, $split = null, $unique = false ) : array Extracts all values for a single field into a new array
prepend ( string $key, mixed $object ) : Collection Prepends an element to the data array
set ( $key, $value )
shuffle ( ) : object Shuffle all elements in the array
slice ( integer $offset = null, integer $limit = null ) : Collection Returns a slice of the collection
sortBy ( ) : Collection Sorts the collection by any number of fields
toArray ( $callback = null ) : array Converts the current object into an array
toJson ( ) : string Converts the current object into a json string
without ( ) : Collection Returns a new collection without the given element(s)

Method Details

__call() public method

public __call ( $key, $arguments )

__debuginfo() public method

Improved var_dump() output
public __debuginfo ( ) : array
return array

__get() public method

public __get ( $key )

__set() public method

public __set ( $key, $value )

__toString() public method

Makes it possible to echo the entire object
public __toString ( ) : string
return string

append() public method

Appends an element to the data array
public append ( string $key, mixed $object ) : Collection
$key string
$object mixed
return Collection

chunk() public method

Creates chunks of the same size The last chunk may be smaller
public chunk ( integer $size ) : object
$size integer Number of items per chunk
return object A new collection with an item for each chunk and a subcollection in each chunk

count() public method

Counts all elements in the array
public count ( ) : integer
return integer

extractValue() public static method

Makes sure to provide a valid value for each filter method no matter if an object or an array is given
public static extractValue ( mixed $item, string $field ) : mixed
$item mixed
$field string
return mixed

filter() public method

Filter the elements in the array by a callback function
public filter ( func $callback ) : Collection
$callback func the callback function
return Collection

filterBy() public method

Filters the current collection by a field, operator and search value
public filterBy ( ) : Collection
return Collection

findBy() public method

Find a single item by a key and value pair
public findBy ( string $key, mixed $value ) : mixed
$key string
$value mixed
return mixed

first() public method

Returns the first element from the array
public first ( ) : mixed
return mixed

flip() public method

Returns the array in reverse order
public flip ( ) : Collection
return Collection

get() public method

public get ( $key, $default = null )

group() public method

Groups the collection by a given callback
public group ( callable $callback ) : object
$callback callable
return object A new collection with an item for each group and a subcollection in each group

groupBy() public method

Groups the collection by a given field
public groupBy ( string $field, $i = true ) : object
$field string
return object A new collection with an item for each group and a subcollection in each group

has() public method

Checks if an element is in the collection by key.
public has ( string $key ) : boolean
$key string
return boolean

indexOf() public method

Tries to find the index number for the given element
public indexOf ( mixed $needle ) : mixed
$needle mixed the element to search for
return mixed the name of the key or false

keyOf() public method

Tries to find the key for the given element
public keyOf ( mixed $needle ) : mixed
$needle mixed the element to search for
return mixed the name of the key or false

keys() public method

Returns an array of all keys in the collection
public keys ( ) : array
return array

last() public method

Returns the last element from the array
public last ( ) : mixed
return mixed

limit() public method

Returns a new collection with a limited number of elements
public limit ( integer $limit ) : Collection
$limit integer The number of elements to return
return Collection

map() public method

Map a function to each item in the collection
public map ( function $callback ) : Collection
$callback function
return Collection

merge() public method

Returns a new combined collection
public merge ( $collection2 ) : Collection
return Collection

not() public method

Returns a new collection without the given element(s)
public not ( ) : Collection
return Collection

nth() public method

Returns the nth element from the array
public nth ( $n ) : mixed
return mixed

offset() public method

Returns a new collection starting from the given offset
public offset ( integer $offset ) : Collection
$offset integer The index to start from
return Collection

paginate() public method

Add pagination
public paginate ( integer $limit, array $options = [] ) : object
$limit integer the number of items per page
$options array and optional array with options for the pagination class
return object a sliced set of data

pagination() public method

Get the previously added pagination object
public pagination ( ) : object
return object

pluck() public method

Extracts all values for a single field into a new array
public pluck ( string $field, $split = null, $unique = false ) : array
$field string
return array

prepend() public method

Prepends an element to the data array
public prepend ( string $key, mixed $object ) : Collection
$key string
$object mixed
return Collection

set() public method

public set ( $key, $value )

shuffle() public method

Shuffle all elements in the array
public shuffle ( ) : object
return object a new shuffled collection

slice() public method

Returns a slice of the collection
public slice ( integer $offset = null, integer $limit = null ) : Collection
$offset integer The optional index to start the slice from
$limit integer The optional number of elements to return
return Collection

sortBy() public method

Sorts the collection by any number of fields
public sortBy ( ) : Collection
return Collection

toArray() public method

Converts the current object into an array
public toArray ( $callback = null ) : array
return array

toJson() public method

Converts the current object into a json string
public toJson ( ) : string
return string

without() public method

Returns a new collection without the given element(s)
public without ( ) : Collection
return Collection

Property Details

$filters public static property

public static $filters

$pagination protected property

protected $pagination