Collection

Implements \Iterator, \Countable, \ArrayAccess

A collection holds instances of models by converting them from raw data.

It has meta information about the total amount of items, pages and references to other pages. Further more it allows to iterate over all total items, by simply fetching the next page if the bounds of the current page have been reached.

If you want to fetch the next page manually, you can call Collection::fetchNextPage(). This will fetch the next page, if it is available and apply the content to this collection.

In order to fetch a specific page and apply it to the collection, you can fetch it with the rest client and then apply it via Collection::setData(). Whether you want to reset the current data or not can be controlled with the flag reset.

package

NextEvent\PHPSDK\Model

Methods

Creates a new collection.

__construct(string $modelClass, array $instanceArgs = null, array $data = null, \NextEvent\PHPSDK\Rest\Client $restClient = null) 

Arguments

$modelClass

string

The class of the models in this collection.

$instanceArgs

array

Additional instance arguments to pass to the model constructor.

$data

array

Optional response data to initialize the collection with.

$restClient

\NextEvent\PHPSDK\Rest\Client

Rest client for fetching next pages.

Returns the total amount of items in this collection.

count() : integer

Response

integer

Returns the value at current iterator position.

current() : mixed

Response

mixed

Fetches the next page, if it is set and updates the data.

fetchNextPage() : boolean
Throws
\NextEvent\PHPSDK\Exception\CollectionException

If no rest client has been set.

Response

boolean

Whether the next page has been fetched or not. false means, we have no next page to fetch.

Filters this collection with the given callback.

filter(callable $callback) : \NextEvent\PHPSDK\Model\Collection

Arguments

$callback

callable

Response

\NextEvent\PHPSDK\Model\Collection

A new collection instance with the filtered content.

Getter for the autofetch flag

getAutofetch() : boolean

Response

boolean

Returns the current page.

getCurrentPage() : string

Response

string

Returns the last page.

getLastPage() : string

Response

string

The model class of this collection.

getModelClass() : string

Response

string

Returns the next page.

getNextPage() : string

Response

string

The current page number.

getPage() : integer

Response

integer

The amount of pages in total.

getPages() : integer

Response

integer

The page size of this collection.

getPageSize() : integer

Response

integer

Returns the previous page.

getPreviousPage() : string

Response

string

Returns the current iterator position.

key() : integer

Response

integer

Applies a callback to the elements of the collection

map(callable $callback) : array

This works similar to PHP's array_map() function and allows to transform the collection with a given callback function.

Throws
\NextEvent\PHPSDK\Exception\InvalidArgumentException

Arguments

$callback

callable

Callback function to run for each element. Receives the current element as argument.

Response

array

containing all the elements of the collection1 after applying the callback function to each one

Increases the iterator position.

next() : void

Returns whether the given offset exists.

offsetExists(mixed $offset) : boolean

Arguments

$offset

mixed

The offset to check.

Response

boolean

Returns the value at the given offset.

offsetGet(mixed $offset) : mixed

Arguments

$offset

mixed

The offset to get the value at.

Response

mixed

Sets the given value at the given offset.

offsetSet(mixed $offset, mixed $value) : void

Only instances of the current model class can be set in this collection.

Throws
\NextEvent\PHPSDK\Exception\CollectionException

If the value is not an instance of the current model class.

Arguments

$offset

mixed

The offset to set the value at.

$value

mixed

The value to set.

Removes the value at the given $offset.

offsetUnset(mixed $offset) : void

Arguments

$offset

mixed

The offset at which to remove the value.

Resets the iterator position.

rewind() : void

Setter for the autofetch flag

setAutofetch(boolean $autoFetch) 

If enabled, iterating the collection will automatically fetch all pages and thus list all records that match the request/query.

Arguments

$autoFetch

boolean

Sets the data data by applying the given response data.

setData(array $data, boolean $reset = true) : void

This method assumes that the given data holds the structure of a HAL response.

Throws
\NextEvent\PHPSDK\Exception\CollectionException

Arguments

$data

array

$reset

boolean

Whether to reset the internal models array.

Set additional arguments which should be passed to the model constructor.

setInstanceArguments(array $arguments) : void

You can pass a function as a value, which has to return the value type expected by the constructor of the model. The raw model data will be passed to that function.

Arguments

$arguments

array

Returns whether the current iterator position has a valid value.

valid() : boolean

If the iterator position is at the end of the page, but not at the end of the whole collection, the next page will be fetched.

Response

boolean