LFT
  • Namespace
  • Class
  • Tree

Namespaces

  • Docoflow
    • Contracts
    • Entity
    • Facades
    • Models
    • Traits
  • Docolight
    • Agno
      • Traits
    • Container
    • Http
      • Contracts
    • Rest
      • Handler
      • Http
    • Support
      • Debug
      • Traits
  • Docotory
    • Traits
  • None

Classes

  • ActiveRecordWrapper
  • Arr
  • Carbonate
  • ClassLoader
  • Collection
  • CollectionDataProvider
  • Facade
  • Factory
  • Fluent
  • Html
  • IterablePager
  • Repository
  • Set
  • Str

Class Collection

Collection contains a lot of handy methods that will make your work so much easier.

$results = json_decode($github->request('users/krisanalfa/repos'));

// Wrap them in a collection.
$collection = new \Docolight\Support\Collection($results);

// Sort descending by stars.
$collection->sortByDesc('stargazers_count');

// Get top 5 repositories.
$topFiveRepo = $leaderboard->take(5);

// This method will return every value of a given key. The following example returns every user's email address, indexed by their user id.
$collection->lists('email', 'id');

// This will run a filter function over each of the items. If the callback returns true, it will be present in the resulting collection.
$collection->filter(function($user) { if ($user->isNearby($me)) return true; });

// Another great thing about collections is that they can easily be converted to json.
echo $collection->toJson();

// When you cast a collection to a string, it will actually call the toJson method
echo $collection;

// Something that's quite obvious is the count method, which just returns you how many items there are in the collection.
$collection->count();

// Works just like the query, takes the first or last number of items.
$fiveFirst = $collection->take(5);
$fiveLast = $collection->take(-5);

// The sum method will return the sum based on the key or a callback function:
$collection->sum('points');

// You can use sortBy or sortByDesc to sort the collection based on a key or a callback function:
$collection->sortBy('name');

// Sort descending by rating.
$collection->sortByDesc(function($item) { return $item->rating; });

// You can also filter your items
$collection->filter(function ($item) { $item->foo === true; }); // will return the collection with foo === true

// You can paginate the collection too!
$collection->forPage(1, 20); // For page 1, each page has 20 items in it
Docolight\Support\Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Docolight\Http\Contracts\Arrayable

Direct known subclasses

Docoflow\Entity\Group, Docoflow\Entity\Step, Docoflow\Entity\Verificator

Namespace: Docolight\Support
Author: Krisan Alfa Timur krisanalfa@docotel.co.id
Located at Docolight/Support/Collection.php

Methods summary

public
# __construct( mixed $items = [] )

Create a new collection.

Create a new collection.

Parameters

$items
public static static
# make( mixed $items = null )

Create a new collection instance if the value isn't one already.

Create a new collection instance if the value isn't one already.

Parameters

$items

Returns

static
public
# fill( mixed $attributes )

Bulk insert attributes.

Bulk insert attributes.

Parameters

$attributes

Implementation of

Docolight\Http\Contracts\Arrayable::fill()
public array
# all( )

Get all of the items in the collection.

Get all of the items in the collection.

Returns

array
public static
# collapse( )

Collapse the collection of items into a single array.

Collapse the collection of items into a single array.

Returns

static
public boolean
# contains( mixed $key, mixed $value = null )

Determine if an item exists in the collection.

Determine if an item exists in the collection.

Parameters

$key
$value

Returns

boolean
public static
# diff( mixed $items )

Diff the collection with the given items.

Diff the collection with the given items.

Parameters

$items

Returns

static
public
# each( callable $callback )

Execute a callback over each item.

Execute a callback over each item.

Parameters

$callback

Returns


$this
public static
# fetch( string $key )

Fetch a nested element of the collection.

Fetch a nested element of the collection.

Deprecated

since version 5.1. Use pluck instead.

Parameters

$key

Returns

static
public static
# filter( callable $callback = null )

Run a filter over each of the items.

Run a filter over each of the items.

Parameters

$callback

Returns

static
public static
# where( string $key, mixed $value, boolean $strict = true )

Filter items by the given key value pair.

Filter items by the given key value pair.

Parameters

$key
$value
$strict

Returns

static
public static
# whereLoose( string $key, mixed $value )

Filter items by the given key value pair using loose comparison.

Filter items by the given key value pair using loose comparison.

Parameters

$key
$value

Returns

static
public mixed
# first( callable $callback = null, mixed $default = null )

Get the first item from the collection.

Get the first item from the collection.

Parameters

$callback
$default

Returns

mixed
public static
# flatten( )

Get a flattened array of the items in the collection.

Get a flattened array of the items in the collection.

Returns

static
public static
# flip( )

Flip the items in the collection.

Flip the items in the collection.

Returns

static
public
# forget( mixed $key )

