PrioritySorter
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
An implementation of the factory method, allows you to create an instance of a class
Creates a class instance by the "singleton" design pattern.
PrioritySorter constructor.
Sorts the items and returns a new version of $this->items
Set the priorities for the items
Sets the list of all items
Add a variable for replacination, e.g. addVariable->('$project', 'myproject')
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();
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).
__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"