You are here

protected static function Sort::expandFieldString in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/Query/Sort.php \Drupal\jsonapi\Query\Sort::expandFieldString()

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 core/modules/jsonapi/src/Query/Sort.php
Creates a Sort object from a query parameter.

File

core/modules/jsonapi/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));
}