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
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
An implementation of the factory method, allows you to create an instance of a class
Creates a class instance by the "singleton" design pattern.
Add a variable for replacination, e.g. addVariable->('$project', 'myproject')
If the "rest" key exists in the order array, replace it by the unspecified items
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();
        
                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"
        
                    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