Skip to content

Commit 7439d1d

Browse files
author
Joan He
committed
MAGETWO-59764: Create serialize class in framework
1 parent 61eda85 commit 7439d1d

File tree

3 files changed

+121
-2
lines changed

3 files changed

+121
-2
lines changed
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Serialize
22

3-
**Serialize** libaray provides *SerializerInterface* and multiple implementations of serializer to support different kinds of needs of serializing/unserializing of data. Here are list of serializers in this library:
3+
**Serialize** library provides interface *SerializerInterface* and multiple implementations:
44

5-
* **Json** (default) - It can be used to serialize string, integer, float, boolean, or array data to json string; it unserializes json string to string, integer, float, boolean, or array. This is the recommended serializer.
5+
* *Json* - default implementation. Uses PHP native json_encode/json_decode functions;
6+
* *Serialize* - less secure than *Json*, but gives higher performance on big arrays. Uses PHP native serialize/unserialize functions, does not unserialize objects on PHP 7.
7+
8+
Using *Serialize* implementation directly is discouraged, always use *SerializerInterface*, using *Serialize* implementation may lead to security vulnerabilities.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Serialize\Serializer;
7+
8+
use Magento\Framework\Serialize\SerializerInterface;
9+
10+
/**
11+
* Less secure than Json implementation, but gives higher performance on big arrays. Does not unserialize objects on
12+
* PHP 7. Using this implementation directly is discouraged as it may lead to security vulnerabilities, especially on
13+
* older versions of PHP
14+
*/
15+
class Serialize implements SerializerInterface
16+
{
17+
/**
18+
* {@inheritDoc}
19+
*/
20+
public function serialize($data)
21+
{
22+
return serialize($data);
23+
}
24+
25+
/**
26+
* {@inheritDoc}
27+
*/
28+
public function unserialize($string)
29+
{
30+
if ($this->getPhpVersion() >= 7) {
31+
return unserialize($string, ['allowed_classes' => false]);
32+
}
33+
return unserialize($string);
34+
}
35+
36+
/**
37+
* Return major PHP version
38+
*
39+
* @return int
40+
*/
41+
private function getPhpVersion()
42+
{
43+
return PHP_MAJOR_VERSION;
44+
}
45+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Serialize\Test\Unit\Serializer;
7+
8+
use Magento\Framework\Serialize\Serializer\Serialize;
9+
use Magento\Framework\Serialize\Signer;
10+
use Psr\Log\LoggerInterface;
11+
use Magento\Framework\Serialize\InvalidSignatureException;
12+
13+
class SerializeTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* @var Serialize
17+
*/
18+
private $serialize;
19+
20+
protected function setUp()
21+
{
22+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
23+
$this->serialize = $objectManager->getObject(Serialize::class);
24+
}
25+
26+
/**
27+
* @param string|int|float|bool|array|null $value
28+
* @param string $serializedValue
29+
* @dataProvider serializeDataProvider
30+
*/
31+
public function testSerialize($value, $serializedValue)
32+
{
33+
$this->assertEquals($serializedValue, $this->serialize->serialize($value));
34+
}
35+
36+
public function serializeDataProvider()
37+
{
38+
return [
39+
['string', 's:6:"string";'],
40+
['', 's:0:"";'],
41+
[10, 'i:10;'],
42+
[10.5, 'd:10.5;'],
43+
[null, 'N;'],
44+
[false, 'b:0;'],
45+
[['foo' => 'bar'], 'a:1:{s:3:"foo";s:3:"bar";}'],
46+
];
47+
}
48+
49+
/**
50+
* @param string $serializedValue
51+
* @param string|int|float|bool|array|null $value
52+
* @dataProvider unserializeDataProvider
53+
*/
54+
public function testUnserialize($serializedValue, $value)
55+
{
56+
$this->assertEquals($value, $this->serialize->unserialize($serializedValue));
57+
}
58+
59+
public function unserializeDataProvider()
60+
{
61+
return [
62+
['s:6:"string";', 'string'],
63+
['s:0:"";', ''],
64+
['i:10;', 10],
65+
['d:10.5;', 10.5],
66+
['N;', null],
67+
['b:0;', false],
68+
['a:1:{s:3:"foo";s:3:"bar";}', ['foo' => 'bar']],
69+
];
70+
}
71+
}

0 commit comments

Comments
 (0)