diff --git a/src/Serializer/DefaultSerializer.php b/src/Serializer/DefaultSerializer.php index 0fc98cb..393d92f 100644 --- a/src/Serializer/DefaultSerializer.php +++ b/src/Serializer/DefaultSerializer.php @@ -2,65 +2,24 @@ namespace Contributte\Redis\Serializer; -use Contributte\Redis\Exception\LogicalException; - final class DefaultSerializer implements Serializer { - private const SERIALIZE_IGBINARY = 2; - private const SERIALIZE_PHP = 1; - - private const META_SERIALIZED = 'serialized'; - - /** @var bool */ - private $igbinary; - - public function __construct() - { - $this->igbinary = extension_loaded('igbinary'); - } - /** - * @param mixed $data - * @param mixed[] $meta - * @return string + * {@inheritDoc} */ public function serialize($data, array &$meta): string { - if ($this->igbinary) { - $meta[self::META_SERIALIZED] = self::SERIALIZE_IGBINARY; - return @igbinary_serialize($data); - } - - if (!is_string($data)) { - $meta[self::META_SERIALIZED] = self::SERIALIZE_PHP; - return @serialize($data); - } - - return $data; + return @serialize($data); } + /** - * @param string $data - * @param mixed[] $meta - * @return mixed + * {@inheritDoc} */ public function unserialize(string $data, array $meta) { - switch ($meta[self::META_SERIALIZED] ?? 0) { - case self::SERIALIZE_IGBINARY: - if ($this->igbinary) { - return @igbinary_unserialize($data); - } - - throw new LogicalException('Incompatible serialization method, igbinary is missing but required.'); - - case self::SERIALIZE_PHP: - return @unserialize($data); - - default: - return $data; - } + return @unserialize($data); } } diff --git a/src/Serializer/IgbinarySerializer.php b/src/Serializer/IgbinarySerializer.php new file mode 100644 index 0000000..7f660aa --- /dev/null +++ b/src/Serializer/IgbinarySerializer.php @@ -0,0 +1,24 @@ +serialize('foo', $meta)); + Assert::equal('a:1:{s:3:"foo";s:3:"bar";}', $serializer->serialize(['foo' => 'bar'], $meta)); + + Assert::equal('foo', $serializer->unserialize('s:3:"foo";', $meta)); + Assert::equal(['foo' => 'bar'], $serializer->unserialize('a:1:{s:3:"foo";s:3:"bar";}', $meta)); +}); diff --git a/tests/cases/Serializer/IgbinarySerializer.phpt b/tests/cases/Serializer/IgbinarySerializer.phpt new file mode 100644 index 0000000..62c7640 --- /dev/null +++ b/tests/cases/Serializer/IgbinarySerializer.phpt @@ -0,0 +1,24 @@ +serialize('foo', $meta)); + Assert::equal("\x00\x00\x00\x02\x14\x01\x11\x03foo\x11\x03bar", $serializer->serialize(['foo' => 'bar'], $meta)); + + Assert::equal('foo', $serializer->unserialize("\x00\x00\x00\x02\x11\x03foo", $meta)); + Assert::equal(['foo' => 'bar'], $serializer->unserialize("\x00\x00\x00\x02\x14\x01\x11\x03foo\x11\x03bar", $meta)); +});