Properties

$factory

$factory : \Charcoal\Factory\FactoryInterface

Store the factory instance for the current class.

Type

\Charcoal\Factory\FactoryInterface

$callback

$callback : callable|null

The callback routine applied to every object added to the collection.

Type

callable|null

$dynamicTypeField

$dynamicTypeField : string|null

The field which defines the data's model.

Type

string|null

$collectionClass

$collectionClass : string

The class name of the collection to use.

Must be a fully-qualified PHP namespace and an implementation of \ArrayAccess.

Type

string

Methods

__construct()

__construct(array  $data) 

Return a new CollectionLoader object.

Parameters

array $data

The loader's dependencies.

setFactory()

setFactory(\Charcoal\Factory\FactoryInterface  $factory) : \Charcoal\Loader\CollectionLoader

Set an object model factory.

Parameters

\Charcoal\Factory\FactoryInterface $factory

The model factory, to create objects.

Returns

\Charcoal\Loader\CollectionLoader

Chainable

setData()

setData(array  $data) : \Charcoal\Loader\CollectionLoader

Set the loader data.

Parameters

array $data

Data to assign to the loader.

Returns

\Charcoal\Loader\CollectionLoader

Chainable

source()

source() : mixed

Retrieve the source to load objects from.

Throws

\RuntimeException

If no source has been defined.

Returns

mixed

model()

model() : \Charcoal\Loader\Model

Retrieve the object model.

Throws

\RuntimeException

If no model has been defined.

Returns

\Charcoal\Loader\Model

hasModel()

hasModel() : boolean

Determine if the loader has an object model.

Returns

boolean

setModel()

setModel(string|\Charcoal\Model\ModelInterface  $model) : \Charcoal\Loader\CollectionLoader

Set the model to use for the loaded objects.

Parameters

string|\Charcoal\Model\ModelInterface $model

An object model.

Throws

\InvalidArgumentException

If the given argument is not a model.

Returns

\Charcoal\Loader\CollectionLoader

CHainable

setDynamicTypeField()

setDynamicTypeField(string  $field) : \Charcoal\Loader\CollectionLoader

Parameters

string $field

The field to use for dynamic object type.

Throws

\InvalidArgumentException

If the field is not a string.

Returns

\Charcoal\Loader\CollectionLoader

Chainable

properties()

properties() : array

Alias of {@see SourceInterface::properties()}

Returns

array

setProperties()

setProperties(array  $properties) : \Charcoal\Loader\CollectionLoader

Alias of {@see SourceInterface::setProperties()}

Parameters

array $properties

An array of property identifiers.

Returns

\Charcoal\Loader\CollectionLoader

Chainable

addProperty()

addProperty(string  $property) : \Charcoal\Loader\CollectionLoader

Alias of {@see SourceInterface::addProperty()}

Parameters

string $property

A property identifier.

Returns

\Charcoal\Loader\CollectionLoader

Chainable

setKeywords()

setKeywords(array  $keywords) : \Charcoal\Loader\CollectionLoader

Set "search" keywords to filter multiple properties.

Parameters

array $keywords

An array of keywords and properties.

Returns

\Charcoal\Loader\CollectionLoader

Chainable

addKeyword()

addKeyword(string  $keyword, array  $properties = null) : \Charcoal\Loader\CollectionLoader

Add a "search" keyword filter to multiple properties.

Parameters

string $keyword

A value to match among $properties.

array $properties

An array of property identifiers.

Returns

\Charcoal\Loader\CollectionLoader

Chainable

filters()

filters() : array

Alias of {@see SourceInterface::filters()}

Returns

array

setFilters()

setFilters(array  $filters) : \Charcoal\Model\Collection

Alias of {@see SourceInterface::setFilters()}

Parameters

array $filters

An array of filters.

Returns

\Charcoal\Model\Collection

Chainable

addFilter()

addFilter(string|array|\Charcoal\Loader\Filter  $param, mixed  $val = null, array  $options = null) : \Charcoal\Loader\CollectionLoader

Alias of {@see SourceInterface::addFilter()}

Parameters

string|array|\Charcoal\Loader\Filter $param

A property identifier, filter array, or Filter object.

mixed $val

Optional. The value to match. Only used if the first argument is a string.

array $options

Optional. Filter options. Only used if the first argument is a string.

Returns

\Charcoal\Loader\CollectionLoader

Chainable

orders()

orders() : array

Alias of {@see SourceInterface::orders()}

Returns

array

setOrders()

setOrders(array  $orders) : \Charcoal\Loader\CollectionLoader

Alias of {@see SourceInterface::setOrders()}

Parameters

array $orders

An array of orders.

Returns

\Charcoal\Loader\CollectionLoader

Chainable

addOrder()

addOrder(string|array|\Charcoal\Loader\Order  $param, string  $mode = 'asc', array  $orderOptions = null) : \Charcoal\Loader\CollectionLoader

Alias of {@see SourceInterface::addOrder()}

Parameters

string|array|\Charcoal\Loader\Order $param

