You are here

class FileEntityNormalizer in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/hal/src/Normalizer/FileEntityNormalizer.php \Drupal\hal\Normalizer\FileEntityNormalizer

Converts the Drupal entity object structure to a HAL array structure.

Hierarchy

Expanded class hierarchy of FileEntityNormalizer

1 file declares its use of FileEntityNormalizer
FileNormalizeTest.php in core/modules/hal/src/Tests/FileNormalizeTest.php
Contains \Drupal\hal\Tests\FileNormalizeTest.
1 string reference to 'FileEntityNormalizer'
hal.services.yml in core/modules/hal/hal.services.yml
core/modules/hal/hal.services.yml
1 service uses FileEntityNormalizer
serializer.normalizer.file_entity.hal in core/modules/hal/hal.services.yml
Drupal\hal\Normalizer\FileEntityNormalizer

File

core/modules/hal/src/Normalizer/FileEntityNormalizer.php, line 18
Contains \Drupal\hal\Normalizer\FileEntityNormalizer.

Namespace

Drupal\hal\Normalizer
View source
class FileEntityNormalizer extends ContentEntityNormalizer {

  /**
   * The interface or class that this Normalizer supports.
   *
   * @var string
   */
  protected $supportedInterfaceOrClass = 'Drupal\\file\\FileInterface';

  /**
   * The HTTP client.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $httpClient;

  /**
   * Constructs a FileEntityNormalizer object.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   * @param \GuzzleHttp\ClientInterface $http_client
   *   The HTTP Client.
   * @param \Drupal\rest\LinkManager\LinkManagerInterface $link_manager
   *   The hypermedia link manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
  public function __construct(EntityManagerInterface $entity_manager, ClientInterface $http_client, LinkManagerInterface $link_manager, ModuleHandlerInterface $module_handler) {
    parent::__construct($link_manager, $entity_manager, $module_handler);
    $this->httpClient = $http_client;
  }

  /**
   * {@inheritdoc}
   */
  public function normalize($entity, $format = NULL, array $context = array()) {
    $data = parent::normalize($entity, $format, $context);

    // Replace the file url with a full url for the file.
    $data['uri'][0]['value'] = $this
      ->getEntityUri($entity);
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = array()) {
    $file_data = (string) $this->httpClient
      ->get($data['uri'][0]['value'])
      ->getBody();
    $path = 'temporary://' . drupal_basename($data['uri'][0]['value']);
    $data['uri'] = file_unmanaged_save_data($file_data, $path);
    return $this->entityManager
      ->getStorage('file')
      ->create($data);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentEntityNormalizer::$entityManager protected property The entity manager.
ContentEntityNormalizer::$linkManager protected property The hypermedia link manager.
ContentEntityNormalizer::$moduleHandler protected property The module handler.
ContentEntityNormalizer::getEntityUri protected function Constructs the entity URI.
ContentEntityNormalizer::getTypedDataIds protected function Gets the typed data IDs for a type URI.
FileEntityNormalizer::$httpClient protected property The HTTP client.
FileEntityNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides ContentEntityNormalizer::$supportedInterfaceOrClass
FileEntityNormalizer::denormalize public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize(). Overrides ContentEntityNormalizer::denormalize
FileEntityNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides ContentEntityNormalizer::normalize
FileEntityNormalizer::__construct public function Constructs a FileEntityNormalizer object. Overrides ContentEntityNormalizer::__construct
NormalizerBase::$formats protected property The formats that the Normalizer can handle.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer.
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase::supportsDenormalization
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. Overrides NormalizerBase::supportsNormalization
SerializerAwareNormalizer::$serializer protected property
SerializerAwareNormalizer::setSerializer public function Sets the owning Serializer object. Overrides SerializerAwareInterface::setSerializer