Alexander Makarov
7 years ago
committed by
GitHub
13 changed files with 392 additions and 19 deletions
@ -0,0 +1,45 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\serialize; |
||||
|
||||
use yii\base\BaseObject; |
||||
|
||||
/** |
||||
* CallbackSerializer serializes data via custom PHP callback. |
||||
* |
||||
* @author Paul Klimov <klimov.paul@gmail.com> |
||||
* @since 2.1.0 |
||||
*/ |
||||
class CallbackSerializer extends BaseObject implements SerializerInterface |
||||
{ |
||||
/** |
||||
* @var callable PHP callback, which should be used to serialize value. |
||||
*/ |
||||
public $serialize; |
||||
/** |
||||
* @var callable PHP callback, which should be used to unserialize value. |
||||
*/ |
||||
public $unserialize; |
||||
|
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function serialize($value) |
||||
{ |
||||
return call_user_func($this->serialize, $value); |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function unserialize($value) |
||||
{ |
||||
return call_user_func($this->unserialize, $value); |
||||
} |
||||
} |
@ -0,0 +1,36 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\serialize; |
||||
|
||||
use yii\base\BaseObject; |
||||
|
||||
/** |
||||
* IgbinarySerializer uses [Igbinary PHP extension](http://pecl.php.net/package/igbinary) for serialization. |
||||
* Make sure you have 'igbinary' PHP extension install at your system before attempt to use this serializer. |
||||
* |
||||
* @author Paul Klimov <klimov.paul@gmail.com> |
||||
* @since 2.1.0 |
||||
*/ |
||||
class IgbinarySerializer extends BaseObject implements SerializerInterface |
||||
{ |
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function serialize($value) |
||||
{ |
||||
return igbinary_serialize($value); |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function unserialize($value) |
||||
{ |
||||
return igbinary_unserialize($value); |
||||
} |
||||
} |
@ -0,0 +1,44 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\serialize; |
||||
|
||||
use yii\base\BaseObject; |
||||
use yii\helpers\Json; |
||||
|
||||
/** |
||||
* JsonSerializer serializes data in JSON format. |
||||
* |
||||
* @author Paul Klimov <klimov.paul@gmail.com> |
||||
* @since 2.1.0 |
||||
*/ |
||||
class JsonSerializer extends BaseObject implements SerializerInterface |
||||
{ |
||||
/** |
||||
* @var integer the encoding options. For more details please refer to |
||||
* <http://www.php.net/manual/en/function.json-encode.php>. |
||||
* Default is `JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE`. |
||||
*/ |
||||
public $options = 320; |
||||
|
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function serialize($value) |
||||
{ |
||||
return Json::encode($value, $this->options); |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function unserialize($value) |
||||
{ |
||||
return Json::decode($value); |
||||
} |
||||
} |
@ -0,0 +1,35 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\serialize; |
||||
|
||||
use yii\base\BaseObject; |
||||
|
||||
/** |
||||
* PhpSerializer uses native PHP `serialize()` and `unserialize()` functions for the serialization. |
||||
* |
||||
* @author Paul Klimov <klimov.paul@gmail.com> |
||||
* @since 2.1.0 |
||||
*/ |
||||
class PhpSerializer extends BaseObject implements SerializerInterface |
||||
{ |
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function serialize($value) |
||||
{ |
||||
return serialize($value); |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function unserialize($value) |
||||
{ |
||||
return unserialize($value); |
||||
} |
||||
} |
@ -0,0 +1,31 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\serialize; |
||||
|
||||
/** |
||||
* SerializerInterface defines serializer interface. |
||||
* |
||||
* @author Paul Klimov <klimov.paul@gmail.com> |
||||
* @since 2.1.0 |
||||
*/ |
||||
interface SerializerInterface |
||||
{ |
||||
/** |
||||
* Serializes given value. |
||||
* @param mixed $value value to be serialized |
||||
* @return string serialized value. |
||||
*/ |
||||
public function serialize($value); |
||||
|
||||
/** |
||||
* Restores value from its serialized representations |
||||
* @param string $value serialized string. |
||||
* @return mixed restored value |
||||
*/ |
||||
public function unserialize($value); |
||||
} |
@ -0,0 +1,27 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yiiunit\framework\serialize; |
||||
|
||||
use yii\serialize\CallbackSerializer; |
||||
|
||||
/** |
||||
* @group serialize |
||||
*/ |
||||
class CallbackSerializerTest extends SerializerTest |
||||
{ |
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
protected function createSerializer() |
||||
{ |
||||
return new CallbackSerializer([ |
||||
'serialize' => 'serialize', |
||||
'unserialize' => 'unserialize', |
||||
]); |
||||
} |
||||
} |
@ -0,0 +1,37 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yiiunit\framework\serialize; |
||||
|
||||
use yii\serialize\IgbinarySerializer; |
||||
|
||||
/** |
||||
* @group serialize |
||||
*/ |
||||
class IgbinarySerializerTest extends SerializerTest |
||||
{ |
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
protected function setUp() |
||||
{ |
||||
if (!function_exists('igbinary_serialize')) { |
||||
$this->markTestSkipped('igbinary extension is required.'); |
||||
return; |
||||
} |
||||
|
||||
parent::setUp(); |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
protected function createSerializer() |
||||
{ |
||||
return new IgbinarySerializer(); |
||||
} |
||||
} |
@ -0,0 +1,24 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yiiunit\framework\serialize; |
||||
|
||||
use yii\serialize\JsonSerializer; |
||||
|
||||
/** |
||||
* @group serialize |
||||
*/ |
||||
class JsonSerializerTest extends SerializerTest |
||||
{ |
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
protected function createSerializer() |
||||
{ |
||||
return new JsonSerializer(); |
||||
} |
||||
} |
@ -0,0 +1,24 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yiiunit\framework\serialize; |
||||
|
||||
use yii\serialize\PhpSerializer; |
||||
|
||||
/** |
||||
* @group serialize |
||||
*/ |
||||
class PhpSerializerTest extends SerializerTest |
||||
{ |
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
protected function createSerializer() |
||||
{ |
||||
return new PhpSerializer(); |
||||
} |
||||
} |
@ -0,0 +1,51 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yiiunit\framework\serialize; |
||||
|
||||
use yiiunit\TestCase; |
||||
|
||||
/** |
||||
* @group serialize |
||||
*/ |
||||
abstract class SerializerTest extends TestCase |
||||
{ |
||||
/** |
||||
* Creates serializer instance for the tests. |
||||
* @return \yii\serialize\SerializerInterface |
||||
*/ |
||||
abstract protected function createSerializer(); |
||||
|
||||
/** |
||||
* Data provider for [[testSerialize()]] |
||||
* @return array test data. |
||||
*/ |
||||
public function dataProviderSerialize() |
||||
{ |
||||
return [ |
||||
['some-string'], |
||||
[345], |
||||
[56.89], |
||||
[['some' => 'array']], |
||||
]; |
||||
} |
||||
|
||||
/** |
||||
* @dataProvider dataProviderSerialize |
||||
* |
||||
* @param mixed $value |
||||
*/ |
||||
public function testSerialize($value) |
||||
{ |
||||
$serializer = $this->createSerializer(); |
||||
|
||||
$serialized = $serializer->serialize($value); |
||||
$this->assertTrue(is_string($serialized)); |
||||
|
||||
$this->assertEquals($value, $serializer->unserialize($serialized)); |
||||
} |
||||
} |
Loading…
Reference in new issue