Documentation

FlexIndexInterface extends FlexCollectionInterface

Defines Indexes for Flex Objects.

Flex indexes are similar to database indexes, they contain indexed fields which can be used to quickly look up or find the objects without loading them.

Tags
used-by

\Grav\Framework\Flex\FlexIndex

since
1.6
template
extends

Table of Contents

Methods

__construct()  : mixed
Creates a new Flex Collection.
call()  : array<string|int, mixed>
chunk()  : array<string|int, mixed>
Split collection into chunks.
collectionGroup()  : array<string|int, static>
Group items in the collection by a field and return them as associated array of collections.
copy()  : static
Create a copy from this collection by cloning all objects in the collection.
createFromArray()  : static
Creates a Flex Collection from an array.
createFromStorage()  : static
Helper method to create Flex Index.
defNestedProperty()  : $this
defProperty()  : $this
filterBy()  : FlexCollectionInterface
Filter collection by filter array with keys and values.
getCacheChecksum()  : string
Get cache checksum for the object / collection.
getCacheKey()  : string
Get a cache key which is used for caching the object / collection.
getCollection()  : FlexCollectionInterface
Load all the objects into memory,
getFlexDirectory()  : FlexDirectory
Get Flex Directory for the object / collection.
getFlexFeatures()  : array<string|int, mixed>
Get full list of features the object / collection implements.
getFlexKeys()  : array<string|int, string>
Get Flex keys from all the objects in the collection.
getFlexType()  : string
Get Flex Type of the object / collection.
getIndex()  : FlexIndexInterface
Get Flex Index from the Flex Collection.
getIndexMap()  : array<string|int, mixed>
getKey()  : string
getMetaData()  : array<string|int, mixed>
Get metadata associated to the object
getNestedProperty()  : mixed|array<string|int, mixed>
getObjectKeys()  : array<string|int, mixed>
getProperty()  : array<string|int, mixed>
getStorageKeys()  : array<string|int, string>
Get storage keys from all the objects in the collection.
getTimestamp()  : int
Get last updated timestamp for the object / collection.
getTimestamps()  : array<string|int, int>
Get timestamps from all the objects in the collection.
getType()  : string
group()  : array<string|int, mixed>
Group items in the collection by a field and return them as associated array.
hasFlexFeature()  : bool
Test whether the feature is implemented in the object / collection.
hasNestedProperty()  : bool|array<string|int, bool>
hasProperty()  : array<string|int, bool>
limit()  : ObjectCollectionInterface
loadEntriesFromStorage()  : array<string|int, mixed>
Method to load index from the object storage, usually filesystem.
orderBy()  : ObjectCollectionInterface
render()  : ContentBlockInterface|HtmlBlock
Renders the object.
reverse()  : CollectionInterface
Reverse the order of the items.
search()  : FlexCollectionInterface
Search a string from the collection.
select()  : CollectionInterface
Select items from collection.
setKey()  : $this
setNestedProperty()  : $this
setProperty()  : $this
shuffle()  : CollectionInterface
Shuffle items.
sort()  : FlexCollectionInterface
Sort the collection.
unselect()  : CollectionInterface
Un-select items from collection.
unsetNestedProperty()  : $this
unsetProperty()  : $this
withKeyField()  : static
Return new collection with a different key.

Methods

__construct()

Creates a new Flex Collection.

public __construct([array<string|int, FlexObjectInterface$entries = [] ][, FlexDirectory|null $directory = null ]) : mixed
Parameters
$entries : array<string|int, FlexObjectInterface> = []

Associated array of Flex Objects to be included in the collection.

$directory : FlexDirectory|null = null

Flex Directory where all the objects belong into.

Tags
used-by

FlexDirectory::createCollection() Official method to create Flex Collection.

throws
InvalidArgumentException

call()

public call(string $name[, array<string|int, mixed> $arguments = [] ]) : array<string|int, mixed>
Parameters
$name : string

Method name.

$arguments : array<string|int, mixed> = []

List of arguments passed to the function.

Return values
array<string|int, mixed>

Return values.

chunk()

Split collection into chunks.

