You are here

protected function SortNormalizer::expandFieldString in JSON:API 8

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 SortNormalizer::expandFieldString()
SortNormalizer::expand in src/Normalizer/SortNormalizer.php

File

src/Normalizer/SortNormalizer.php, line 92

Class

SortNormalizer
The normalizer used for JSON API sorts.

Namespace

Drupal\jsonapi\Normalizer

Code

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