You are here

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

Expands a sort item in case a shortcut was used.

Parameters

array $sort_item: The raw sort item.

Return value

array The expanded sort item.

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

File

src/Query/Sort.php, line 148

Class

Sort
Gathers information about the sort parameter.

Namespace

Drupal\jsonapi\Query

Code

protected static function expandItem(array $sort_item) {
  $cacheability = (new CacheableMetadata())
    ->addCacheContexts([
    'url.query_args:sort',
  ]);
  $defaults = [
    static::DIRECTION_KEY => 'ASC',
    static::LANGUAGE_KEY => NULL,
  ];
  if (!isset($sort_item[static::PATH_KEY])) {
    throw new CacheableBadRequestHttpException($cacheability, 'You need to provide a field name for the sort parameter.');
  }
  $expected_keys = [
    static::PATH_KEY,
    static::DIRECTION_KEY,
    static::LANGUAGE_KEY,
  ];
  $expanded = array_merge($defaults, $sort_item);

  // Verify correct sort keys.
  if (count(array_diff($expected_keys, array_keys($expanded))) > 0) {
    throw new CacheableBadRequestHttpException($cacheability, 'You have provided an invalid set of sort keys.');
  }
  return $expanded;
}