public chunk(int $size) : array<string|int, mixed>
Parameters
$size : int

Size of each chunk.

Tags
phpstan-return

array<array<TKey,T>>

Return values
array<string|int, mixed>

collectionGroup()

Group items in the collection by a field and return them as associated array of collections.

public collectionGroup(string $property) : array<string|int, static>
Parameters
$property : string
Tags
phpstan-return

array<static<TKey,T>>

Return values
array<string|int, static>

copy()

Create a copy from this collection by cloning all objects in the collection.

public copy() : static
Tags
phpstan-return

static<TKey,T>

Return values
static

createFromArray()

Creates a Flex Collection from an array.

public static createFromArray(array<string|int, FlexObjectInterface$entries, FlexDirectory $directory[, string|null $keyField = null ]) : static
Parameters
$entries : array<string|int, FlexObjectInterface>

Associated array of Flex Objects to be included in the collection.

$directory : FlexDirectory

Flex Directory where all the objects belong into.

$keyField : string|null = null

Key field used to index the collection.

Tags
used-by

FlexDirectory::createCollection() Official method to create a Flex Collection.

Return values
static

Returns a new Flex Collection.

createFromStorage()

Helper method to create Flex Index.

public static createFromStorage(FlexDirectory $directory) : static
Parameters
$directory : FlexDirectory

Flex directory.

Tags
used-by

FlexDirectory::getIndex() Official method to get Index from a Flex Directory.

Return values
static

Returns a new Flex Index.

defNestedProperty()

public defNestedProperty(string $property, mixed $default[, string|null $separator = null ]) : $this
Parameters
$property : string

Object property to be defined.

$default : mixed

Default value.

$separator : string|null = null

Separator, defaults to '.'

Tags
throws
RuntimeException
Return values
$this

defProperty()

public defProperty(string $property, mixed $default) : $this
Parameters
$property : string

Object property to be defined.

$default : mixed

Default value.

Return values
$this

getCacheChecksum()

Get cache checksum for the object / collection.

public getCacheChecksum() : string

If checksum changes, cache gets invalided.

Return values
string

Returns cache checksum.

getCacheKey()

Get a cache key which is used for caching the object / collection.

public getCacheKey() : string
Return values
string

Returns cache key.

getFlexFeatures()

Get full list of features the object / collection implements.

public getFlexFeatures() : array<string|int, mixed>
Return values
array<string|int, mixed>

getFlexKeys()

Get Flex keys from all the objects in the collection.

public getFlexKeys() : array<string|int, string>
Tags
see
Flex::getObjects()

If you want to get list of Flex Objects from any Flex Directory.

Return values
array<string|int, string>

Returns[key => flex_key, ...] pairs.

getFlexType()

Get Flex Type of the object / collection.

public getFlexType() : string
APIYes
Return values
string

Returns Flex Type of the collection.

getIndexMap()

public getIndexMap([string|null $indexKey = null ]) : array<string|int, mixed>
Parameters
$indexKey : string|null = null
Return values
array<string|int, mixed>

getMetaData()

Get metadata associated to the object

public getMetaData(string $key) : array<string|int, mixed>
Parameters
$key : string

Key.

Return values
array<string|int, mixed>

getNestedProperty()

public getNestedProperty(string $property[, mixed|null $default = null ][, string|null $separator = null ]) : mixed|array<string|int, mixed>
Parameters
$property : string

Object property to be fetched.

$default : mixed|null = null

Default value if property has not been set.

$separator : string|null = null

Separator, defaults to '.'

Return values
mixed|array<string|int, mixed>

Property value.

getProperty()

public getProperty(string $property[, mixed|null $default = null ]) : array<string|int, mixed>
Parameters
$property : string

Object property to be fetched.

$default : mixed|null = null

Default value if property has not been set.

Return values
array<string|int, mixed>

List of [key => value] pairs.

getStorageKeys()

Get storage keys from all the objects in the collection.

public getStorageKeys() : array<string|int, string>
Tags
see
FlexDirectory::getObject()

If you want to get Flex Object from the Flex Directory.

Return values
array<string|int, string>

Returns [key => storage_key, ...] pairs.

getTimestamp()

