Ejemplo n.º 1
0
 public function testUnserializeFailsIfNoString()
 {
     try {
         Serializer::unserialize(1234);
         $this->fail('Expected an UnserializationFailedException');
     } catch (UnserializationFailedException $e) {
         $this->assertContains('Could not unserialize value of type integer.', $e->getMessage());
     }
 }
 /**
  * {@inheritdoc}
  */
 public function getOrFail($key)
 {
     KeyUtil::validate($key);
     try {
         $serialized = $this->clientGet($key);
     } catch (Exception $e) {
         throw ReadException::forException($e);
     }
     if ($this->clientNotFoundValue() === $serialized) {
         throw NoSuchKeyException::forKey($key);
     }
     return Serializer::unserialize($serialized);
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function getMultipleOrFail(array $keys)
 {
     KeyUtil::validateMultiple($keys);
     // Normalize indices of the array
     $keys = array_values($keys);
     $data = $this->doGetMultiple($keys);
     $results = array();
     $resolved = array();
     foreach ($data as $row) {
         $results[$row['meta_key']] = Serializer::unserialize($row['meta_value']);
         $resolved[] = $row['meta_key'];
     }
     $notResolvedArr = array_diff($keys, $resolved);
     if (!empty($notResolvedArr)) {
         throw NoSuchKeyException::forKeys($notResolvedArr);
     }
     return $results;
 }
Ejemplo n.º 4
0
 public function __construct(Collection $collection, $flags = 0)
 {
     $this->collection = $collection;
     if ($flags & self::NO_SERIALIZE) {
         if ($flags & self::SUPPORT_BINARY) {
             $this->serialize = function ($unserialized) {
                 if (!is_string($unserialized)) {
                     throw UnsupportedValueException::forValue($unserialized, $this);
                 }
                 return new Binary($unserialized, Binary::TYPE_GENERIC);
             };
             $this->unserialize = function (Binary $serialized) {
                 return $serialized->getData();
             };
         } else {
             $this->serialize = function ($unserialized) {
                 if (!is_scalar($unserialized) && !is_array($unserialized) && null !== $unserialized) {
                     throw UnsupportedValueException::forValue($unserialized, $this);
                 }
                 return $unserialized;
             };
             $this->unserialize = function ($serialized) {
                 return $serialized;
             };
         }
     } else {
         if ($flags & self::SUPPORT_BINARY) {
             $this->serialize = function ($unserialized) {
                 return new Binary(Serializer::serialize($unserialized), Binary::TYPE_GENERIC);
             };
             $this->unserialize = function (Binary $serialized) {
                 return Serializer::unserialize($serialized->getData());
             };
         } else {
             $this->serialize = function ($unserialized) {
                 return Serializer::serialize($unserialized);
             };
             $this->unserialize = function ($serialized) {
                 return Serializer::unserialize($serialized);
             };
         }
     }
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function getMultipleOrFail(array $keys)
 {
     KeyUtil::validateMultiple($keys);
     $values = array();
     $notFoundKeys = array();
     try {
         $bucket = $this->client->bucket($this->bucketName);
         foreach ($keys as $key) {
             $values[$key] = $bucket->getBinary($key);
             if (!$values[$key]->exists()) {
                 $notFoundKeys[] = $key;
             }
         }
     } catch (Exception $e) {
         throw ReadException::forException($e);
     }
     if (0 !== count($notFoundKeys)) {
         throw NoSuchKeyException::forKeys($notFoundKeys);
     }
     foreach ($values as $key => $object) {
         $values[$key] = Serializer::unserialize($object->getData());
     }
     return $values;
 }
Ejemplo n.º 6
0
 private function unserializeValue($value)
 {
     // Unserialize value if it is a string...
     $unserializeValue = is_string($value) && (!($this->flags & self::NO_SERIALIZE_STRINGS) || 'O:' === ($prefix = substr($value, 0, 2)) || 'a:' === $prefix && !($this->flags & self::NO_SERIALIZE_ARRAYS));
     if ($unserializeValue) {
         return Serializer::unserialize($value);
     }
     if (is_array($value)) {
         return array_map(array($this, 'unserializeValue'), $value);
     }
     return $value;
 }