public function testCanUnserialize()
 {
     $serializer = Serializer::factory('phpserialize');
     $serializerStrategy = new SerializableStrategy($serializer);
     $serialized = $serializerStrategy->hydrate('s:3:"foo";');
     $this->assertEquals($serialized, 'foo');
 }
 /**
  * @return SerializerAdapterInterface
  */
 public function getSerializer()
 {
     if (null === $this->serializer) {
         $this->serializer = Serializer::factory('PhpSerialize');
     }
     return $this->serializer;
 }
Example #3
0
 public function hydrate($value)
 {
     if (!is_string($value)) {
         return $value;
     }
     return Serializer::unserialize($value);
 }
 /**
  * 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;
 }
Example #5
0
 /**
  * test basic object creation with serialized data
  *
  * @requires exampleCollection
  * @requires _exampleCollectionObject
  */
 public function testCreateSerializedCollection()
 {
     $data = $this->_exampleCollection();
     unset($data[7]);
     unset($data[8]);
     $serialized = Serializer::serialize($data);
     $collection = new Collection(['data' => $serialized, 'type' => 'serialized']);
     $this->assertEquals('lorem ipsum', $collection->first());
     $this->assertEquals($data[1]['data_first'], $collection->getElement(1)['data_first']);
 }
Example #6
0
 public function index01Action()
 {
     echo "<h3 style='color:red;font-weight:bold'>" . __METHOD__ . "</h3>";
     // $serializer = new \Zend\Serializer\Adapter\PhpSerialize();
     $serializer = \Zend\Serializer\Serializer::factory("PhpSerialize");
     $array = array("name" => "trongle", "age" => "23", "IQ" => 10, "handsome" => 10, "talent" => 10);
     $serialize = $serializer->serialize($array);
     echo $serialize;
     $arr = $serializer->unserialize($serialize);
     echo "<pre style='font-weight:bold'>";
     print_r($arr);
     echo "</pre>";
     return false;
 }
 /**
  * create serialized string to test
  *
  * @param mixed $first
  * @param mixed $second
  * @param bool @object
  * @return string
  */
 protected function _exampleSerializedData($first, $second, $object = false)
 {
     if ($object) {
         return Serializer::serialize((object) $this->_getSimpleData($first, $second));
     }
     return Serializer::serialize($this->_getSimpleData($first, $second));
 }
 /**
  * Returns the serializer adapter.
  * 
  * @throws Exception\MissingAdapterException
  * @throws Exception\MissingOptionException
  * @return \Zend\Serializer\Adapter\AdapterInterface
  */
 public function getAdapter()
 {
     if (!$this->_adapter instanceof \Zend\Serializer\Adapter\AdapterInterface) {
         $adapterConfig = $this->_options->get('adapter');
         if (!$adapterConfig) {
             throw new Exception\MissingAdapterException();
         }
         if (!isset($adapterConfig['name'])) {
             throw new Exception\MissingOptionException('adapter/name');
         }
         $name = $adapterConfig['name'];
         $options = array();
         if (isset($adapterConfig['options']) && is_array($adapterConfig['options'])) {
             $options = $adapterConfig['options'];
         }
         $this->_adapter = \Zend\Serializer\Serializer::factory($name, $options);
     }
     return $this->_adapter;
 }
Example #9
0
 public function testUnserializeSpecificAdapter()
 {
     $adapter = new Adapter\Json();
     $value = '"test"';
     $expected = $adapter->unserialize($value);
     $this->assertEquals($expected, Serializer::unserialize($value, array('adapter' => $adapter)));
 }
Example #10
0
 /**
  * Get serializer
  *
  * Used by:
  * - Serializer
  *
  * @return SerializerAdapter
  */
 public function getSerializer()
 {
     if (!$this->serializer instanceof SerializerAdapter) {
         // use default serializer
         if (!$this->serializer) {
             $this->setSerializer(SerializerFactory::getDefaultAdapter());
             // instantiate by class name + serializer_options
         } else {
             $options = $this->getSerializerOptions();
             $this->setSerializer(SerializerFactory::factory($this->serializer, $options));
         }
     }
     return $this->serializer;
 }
 /**
  * Insert an event
  *
  * @param StreamName $streamName
  * @param DomainEvent $e
  * @return void
  */
 protected function insertEvent(StreamName $streamName, DomainEvent $e)
 {
     $eventData = array('event_id' => $e->uuid()->toString(), 'version' => $e->version(), 'event_name' => $e->messageName(), 'event_class' => get_class($e), 'payload' => Serializer::serialize($e->payload(), $this->serializerAdapter), 'created_at' => $e->createdAt()->format(\DateTime::ISO8601));
     foreach ($e->metadata() as $key => $value) {
         $eventData[$key] = (string) $value;
     }
     $tableGateway = $this->getTablegateway($streamName);
     $tableGateway->insert($eventData);
 }
Example #12
0
 /**
  * apply serialized collection into object
  *
  * @param string $string
  * @return $this
  */
 public function unserialize($string)
 {
     $data = [];
     try {
         $data = Serializer::unserialize($string);
     } catch (ExceptionInterface $exception) {
         $this->_addException($exception);
     }
     foreach ($data as $element) {
         $this->addElement($element);
     }
     if ($this->_objectCreation) {
         return $this->_afterAppendDataToNewObject();
     }
     return $this;
 }
 /**
  * recursive method to create structure xml structure of object DATA
  *
  * @param $data
  * @param Xml $xml
  * @param boolean $addCdata
  * @param Xml|DOMElement $parent
  * @return Xml
  */
 protected function _arrayToXml($data, Xml $xml, $addCdata, $parent)
 {
     foreach ($data as $key => $value) {
         $key = str_replace(' ', '_', $key);
         $attributes = [];
         $data = '';
         if (is_object($value)) {
             try {
                 $data = Serializer::serialize($value);
             } catch (ExceptionInterface $exception) {
                 $this->_addException($exception);
             }
             $value = ['@attributes' => ['serialized_object' => true], $data];
         }
         try {
             $isArray = is_array($value);
             if ($isArray && array_key_exists('@attributes', $value)) {
                 $attributes = $value['@attributes'];
                 unset($value['@attributes']);
             }
             if ($isArray) {
                 $parent = $this->_convertArrayDataToXml($value, $addCdata, $xml, $key, $parent, $attributes);
                 continue;
             }
             $element = $this->_appendDataToNode($addCdata, $xml, $key, $value);
             $parent->appendChild($element);
         } catch (DOMException $exception) {
             $this->_addException($exception);
         }
     }
     return $xml;
 }