SortNormalizer.php in JSON:API 8
File
src/Normalizer/SortNormalizer.php
View source
<?php
namespace Drupal\jsonapi\Normalizer;
use Drupal\jsonapi\Query\Sort;
use Drupal\jsonapi\Context\FieldResolver;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class SortNormalizer implements DenormalizerInterface {
protected $supportedInterfaceOrClass = Sort::class;
protected $fieldResolver;
public function __construct(FieldResolver $field_resolver) {
$this->fieldResolver = $field_resolver;
}
public function supportsDenormalization($data, $type, $format = NULL) {
return $type == $this->supportedInterfaceOrClass;
}
public function denormalize($data, $class, $format = NULL, array $context = []) {
$expanded = $this
->expand($data);
$expanded = array_map(function ($item) use ($context) {
$item[Sort::PATH_KEY] = $this->fieldResolver
->resolveInternalEntityQueryPath($context['entity_type_id'], $context['bundle'], $item[Sort::PATH_KEY]);
return $item;
}, $expanded);
return new Sort($expanded);
}
protected function expand($sort) {
if (empty($sort)) {
throw new BadRequestHttpException('You need to provide a value for the sort parameter.');
}
if (is_string($sort)) {
$sort = $this
->expandFieldString($sort);
}
$expanded = [];
foreach ($sort as $sort_index => $sort_item) {
$expanded[$sort_index] = $this
->expandItem($sort_index, $sort_item);
}
return $expanded;
}
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));
}
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);
if (count(array_diff($expected_keys, array_keys($expanded))) > 0) {
throw new BadRequestHttpException('You have provided an invalid set of sort keys.');
}
return $expanded;
}
}