Tobscure\JsonApi\Parameters::getSort PHP Method

getSort() public method

Get the sort.
public getSort ( array $available = [] ) : array
$available array
return array
    public function getSort(array $available = [])
    {
        $sort = [];
        if ($input = $this->getInput('sort')) {
            $fields = explode(',', $input);
            foreach ($fields as $field) {
                if (substr($field, 0, 1) === '-') {
                    $field = substr($field, 1);
                    $order = 'desc';
                } else {
                    $order = 'asc';
                }
                $sort[$field] = $order;
            }
            $invalid = array_diff(array_keys($sort), $available);
            if (count($invalid)) {
                throw new InvalidParameterException('Invalid sort fields [' . implode(',', $invalid) . ']', 3, null, 'sort');
            }
        }
        return $sort;
    }

Usage Example

Example #1
0
 public function testGetSortDefaultsToEmptyArray()
 {
     $parameters = new Parameters([]);
     $this->assertEmpty($parameters->getSort());
 }
All Usage Examples Of Tobscure\JsonApi\Parameters::getSort