class PrioritySorter (View source)

Sorts an associative array of items given a list of priorities, where priorities are the keys of the items in the order they are desired.

Allows user-defined variables, and a "rest" key to symbolise all remaining items. Example:

$myItems = [ 'product-one' => new Product(...), 'product-two' => new Product(...), 'product-three' => new Product(...), 'product-four' => new Product(...), ];

$priorities = [ '$featured', 'product-two', '...rest', ];

$sorter = new PrioritySorter($items, $priorities); $sorter->setVariable('$featured', 'product-three'); $sorter->getSortedList();

[ 'product-three' => [object] Product, 'product-two' => [object] Product, 'product-one' => [object] Product, 'product-four' => [object] Product ]

Traits

A class that can be instantiated or replaced via DI

Methods

static Injectable
create(array ...$args)

An implementation of the factory method, allows you to create an instance of a class

static Injectable
singleton(string $class = null)

Creates a class instance by the "singleton" design pattern.

__construct(array $items = [], array $priorities = [])

PrioritySorter constructor.

array
getSortedList()

Sorts the items and returns a new version of $this->items

$this
setPriorities(array $priorities)

Set the priorities for the items

$this
setItems(array $items)

Sets the list of all items

$this
setVariable(string $name, $value)

Add a variable for replacination, e.g. addVariable->('$project', 'myproject')

$this
setRestKey($key)

The key used for "all other items"

Details

static Injectable create(array ...$args)

An implementation of the factory method, allows you to create an instance of a class

This method will defer class substitution to the Injector API, which can be customised via the Config API to declare substitution classes.

This can be called in one of two ways - either calling via the class directly, or calling on Object and passing the class name as the first parameter. The following are equivalent: $list = DataList::create('SiteTree'); $list = SiteTree::get();

Parameters

array ...$args

Return Value

Injectable

static Injectable singleton(string $class = null)

Creates a class instance by the "singleton" design pattern.

It will always return the same instance for this class, which can be used for performance reasons and as a simple way to access instance methods which don't rely on instance data (e.g. the custom SilverStripe static handling).

Parameters

string $class

Optional classname to create, if the called class should not be used

Return Value

Injectable

The singleton instance

__construct(array $items = [], array $priorities = [])

PrioritySorter constructor.

Parameters

array $items
array $priorities

array getSortedList()

Sorts the items and returns a new version of $this->items

Return Value

array

$this setPriorities(array $priorities)

Set the priorities for the items

Parameters

array $priorities

An array of keys used in $this->items

Return Value

$this

$this setItems(array $items)

Sets the list of all items

Parameters

array $items

Return Value

$this

$this setVariable(string $name, $value)

Add a variable for replacination, e.g. addVariable->('$project', 'myproject')

Parameters

string $name
$value

Return Value

$this

$this setRestKey($key)

The key used for "all other items"

Parameters

$key

Return Value

$this