PrioritySorter
class PrioritySorter
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
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
in Injectable at line 26
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();
in Injectable at line 43
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).
at line 81
__construct(array $items = [], array $priorities = [])
PrioritySorter constructor.
at line 92
array
getSortedList()
Sorts the items and returns a new version of $this->items
at line 119
$this
setPriorities(array $priorities)
Set the priorities for the items
at line 132
$this
setItems(array $items)
Sets the list of all items
at line 147
$this
setVariable(string $name, $value)
Add a variable for replacination, e.g. addVariable->('$project', 'myproject')
at line 160
$this
setRestKey($key)
The key used for "all other items"