You are here

class JsonApiParamEnhancer in JSON:API 8

Processes the request query parameters.

@internal

Hierarchy

Expanded class hierarchy of JsonApiParamEnhancer

1 file declares its use of JsonApiParamEnhancer
JsonApiParamEnhancerTest.php in tests/src/Unit/Routing/JsonApiParamEnhancerTest.php
1 string reference to 'JsonApiParamEnhancer'
jsonapi.services.yml in ./jsonapi.services.yml
jsonapi.services.yml
1 service uses JsonApiParamEnhancer
jsonapi.params.enhancer in ./jsonapi.services.yml
Drupal\jsonapi\Routing\JsonApiParamEnhancer

File

src/Routing/JsonApiParamEnhancer.php, line 18

Namespace

Drupal\jsonapi\Routing
View source
class JsonApiParamEnhancer implements RouteEnhancerInterface {

  /**
   * The filter normalizer.
   *
   * @var \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
   */
  protected $filterNormalizer;

  /**
   * The sort normalizer.
   *
   * @var \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
   */
  protected $sortNormalizer;

  /**
   * The page normalizer.
   *
   * @var Symfony\Component\Serializer\Normalizer\DenormalizerInterface
   */
  protected $pageNormalizer;

  /**
   * {@inheritdoc}
   */
  public function __construct(DenormalizerInterface $filter_normalizer, DenormalizerInterface $sort_normalizer, DenormalizerInterface $page_normalizer) {
    $this->filterNormalizer = $filter_normalizer;
    $this->sortNormalizer = $sort_normalizer;
    $this->pageNormalizer = $page_normalizer;
  }

  /**
   * {@inheritdoc}
   */
  public function applies(Route $route) {
    return (bool) Routes::getResourceTypeNameFromParameters($route
      ->getDefaults());
  }

  /**
   * {@inheritdoc}
   */
  public function enhance(array $defaults, Request $request) {
    $options = [];
    $resource_type = Routes::getResourceTypeNameFromParameters($defaults);
    $context = [
      'entity_type_id' => $resource_type
        ->getEntityTypeId(),
      'bundle' => $resource_type
        ->getBundle(),
    ];
    if ($request->query
      ->has('sort')) {
      $sort = $request->query
        ->get('sort');
      $options['sort'] = $this->sortNormalizer
        ->denormalize($sort, Sort::class, NULL, $context);
    }
    $page = $request->query
      ->has('page') ? $request->query
      ->get('page') : [];
    $options['page'] = $this->pageNormalizer
      ->denormalize($page, OffsetPage::class);
    $defaults['_json_api_params'] = $options;
    return $defaults;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JsonApiParamEnhancer::$filterNormalizer protected property The filter normalizer.
JsonApiParamEnhancer::$pageNormalizer protected property The page normalizer.
JsonApiParamEnhancer::$sortNormalizer protected property The sort normalizer.
JsonApiParamEnhancer::applies public function Declares if the route enhancer applies to the given route. Overrides RouteEnhancerInterface::applies
JsonApiParamEnhancer::enhance public function Update the defaults based on its own data and the request.
JsonApiParamEnhancer::__construct public function