You are here

protected function SortNormalizer::expandItem in JSON:API 8

Expands a sort item in case a shortcut was used.

Parameters

string $sort_index: Unique identifier for the sort parameter being expanded.

array $sort_item: The raw sort item.

Return value

array The expanded sort item.

1 call to SortNormalizer::expandItem()
SortNormalizer::expand in src/Normalizer/SortNormalizer.php

File

src/Normalizer/SortNormalizer.php, line 120

Class

SortNormalizer
The normalizer used for JSON API sorts.

Namespace

Drupal\jsonapi\Normalizer

Code

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

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