A property identifier, order array, or Order object.

string $mode

Optional. Sort order. Only used if the first argument is a string.

array $orderOptions

Optional. Filter options. Only used if the first argument is a string.

Returns

\Charcoal\Loader\CollectionLoader

Chainable

pagination()

pagination() : \Charcoal\Loader\Pagination

Alias of {@see SourceInterface::pagination()}

Returns

\Charcoal\Loader\Pagination

setPagination()

setPagination(mixed  $param) : \Charcoal\Loader\CollectionLoader

Alias of {@see SourceInterface::setPagination()}

Parameters

mixed $param

An associative array of pagination settings.

Returns

\Charcoal\Loader\CollectionLoader

Chainable

page()

page() : integer

Alias of {@see PaginationInterface::page()}

Returns

integer

setPage()

setPage(integer  $page) : \Charcoal\Loader\CollectionLoader

Alias of {@see PaginationInterface::pagination()}

Parameters

integer $page

A page number.

Returns

\Charcoal\Loader\CollectionLoader

Chainable

numPerPage()

numPerPage() : integer

Alias of {@see PaginationInterface::numPerPage()}

Returns

integer

setNumPerPage()

setNumPerPage(integer  $num) : \Charcoal\Loader\CollectionLoader

Alias of {@see PaginationInterface::setNumPerPage()}

Parameters

integer $num

The number of items to display per page.

Returns

\Charcoal\Loader\CollectionLoader

Chainable

setCallback()

setCallback(callable  $callback) : \Charcoal\Loader\CollectionLoader

Set the callback routine applied to every object added to the collection.

Parameters

callable $callback

The callback routine.

Returns

\Charcoal\Loader\CollectionLoader

Chainable

callback()

callback() : callable|null

Retrieve the callback routine applied to every object added to the collection.

Returns

callable|null

load()

load(string|null  $ident = null, callable  $callback = null) : array|\ArrayAccess

Load a collection from source.

Parameters

string|null $ident

Optional. A pre-defined list to use from the model.

callable $callback

Optional. Apply a callback to every entity of the collection. Leave blank to use {@see CollectionLoader::callback()}.

Throws

\Charcoal\Loader\Exception

If the database connection fails.

Returns

array|\ArrayAccess

loadCount()

loadCount() : integer

Get the total number of items for this collection query.

Throws

\RuntimeException

If the database connection fails.

Returns

integer

loadFromQuery()

loadFromQuery(string|array  $query, callable  $callback = null) : array|\ArrayAccess

Load list from query.

Example — Binding values to $query

$this->loadFromQuery([
    'SELECT name, colour, calories FROM fruit WHERE calories < :calories AND colour = :colour',
    [
        'calories' => 150,
        'colour'   => 'red'
    ],
    [ 'calories' => PDO::PARAM_INT ]
]);

Parameters

string|array $query

The SQL query as a string or an array composed of the query, parameter binds, and types of parameter bindings.

callable $callback

Optional. Apply a callback to every entity of the collection. Leave blank to use {@see CollectionLoader::callback()}.

Throws

\RuntimeException

If the database connection fails.

\InvalidArgumentException

If the SQL string/set is invalid.

Returns

array|\ArrayAccess

createCollection()

createCollection() : array|\ArrayAccess

Create a collection class or array.

Throws

\RuntimeException

If the collection class is invalid.

Returns

array|\ArrayAccess

setCollectionClass()

setCollectionClass(string  $className) : \Charcoal\Loader\AbstractPropertyDisplay

Set the class name of the collection.

Parameters

string $className

The class name of the collection.

Throws

\InvalidArgumentException

If the class name is not a string.

Returns

\Charcoal\Loader\AbstractPropertyDisplay —

Chainable

collectionClass()

collectionClass() : string

Retrieve the class name of the collection.

Returns

string

factory()

factory() : \Charcoal\Factory\FactoryInterface

Retrieve the object model factory.

Throws

\RuntimeException

If the model factory was not previously set.

Returns

\Charcoal\Factory\FactoryInterface

processCollection()

processCollection(array|\Traversable  $results, callable  $callback = null) : array|\ArrayAccess

Process the collection of raw data.

Parameters

array|\Traversable $results

The raw result set.

callable $callback

Optional. Apply a callback to every entity of the collection. Leave blank to use {@see CollectionLoader::callback()}.

Throws

\InvalidArgumentException

If the SQL string/set is invalid.

Returns

array|\ArrayAccess

getter()

getter(string  $key) : string

Allow an object to define how the key getter are called.

Parameters

string $key

The key to get the getter from.

Returns

string —

The getter method name, for a given key.

setter()

setter(string  $key) : string

Allow an object to define how the key setter are called.

Parameters

string $key

The key to get the setter from.

Returns

string —

The setter method name, for a given key.

camelize()

camelize(string  $str) : string

Transform a snake_case string to camelCase.

Parameters

string $str

The snake_case string to camelize.

Returns

string —

The camelcase'd string.