public function hydrate($value) { if (!is_string($value)) { return $value; } return Serializer::unserialize($value); }
public function testUnserializeSpecificAdapter() { $adapter = new Adapter\Json(); $value = '"test"'; $expected = $adapter->unserialize($value); $this->assertEquals($expected, Serializer::unserialize($value, array('adapter' => $adapter))); }
/** * @param StreamName $streamName * @param array $metadata * @param null|int $minVersion * @return DomainEvent[] */ public function loadEventsByMetadataFrom(StreamName $streamName, array $metadata, $minVersion = null) { $tableGateway = $this->getTablegateway($streamName); $sql = $tableGateway->getSql(); $select = $sql->select()->order('version'); $where = new \Zend\Db\Sql\Where(); if (!is_null($minVersion)) { $where->AND->greaterThanOrEqualTo('version', $minVersion); } if (!empty($metadata)) { foreach ($metadata as $key => $value) { $where->AND->equalTo($key, (string) $value); } } $select->where($where); $eventsData = $tableGateway->selectWith($select); $events = array(); foreach ($eventsData as $eventData) { $payload = Serializer::unserialize($eventData->payload, $this->serializerAdapter); $eventClass = $eventData['event_class']; //Add metadata stored in table foreach ($eventData as $key => $value) { if (!in_array($key, $this->standardColumns)) { $metadata[$key] = $value; } } $events[] = $eventClass::fromArray(['uuid' => $eventData['event_id'], 'name' => $eventData['event_name'], 'version' => (int) $eventData['version'], 'created_at' => $eventData['created_at'], 'payload' => $payload, 'metadata' => $metadata]); } return $events; }
/** * 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; }
/** * set data from serialized string as object data * if data is an object set one variable where key is an object class name * * @param mixed $data * @return $this */ public function appendSerialized($data) { try { $data = Serializer::unserialize($data); } catch (ExceptionInterface $exception) { $this->_addException($exception); } if (is_object($data)) { $name = $this->_convertKeyNames(get_class($data)); $this->appendData($name, $data); } elseif (is_array($data)) { $this->appendArray($data); } else { $this->appendData($this->_defaultDataName, $data); } if ($this->_objectCreation) { return $this->_afterAppendDataToNewObject(); } return $this; }
/** * @param StreamName $streamName * @param array $metadata * @param null|int $minVersion * @return DomainEvent[] */ public function loadEventsByMetadataFrom(StreamName $streamName, array $metadata, $minVersion = null) { $queryBuilder = $this->connection->createQueryBuilder(); $table = $this->getTable($streamName); $queryBuilder->select('*')->from($table, $table)->orderBy('version', 'ASC'); foreach ($metadata as $key => $value) { $queryBuilder->andWhere($key . ' = :value' . $key)->setParameter('value' . $key, (string) $value); } if (!is_null($minVersion)) { $queryBuilder->andWhere('version >= :version')->setParameter('version', $minVersion); } /* @var $stmt \Doctrine\DBAL\Statement */ $stmt = $queryBuilder->execute(); $events = array(); foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $eventData) { $payload = Serializer::unserialize($eventData['payload'], $this->serializerAdapter); $eventClass = $eventData['event_class']; //Add metadata stored in table foreach ($eventData as $key => $value) { if (!in_array($key, $this->standardColumns)) { $metadata[$key] = $value; } } $events[] = $eventClass::fromArray(['uuid' => $eventData['event_id'], 'name' => $eventData['event_name'], 'version' => (int) $eventData['version'], 'created_at' => $eventData['created_at'], 'payload' => $payload, 'metadata' => $metadata]); } return $events; }