SerializableStrategy.php in Zircon Profile 8.0
File
vendor/zendframework/zend-hydrator/src/Strategy/SerializableStrategy.php
View source
<?php
namespace Zend\Hydrator\Strategy;
use Zend\Hydrator\Exception\InvalidArgumentException;
use Zend\Serializer\Adapter\AdapterInterface as SerializerAdapter;
use Zend\Serializer\Serializer as SerializerFactory;
class SerializableStrategy implements StrategyInterface {
protected $serializer;
protected $serializerOptions = [];
public function __construct($serializer, $serializerOptions = null) {
$this
->setSerializer($serializer);
if ($serializerOptions) {
$this
->setSerializerOptions($serializerOptions);
}
}
public function extract($value) {
$serializer = $this
->getSerializer();
return $serializer
->serialize($value);
}
public function hydrate($value) {
$serializer = $this
->getSerializer();
return $serializer
->unserialize($value);
}
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;
}
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;
}
public function setSerializerOptions($serializerOptions) {
$this->serializerOptions = $serializerOptions;
return $this;
}
public function getSerializerOptions() {
return $this->serializerOptions;
}
}