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

Properties

protected string $restKey

The key that is used to denote all remaining items that have not been specified in priorities

protected array $variables

A map of variables to their values

protected array $items

An associative array of items, whose keys can be used in the $priorities list

protected array $priorities

An indexed array of keys in the $items list, reflecting the desired sort

protected array $names

The keys of the $items array

Methods

public static 
create(mixed ...$args)

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

public static 
singleton(string $class = null)

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

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

PrioritySorter constructor.

public
array
getSortedList()

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

public
$this
setPriorities(array $priorities)

Set the priorities for the items

public
$this
setItems(array $items)

Sets the list of all items

public
$this
setVariable(string $name, $value)

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

public
$this
setRestKey($key)

The key used for "all other items"

protected
addVariables()

If variables are defined, interpolate their values

protected
includeRest(array $list)

If the "rest" key exists in the order array, replace it by the unspecified items

protected
mixed
resolveValue($name)

Ensure variables get converted to their values

Details

static Injectable create(mixed ...$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::class); $list = SiteTree::get();

Parameters

mixed ...$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

protected addVariables()

If variables are defined, interpolate their values

protected includeRest(array $list)

If the "rest" key exists in the order array, replace it by the unspecified items

Parameters

array $list

protected mixed resolveValue($name)

Ensure variables get converted to their values

Parameters

$name

Return Value

mixed