You are here

NormalizerBase.php in JSON:API 8

Same filename and directory in other branches
  1. 8.2 src/Normalizer/NormalizerBase.php

File

src/Normalizer/NormalizerBase.php
View source
<?php

namespace Drupal\jsonapi\Normalizer;

use Drupal\serialization\Normalizer\NormalizerBase as SerializationNormalizerBase;

/**
 * Base normalizer used in all JSON API normalizers.
 *
 * @internal
 */
abstract class NormalizerBase extends SerializationNormalizerBase {

  /**
   * The formats that the Normalizer can handle.
   *
   * @var array
   */
  protected $formats = [
    'api_json',
  ];

  /**
   * {@inheritdoc}
   */
  public function supportsNormalization($data, $format = NULL) {
    return in_array($format, $this->formats, TRUE) && parent::supportsNormalization($data, $format);
  }

  /**
   * {@inheritdoc}
   */
  public function supportsDenormalization($data, $type, $format = NULL) {
    if (in_array($format, $this->formats, TRUE) && (class_exists($this->supportedInterfaceOrClass) || interface_exists($this->supportedInterfaceOrClass))) {
      $target = new \ReflectionClass($type);
      $supported = new \ReflectionClass($this->supportedInterfaceOrClass);
      if ($supported
        ->isInterface()) {
        return $target
          ->implementsInterface($this->supportedInterfaceOrClass);
      }
      else {
        return $target
          ->getName() == $this->supportedInterfaceOrClass || $target
          ->isSubclassOf($this->supportedInterfaceOrClass);
      }
    }
    return FALSE;
  }

}

Classes

Namesort descending Description
NormalizerBase Base normalizer used in all JSON API normalizers.