Bolt\Twig\Handler\ArrayHandler::order PHP Method

order() public method

Sorts / orders items of an array.
public order ( array $array, string $on, string $onSecondary = '' ) : array
$array array
$on string
$onSecondary string
return array
    public function order($array, $on, $onSecondary = '')
    {
        // Set the 'orderOn' and 'orderAscending', taking into account things like '-datepublish'.
        list($this->orderOn, $this->orderAscending) = $this->app['storage']->getSortOrder($on);
        // Set the secondary order, if any.
        if (!empty($onSecondary)) {
            list($this->orderOnSecondary, $this->orderAscendingSecondary) = $this->app['storage']->getSortOrder($onSecondary);
        } else {
            $this->orderOnSecondary = false;
            $this->orderAscendingSecondary = false;
        }
        uasort($array, [$this, 'orderHelper']);
        return $array;
    }

Usage Example

Example #1
0
 public function testOrderNameMatchNoSecondary()
 {
     $app = $this->getApp();
     $srcArr = [['name' => 'Johno', 'type' => 'koala'], ['name' => 'Bruce', 'type' => 'clippy'], ['name' => 'Johno', 'type' => 'batman']];
     $handler = new ArrayHandler($app);
     $result = $handler->order($srcArr, 'name');
     $this->assertRegExp('#{"[0-2]":{"name":"Bruce","type":"clippy"},"[0-2]":{"name":"Johno","type":"(batman|koala)"},"[0-2]":{"name":"Johno","type":"(koala|batman)"}}#', json_encode($result));
 }