You are here

public static function OffsetPage::createFromQueryParameter in JSON:API 8.2

Creates an OffsetPage object from a query parameter.

Parameters

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

Return value

\Drupal\jsonapi\Query\OffsetPage An OffsetPage object with defaults.

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

File

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]);
}