You are here

class SerializableStrategy in Zircon Profile 8

Same name in this branch
  1. 8 vendor/zendframework/zend-hydrator/src/Strategy/SerializableStrategy.php \Zend\Hydrator\Strategy\SerializableStrategy
  2. 8 vendor/zendframework/zend-stdlib/src/Hydrator/Strategy/SerializableStrategy.php \Zend\Stdlib\Hydrator\Strategy\SerializableStrategy
Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-hydrator/src/Strategy/SerializableStrategy.php \Zend\Hydrator\Strategy\SerializableStrategy

Hierarchy

Expanded class hierarchy of SerializableStrategy

1 file declares its use of SerializableStrategy
SerializableStrategy.php in vendor/zendframework/zend-stdlib/src/Hydrator/Strategy/SerializableStrategy.php

File

vendor/zendframework/zend-hydrator/src/Strategy/SerializableStrategy.php, line 16

Namespace

Zend\Hydrator\Strategy
View source
class SerializableStrategy implements StrategyInterface {

  /**
   * @var string|SerializerAdapter
   */
  protected $serializer;

  /**
   * @var array
   */
  protected $serializerOptions = [];

  /**
   *
   * @param mixed $serializer string or SerializerAdapter
   * @param mixed $serializerOptions
   */
  public function __construct($serializer, $serializerOptions = null) {
    $this
      ->setSerializer($serializer);
    if ($serializerOptions) {
      $this
        ->setSerializerOptions($serializerOptions);
    }
  }

  /**
   * Serialize the given value so that it can be extracted by the hydrator.
   *
   * @param mixed $value The original value.
   * @return mixed Returns the value that should be extracted.
   */
  public function extract($value) {
    $serializer = $this
      ->getSerializer();
    return $serializer
      ->serialize($value);
  }

  /**
   * Unserialize the given value so that it can be hydrated by the hydrator.
   *
   * @param mixed $value The original value.
   * @return mixed Returns the value that should be hydrated.
   */
  public function hydrate($value) {
    $serializer = $this
      ->getSerializer();
    return $serializer
      ->unserialize($value);
  }

  /**
   * Set serializer
   *
   * @param  string|SerializerAdapter $serializer
   * @return SerializableStrategy
   */
  public function setSerializer($serializer) {
    if (!is_string($serializer) && !$serializer instanceof SerializerAdapter) {
      throw new InvalidArgumentException(sprintf('%s expects either a string serializer name or Zend\\Serializer\\Adapter\\AdapterInterface instance; ' . 'received "%s"', __METHOD__, is_object($serializer) ? get_class($serializer) : gettype($serializer)));
    }
    $this->serializer = $serializer;
    return $this;
  }

  /**
   * Get serializer
   *
   * @return SerializerAdapter
   */
  public function getSerializer() {
    if (is_string($this->serializer)) {
      $options = $this
        ->getSerializerOptions();
      $this
        ->setSerializer(SerializerFactory::factory($this->serializer, $options));
    }
    elseif (null === $this->serializer) {
      $this
        ->setSerializer(SerializerFactory::getDefaultAdapter());
    }
    return $this->serializer;
  }

  /**
   * Set configuration options for instantiating a serializer adapter
   *
   * @param  mixed $serializerOptions
   * @return SerializableStrategy
   */
  public function setSerializerOptions($serializerOptions) {
    $this->serializerOptions = $serializerOptions;
    return $this;
  }

  /**
   * Get configuration options for instantiating a serializer adapter
   *
   * @return mixed
   */
  public function getSerializerOptions() {
    return $this->serializerOptions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SerializableStrategy::$serializer protected property
SerializableStrategy::$serializerOptions protected property
SerializableStrategy::extract public function Serialize the given value so that it can be extracted by the hydrator. Overrides StrategyInterface::extract
SerializableStrategy::getSerializer public function Get serializer
SerializableStrategy::getSerializerOptions public function Get configuration options for instantiating a serializer adapter
SerializableStrategy::hydrate public function Unserialize the given value so that it can be hydrated by the hydrator. Overrides StrategyInterface::hydrate
SerializableStrategy::setSerializer public function Set serializer
SerializableStrategy::setSerializerOptions public function Set configuration options for instantiating a serializer adapter
SerializableStrategy::__construct public function