You are here

public static function OffsetPage::createFromQueryParameter in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/Query/OffsetPage.php \Drupal\jsonapi\Query\OffsetPage::createFromQueryParameter()

Creates an OffsetPage object from a query parameter.

Parameters

mixed $parameter: The `page` query parameter from the Symfony request object.

Return value

static An OffsetPage object with defaults.

3 calls to OffsetPage::createFromQueryParameter()
EntityResource::getJsonApiParams in core/modules/jsonapi/src/Controller/EntityResource.php
Extracts JSON:API query parameters from the request.
OffsetPageTest::testCreateFromQueryParameter in core/modules/jsonapi/tests/src/Unit/Query/OffsetPageTest.php
@covers ::createFromQueryParameter @dataProvider parameterProvider
OffsetPageTest::testCreateFromQueryParameterFail in core/modules/jsonapi/tests/src/Unit/Query/OffsetPageTest.php
@covers ::createFromQueryParameter

File

core/modules/jsonapi/src/Query/OffsetPage.php, line 110

Class

OffsetPage
Value object for containing the requested offset and page parameters.

Namespace

Drupal\jsonapi\Query

Code

public static function createFromQueryParameter($parameter) {
  if (!is_array($parameter)) {
    $cacheability = (new CacheableMetadata())
      ->addCacheContexts([
      'url.query_args:page',
    ]);
    throw new CacheableBadRequestHttpException($cacheability, 'The page parameter needs to be an array.');
  }
  $expanded = $parameter + [
    static::OFFSET_KEY => static::DEFAULT_OFFSET,
    static::SIZE_KEY => static::SIZE_MAX,
  ];
  if ($expanded[static::SIZE_KEY] > static::SIZE_MAX) {
    $expanded[static::SIZE_KEY] = static::SIZE_MAX;
  }
  return new static($expanded[static::OFFSET_KEY], $expanded[static::SIZE_KEY]);
}