Remove an item from the collection by key.

Remove an item from the collection by key.

Parameters

$key

Returns


$this
public mixed
# get( mixed $key, mixed $default = null )

Get an item from the collection by key.

Get an item from the collection by key.

Parameters

$key
$default

Returns

mixed
public static
# groupBy( callable|string $groupBy, boolean $preserveKeys = false )

Group an associative array by a field or using a callback.

Group an associative array by a field or using a callback.

Parameters

$groupBy
$preserveKeys

Returns

static
public static
# keyBy( callable|string $keyBy )

Key an associative array by a field or using a callback.

Key an associative array by a field or using a callback.

Parameters

$keyBy

Returns

static
public boolean
# has( mixed $key )

Determine if an item exists in the collection by key.

Determine if an item exists in the collection by key.

Parameters

$key

Returns

boolean
public string
# implode( string $value, string $glue = null )

Concatenate values of a given key as a string.

Concatenate values of a given key as a string.

Parameters

$value
$glue

Returns

string
public static
# intersect( mixed $items )

Intersect the collection with the given items.

Intersect the collection with the given items.

Parameters

$items

Returns

static
public boolean
# isEmpty( )

Determine if the collection is empty or not.

Determine if the collection is empty or not.

Returns

boolean
protected boolean
# useAsCallable( mixed $value )

Determine if the given value is callable, but not a string.

Determine if the given value is callable, but not a string.

Parameters

$value

Returns

boolean
public static
# keys( )

Get the keys of the collection items.

Get the keys of the collection items.

Returns

static
public mixed
# last( callable $callback = null, mixed $default = null )

Get the last item from the collection.

Get the last item from the collection.

Parameters

$callback
$default

Returns

mixed
public static
# pluck( string $value, string $key = null )

Get an array with the values of a given key.

Get an array with the values of a given key.

Parameters

$value
$key

Returns

static
public static
# lists( string $value, string $key = null )

Alias for the "pluck" method.

Alias for the "pluck" method.

Parameters

$value
$key

Returns

static
public static
# map( callable $callback )

Run a map over each of the items.

Run a map over each of the items.

Parameters

$callback

Returns

static
public static
# merge( mixed $items )

Merge the collection with the given items.

Merge the collection with the given items.

Parameters

$items

Returns

static
public static
# forPage( integer $page, integer $perPage )

"Paginate" the collection by slicing it into a smaller collection.

"Paginate" the collection by slicing it into a smaller collection.

Parameters

$page
$perPage

Returns

static
public mixed
# pop( )

Get and remove the last item from the collection.

Get and remove the last item from the collection.

Returns

mixed
public
# prepend( mixed $value )

Push an item onto the beginning of the collection.

Push an item onto the beginning of the collection.

Parameters

$value

Returns


$this
public
# push( mixed $value )

Push an item onto the end of the collection.

Push an item onto the end of the collection.

Parameters

$value

Returns


$this
public mixed
# pull( mixed $key, mixed $default = null )

Pulls an item from the collection.

Pulls an item from the collection.

Parameters

$key
$default

Returns

mixed
public
# put( mixed $key, mixed $value )

Put an item in the collection by key.

Put an item in the collection by key.

Parameters

$key
$value

Returns


$this
public mixed
# random( integer $amount = 1 )

Get one or more items randomly from the collection.

Get one or more items randomly from the collection.

Parameters

$amount

Returns

mixed

Throws

InvalidArgumentException
public mixed
# reduce( callable $callback, mixed $initial = null )

Reduce the collection to a single value.

Reduce the collection to a single value.

Parameters

$callback
$initial

Returns

mixed
public static
# reject( callable|mixed $callback )

Create a collection of all elements that do not pass a given truth test.

Create a collection of all elements that do not pass a given truth test.

Parameters

$callback

Returns

static
public static
# reverse( )

Reverse items order.

Reverse items order.

Returns

static
public mixed
# search( mixed $value, boolean $strict = false )

Search the collection for a given value and return the corresponding key if successful.

Search the collection for a given value and return the corresponding key if successful.

Parameters

$value
$strict

Returns

mixed
public mixed
# shift( )

Get and remove the first item from the collection.

Get and remove the first item from the collection.

Returns

mixed
public static
# shuffle( )

Shuffle the items in the collection.

Shuffle the items in the collection.

Returns

static
public static
# slice( integer $offset, integer $length = null, boolean $preserveKeys = false )

Slice the underlying collection array.

Slice the underlying collection array.

Parameters

$offset
$length
$preserveKeys

Returns

static
public static
# chunk( integer $size, boolean $preserveKeys = false )

Chunk the underlying collection array.

Chunk the underlying collection array.

Parameters

$size
$preserveKeys

Returns

static
public static
# sort( callable $callback = null )

Sort through each item with a callback.

Sort through each item with a callback.

