You are here

protected static function Sort::expandFieldString in JSON:API 8.2

Expands a simple string sort into a more expressive sort that we can use.

Parameters

string $fields: The comma separated list of fields to expand into an array.

Return value

array The expanded sort.

1 call to Sort::expandFieldString()
Sort::createFromQueryParameter in src/Query/Sort.php
Creates a Sort object from a query parameter.

File

src/Query/Sort.php, line 122

Class

Sort
Gathers information about the sort parameter.

Namespace

Drupal\jsonapi\Query

Code

protected static function expandFieldString($fields) {
  return array_map(function ($field) {
    $sort = [];
    if ($field[0] == '-') {
      $sort[static::DIRECTION_KEY] = 'DESC';
      $sort[static::PATH_KEY] = substr($field, 1);
    }
    else {
      $sort[static::DIRECTION_KEY] = 'ASC';
      $sort[static::PATH_KEY] = $field;
    }
    return $sort;
  }, explode(',', $fields));
}