Get last updated timestamp for the object / collection.

public getTimestamp() : int
APIYes
Return values
int

Returns Unix timestamp.

getTimestamps()

Get timestamps from all the objects in the collection.

public getTimestamps() : array<string|int, int>

This method can be used for example in caching.

Return values
array<string|int, int>

Returns [key => timestamp, ...] pairs.

group()

Group items in the collection by a field and return them as associated array.

public group(string $property) : array<string|int, mixed>
Parameters
$property : string
Return values
array<string|int, mixed>

hasFlexFeature()

Test whether the feature is implemented in the object / collection.

public hasFlexFeature(string $name) : bool
Parameters
$name : string
Return values
bool

hasNestedProperty()

public hasNestedProperty(string $property[, string|null $separator = null ]) : bool|array<string|int, bool>
Parameters
$property : string

Object property name.

$separator : string|null = null

Separator, defaults to '.'

Return values
bool|array<string|int, bool>

True if property has been defined (can be null).

hasProperty()

public hasProperty(string $property) : array<string|int, bool>
Parameters
$property : string

Object property name.

Return values
array<string|int, bool>

List of [key => bool] pairs.

loadEntriesFromStorage()

Method to load index from the object storage, usually filesystem.

public static loadEntriesFromStorage(FlexStorageInterface $storage) : array<string|int, mixed>
Parameters
$storage : FlexStorageInterface

Flex Storage associated to the directory.

Tags
used-by

FlexDirectory::getIndex() Official method to get Index from a Flex Directory.

Return values
array<string|int, mixed>

Returns a list of existing objects [storage_key => [storage_key => xxx, storage_timestamp => 123456, ...]]

render()

Renders the object.

public render([string|null $layout = null ][, array<string|int, mixed> $context = [] ]) : ContentBlockInterface|HtmlBlock
APIYes
Parameters
$layout : string|null = null

Layout to be used.

$context : array<string|int, mixed> = []

Extra context given to the renderer.

Tags
example

$block = $object->render('custom', ['variable' => 'value']);

example

{% render object layout 'custom' with { variable: 'value' } %}

Return values
ContentBlockInterface|HtmlBlock

Returns HtmlBlock containing the rendered output.

Search a string from the collection.

public search(string $search[, string|array<string|int, string>|null $properties = null ][, array<string|int, mixed>|null $options = null ]) : FlexCollectionInterface
APIYes
Parameters
$search : string

Search string.

$properties : string|array<string|int, string>|null = null

Properties to search for, defaults to configured properties.

$options : array<string|int, mixed>|null = null

Search options, defaults to configured options.

Tags
phpstan-return

static<T>

Return values
FlexCollectionInterface

Returns a Flex Collection with only matching objects.

select()

Select items from collection.

public select(array<string|int, int|string> $keys) : CollectionInterface

Collection is returned in the order of $keys given to the function.

Parameters
$keys : array<string|int, int|string>
Tags
phpstan-return

static<TKey,T>

Return values
CollectionInterface

setNestedProperty()

public setNestedProperty(string $property, mixed $value[, string|null $separator = null ]) : $this
Parameters
$property : string

Object property to be updated.

$value : mixed

New value.

$separator : string|null = null

Separator, defaults to '.'

Tags
throws
RuntimeException
Return values
$this

setProperty()

public setProperty(string $property, mixed $value) : $this
Parameters
$property : string

Object property to be updated.

$value : mixed

New value.

Return values
$this

unsetNestedProperty()

public unsetNestedProperty(string $property[, string|null $separator = null ]) : $this
Parameters
$property : string

Object property to be unset.

$separator : string|null = null

Separator, defaults to '.'

Tags
throws
RuntimeException
Return values
$this

unsetProperty()

public unsetProperty(string $property) : $this
Parameters
$property : string

Object property to be unset.

Return values
$this

withKeyField()

Return new collection with a different key.

public withKeyField([string|null $keyField = null ]) : static
APIYes
Parameters
$keyField : string|null = null

Switch key field of the collection.

Tags
phpstan-return

static<T>

Return values
static

Returns a new Flex Collection with new key field.


        
On this page

Search results