Parameters

$callback

Returns

static
public static
# sortBy( callable|string $callback, integer $options = SORT_REGULAR, boolean $descending = false )

Sort the collection using the given callback.

Sort the collection using the given callback.

Parameters

$callback
$options
$descending

Returns

static
public static
# sortByDesc( callable|string $callback, integer $options = SORT_REGULAR )

Sort the collection in descending order using the given callback.

Sort the collection in descending order using the given callback.

Parameters

$callback
$options

Returns

static
public static
# splice( integer $offset, integer|null $length = null, mixed $replacement = [] )

Splice a portion of the underlying collection array.

Splice a portion of the underlying collection array.

Parameters

$offset
$length
$replacement

Returns

static
public mixed
# sum( callable|string|null $callback = null )

Get the sum of the given values.

Get the sum of the given values.

Parameters

$callback

Returns

mixed
public static
# take( integer $limit )

Take the first or last {$limit} items.

Take the first or last {$limit} items.

Parameters

$limit

Returns

static
public
# transform( callable $callback )

Transform each item in the collection using a callback.

Transform each item in the collection using a callback.

Parameters

$callback

Returns


$this
public static
# unique( string|callable|null $key = null )

Return only unique items from the collection array.

Return only unique items from the collection array.

Parameters

$key

Returns

static
public static
# values( )

Reset the keys on the underlying array.

Reset the keys on the underlying array.

Returns

static
protected callable
# valueRetriever( string $value )

Get a value retrieving callback.

Get a value retrieving callback.

Parameters

$value

Returns

callable
public static
# zip( mixed $items )

Zip the collection together with one or more arrays.

Zip the collection together with one or more arrays.

e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); => [[1, 4], [2, 5], [3, 6]]

Parameters

$items
$items

Returns

static
public array
# toArray( )

Get the collection of items as a plain array.

Get the collection of items as a plain array.

Returns

array
public array
# castToArray( )

Convert implementation to an array.

Convert implementation to an array.

Returns

array

Implementation of

Docolight\Http\Contracts\Arrayable::castToArray()
public array
# jsonSerialize( )

Convert the object into something JSON serializable.

Convert the object into something JSON serializable.

Returns

array

Implementation of

JsonSerializable::jsonSerialize()
public string
# toJson( integer $options = 0 )

Get the collection of items as JSON.

Get the collection of items as JSON.

Parameters

$options

Returns

string
public ArrayIterator
# getIterator( )

Get an iterator for the items.

Get an iterator for the items.

Returns

ArrayIterator

Implementation of

IteratorAggregate::getIterator()
public CachingIterator
# getCachingIterator( integer $flags = Docolight\Support\CachingIterator::CALL_TOSTRING )

Get a CachingIterator instance.

Get a CachingIterator instance.

Parameters

$flags

Returns

CachingIterator
public integer
# count( )

Count the number of items in the collection.

Count the number of items in the collection.

Returns

integer

Implementation of

Countable::count()
public boolean
# offsetExists( mixed $key )

Determine if an item exists at an offset.

Determine if an item exists at an offset.

Parameters

$key

Returns

boolean

Implementation of

ArrayAccess::offsetExists()
public mixed
# offsetGet( mixed $key )

Get an item at a given offset.

Get an item at a given offset.

Parameters

$key

Returns

mixed

Implementation of

ArrayAccess::offsetGet()
public
# offsetSet( mixed $key, mixed $value )

Set the item at a given offset.

Set the item at a given offset.

Parameters

$key
$value

Implementation of

ArrayAccess::offsetSet()
public
# offsetUnset( string $key )

Unset the item at a given offset.

Unset the item at a given offset.

Parameters

$key

Implementation of

ArrayAccess::offsetUnset()
public string
# __toString( )

Convert the collection to its string representation.

Convert the collection to its string representation.

Returns

string
public boolean
# hasErrors( )

Hack method to render CGridView.

Hack method to render CGridView.

Returns

boolean
public array
# getValidators( string $attribute )

Hack method to render CGridView.

Hack method to render CGridView.

Parameters

$attribute

Returns

array
public
# setFilters( array $filters )

Set current filter to this collection.

Set current filter to this collection.

Parameters

$filters
protected array
# getArrayableItems( mixed $items )

Results array of items from Collection or Arrayable.

Results array of items from Collection or Arrayable.

Parameters

$items

Returns

array
public mixed
# __get( string $name )

Method overloading to get current filter attribute. Useful if we want to render CGridView.

Method overloading to get current filter attribute. Useful if we want to render CGridView.

Parameters

$name
Filter field name

Returns

mixed

Properties summary

protected array $items

The items contained in the collection.

The items contained in the collection.

# []
protected array $filters

The items contained in the collection.

The items contained in the collection.

# []
LFT API documentation generated by ApiGen