Permalink
Browse files

Added msgpack serializer

  • Loading branch information...
1 parent a68d8c6 commit 5687065c49f456e7ae8f4015e9f9161b2d94f130 @marc-mabe marc-mabe committed Aug 16, 2012
@@ -0,0 +1,91 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Serializer
+ */
+
+namespace Zend\Serializer\Adapter;
+
+use Zend\Serializer\Exception;
+use Zend\Stdlib\ErrorHandler;
+
+/**
+ * @category Zend
+ * @package Zend_Serializer
+ * @subpackage Adapter
+ */
+class MsgPack extends AbstractAdapter
+{
+ /**
+ * @var string Serialized 0 value
+ */
+ private static $serialized0 = null;
+
+ /**
+ * Constructor
+ *
+ * @throws Exception\ExtensionNotLoadedException If msgpack extension is not present
+ */
+ public function __construct($options = null)
+ {
+ if (!extension_loaded('msgpack')) {
+ throw new Exception\ExtensionNotLoadedException(
+ 'PHP extension "msgpack" is required for this adapter'
+ );
+ }
+
+ if (self::$serialized0 === null) {
+ self::$serialized0 = msgpack_serialize(0);
+ }
+
+ parent::__construct($options);
+ }
+
+ /**
+ * Serialize PHP value to msgpack
+ *
+ * @param mixed $value
+ * @return string
+ * @throws Exception\RuntimeException on msgpack error
+ */
+ public function serialize($value)
+ {
+ ErrorHandler::start();
+ $ret = msgpack_serialize($value);
+ $err = ErrorHandler::stop();
+
+ if ($ret === false) {
+ throw new Exception\RuntimeException('Serialization failed', 0, $err);
+ }
+
+ return $ret;
+ }
+
+ /**
+ * Deserialize msgpack string to PHP value
+ *
+ * @param string $serialized
+ * @return mixed
+ * @throws Exception\RuntimeException on msgpack error
+ */
+ public function unserialize($serialized)
+ {
+ if ($serialized === self::$serialized0) {
+ return 0;
+ }
+
+ ErrorHandler::start();
+ $ret = msgpack_unserialize($serialized);
+ $err = ErrorHandler::stop();
+
+ if ($ret === 0) {
+ throw new Exception\RuntimeException('Unserialization failed', 0, $err);
+ }
+
+ return $ret;
+ }
+}
@@ -32,6 +32,7 @@ class AdapterPluginManager extends AbstractPluginManager
protected $invokableClasses = array(
'igbinary' => 'Zend\Serializer\Adapter\IgBinary',
'json' => 'Zend\Serializer\Adapter\Json',
+ 'msgpack' => 'Zend\Serializer\Adapter\MsgPack',
'phpcode' => 'Zend\Serializer\Adapter\PhpCode',
'phpserialize' => 'Zend\Serializer\Adapter\PhpSerialize',
'pythonpickle' => 'Zend\Serializer\Adapter\PythonPickle',
@@ -0,0 +1,154 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Serializer
+ */
+
+namespace ZendTest\Serializer\Adapter;
+
+use Zend\Serializer;
+use Zend\Serializer\Exception\ExtensionNotLoadedException;
+
+/**
+ * @category Zend
+ * @package Zend_Serializer
+ * @subpackage UnitTests
+ * @group Zend_Serializer
+ */
+class MsgPackTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @var Serializer\Adapter\MsgPack
+ */
+ private $adapter;
+
+ public function setUp()
+ {
+ if (!extension_loaded('msgpack')) {
+ try {
+ new Serializer\Adapter\MsgPack();
+ $this->fail("Zend\\Serializer\\Adapter\\MsgPack needs missing ext/msgpack but did't throw exception");
+ } catch (ExtensionNotLoadedException $e) {}
+ $this->markTestSkipped('Zend\\Serializer\\Adapter\\MsgPack needs ext/msgpack');
+ }
+ $this->adapter = new Serializer\Adapter\MsgPack();
+ }
+
+ public function tearDown()
+ {
+ $this->adapter = null;
+ }
+
+ public function testSerializeString()
+ {
+ $value = 'test';
+ $expected = msgpack_serialize($value);
+
+ $data = $this->adapter->serialize($value);
+ $this->assertEquals($expected, $data);
+ }
+
+ public function testSerializeFalse()
+ {
+ $value = false;
+ $expected = msgpack_serialize($value);
+
+ $data = $this->adapter->serialize($value);
+ $this->assertEquals($expected, $data);
+ }
+
+ public function testSerializeNull()
+ {
+ $value = null;
+ $expected = msgpack_serialize($value);
+
+ $data = $this->adapter->serialize($value);
+ $this->assertEquals($expected, $data);
+ }
+
+ public function testSerializeNumeric()
+ {
+ $value = 100;
+ $expected = msgpack_serialize($value);
+
+ $data = $this->adapter->serialize($value);
+ $this->assertEquals($expected, $data);
+ }
+
+ public function testSerializeObject()
+ {
+ $value = new \stdClass();
+ $expected = msgpack_serialize($value);
+
+ $data = $this->adapter->serialize($value);
+ $this->assertEquals($expected, $data);
+ }
+
+ public function testUnserializeString()
+ {
+ $expected = 'test';
+ $value = msgpack_serialize($expected);
+
+ $data = $this->adapter->unserialize($value);
+ $this->assertEquals($expected, $data);
+ }
+
+ public function testUnserializeFalse()
+ {
+ $expected = false;
+ $value = msgpack_serialize($expected);
+
+ $data = $this->adapter->unserialize($value);
+ $this->assertEquals($expected, $data);
+ }
+
+ public function testUnserializeNull()
+ {
+ $expected = null;
+ $value = msgpack_serialize($expected);
+
+ $data = $this->adapter->unserialize($value);
+ $this->assertEquals($expected, $data);
+ }
+
+ public function testUnserializeNumeric()
+ {
+ $expected = 100;
+ $value = msgpack_serialize($expected);
+
+ $data = $this->adapter->unserialize($value);
+ $this->assertEquals($expected, $data);
+ }
+
+ public function testUnserializeObject()
+ {
+ $expected = new \stdClass();
+ $value = msgpack_serialize($expected);
+
+ $data = $this->adapter->unserialize($value);
+ $this->assertEquals($expected, $data);
+ }
+
+ public function testUnserialize0()
+ {
+ $expected = 0;
+ $value = msgpack_serialize($expected);
+
+ $data = $this->adapter->unserialize($value);
+ $this->assertEquals($expected, $data);
+ }
+
+ public function testUnserialzeInvalid()
+ {
+ $value = "\0\1\r\n";
+ $this->setExpectedException(
+ 'Zend\Serializer\Exception\RuntimeException',
+ 'Unserialization failed'
+ );
+ $this->adapter->unserialize($value);
+ }
+}

0 comments on commit 5687065

Please sign in to comment.