[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/zendframework/zend-stdlib/src/Hydrator/Strategy/ -> SerializableStrategy.php (source)

   1  <?php
   2  /**
   3   * Zend Framework (http://framework.zend.com/)
   4   *
   5   * @link      http://github.com/zendframework/zf2 for the canonical source repository
   6   * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   7   * @license   http://framework.zend.com/license/new-bsd New BSD License
   8   */
   9  
  10  namespace Zend\Stdlib\Hydrator\Strategy;
  11  
  12  use Zend\Stdlib\Exception\InvalidArgumentException;
  13  use Zend\Serializer\Adapter\AdapterInterface as SerializerAdapter;
  14  use Zend\Serializer\Serializer as SerializerFactory;
  15  
  16  class SerializableStrategy implements StrategyInterface
  17  {
  18      /**
  19       * @var string|SerializerAdapter
  20       */
  21      protected $serializer;
  22  
  23      /**
  24       * @var array
  25       */
  26      protected $serializerOptions = array();
  27  
  28      /**
  29       *
  30       * @param mixed $serializer string or SerializerAdapter
  31       * @param mixed $serializerOptions
  32       */
  33      public function __construct($serializer, $serializerOptions = null)
  34      {
  35          $this->setSerializer($serializer);
  36          if ($serializerOptions) {
  37              $this->setSerializerOptions($serializerOptions);
  38          }
  39      }
  40  
  41      /**
  42       * Serialize the given value so that it can be extracted by the hydrator.
  43       *
  44       * @param mixed $value The original value.
  45       * @return mixed Returns the value that should be extracted.
  46       */
  47      public function extract($value)
  48      {
  49          $serializer = $this->getSerializer();
  50          return $serializer->serialize($value);
  51      }
  52  
  53      /**
  54       * Unserialize the given value so that it can be hydrated by the hydrator.
  55       *
  56       * @param mixed $value The original value.
  57       * @return mixed Returns the value that should be hydrated.
  58       */
  59      public function hydrate($value)
  60      {
  61          $serializer = $this->getSerializer();
  62          return $serializer->unserialize($value);
  63      }
  64  
  65      /**
  66       * Set serializer
  67       *
  68       * @param  string|SerializerAdapter $serializer
  69       * @return SerializableStrategy
  70       */
  71      public function setSerializer($serializer)
  72      {
  73          if (!is_string($serializer) && !$serializer instanceof SerializerAdapter) {
  74              throw new InvalidArgumentException(sprintf(
  75                  '%s expects either a string serializer name or Zend\Serializer\Adapter\AdapterInterface instance; '
  76                  . 'received "%s"',
  77                  __METHOD__,
  78                  (is_object($serializer) ? get_class($serializer) : gettype($serializer))
  79              ));
  80          }
  81          $this->serializer = $serializer;
  82          return $this;
  83      }
  84  
  85      /**
  86       * Get serializer
  87       *
  88       * @return SerializerAdapter
  89       */
  90      public function getSerializer()
  91      {
  92          if (is_string($this->serializer)) {
  93              $options = $this->getSerializerOptions();
  94              $this->setSerializer(SerializerFactory::factory($this->serializer, $options));
  95          } elseif (null === $this->serializer) {
  96              $this->setSerializer(SerializerFactory::getDefaultAdapter());
  97          }
  98  
  99          return $this->serializer;
 100      }
 101  
 102      /**
 103       * Set configuration options for instantiating a serializer adapter
 104       *
 105       * @param  mixed $serializerOptions
 106       * @return SerializableStrategy
 107       */
 108      public function setSerializerOptions($serializerOptions)
 109      {
 110          $this->serializerOptions = $serializerOptions;
 111          return $this;
 112      }
 113  
 114      /**
 115       * Get configuration options for instantiating a serializer adapter
 116       *
 117       * @return mixed
 118       */
 119      public function getSerializerOptions()
 120      {
 121          return $this->serializerOptions;
 122      }
 123  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1