You are here

class OffsetPageNormalizer in JSON:API 8

The normalizer used for JSON API pagination.

@internal

Hierarchy

  • class \Drupal\jsonapi\Normalizer\OffsetPageNormalizer implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface

Expanded class hierarchy of OffsetPageNormalizer

1 string reference to 'OffsetPageNormalizer'
jsonapi.services.yml in ./jsonapi.services.yml
jsonapi.services.yml
1 service uses OffsetPageNormalizer
serializer.normalizer.offset_page.jsonapi in ./jsonapi.services.yml
Drupal\jsonapi\Normalizer\OffsetPageNormalizer

File

src/Normalizer/OffsetPageNormalizer.php, line 14

Namespace

Drupal\jsonapi\Normalizer
View source
class OffsetPageNormalizer implements DenormalizerInterface {

  /**
   * The interface or class that this Normalizer supports.
   *
   * @var string
   */
  protected $supportedInterfaceOrClass = OffsetPage::class;

  /**
   * {@inheritdoc}
   */
  public function supportsDenormalization($data, $type, $format = NULL) {
    return $type == $this->supportedInterfaceOrClass;
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) {
    $expanded = $this
      ->expand($data);
    return new OffsetPage($expanded[OffsetPage::OFFSET_KEY], $expanded[OffsetPage::SIZE_KEY]);
  }

  /**
   * {@inheritdoc}
   */
  protected function expand($data) {
    if (!is_array($data)) {
      throw new BadRequestHttpException('The page parameter needs to be an array.');
    }
    $expanded = $data + [
      OffsetPage::OFFSET_KEY => OffsetPage::DEFAULT_OFFSET,
      OffsetPage::SIZE_KEY => OffsetPage::SIZE_MAX,
    ];
    if ($expanded[OffsetPage::SIZE_KEY] > OffsetPage::SIZE_MAX) {
      $expanded[OffsetPage::SIZE_KEY] = OffsetPage::SIZE_MAX;
    }
    return $expanded;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OffsetPageNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports.
OffsetPageNormalizer::denormalize public function Denormalizes data back into an object of the given class.
OffsetPageNormalizer::expand protected function
OffsetPageNormalizer::supportsDenormalization public function Checks whether the given class is supported for denormalization by this normalizer.