1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the webmozart/key-value-store package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Webmozart\KeyValueStore\Util; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use Webmozart\KeyValueStore\Api\SerializationFailedException; |
16
|
|
|
use Webmozart\KeyValueStore\Api\UnserializationFailedException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Wrapper for `serialize()`/`unserialize()` that throws proper exceptions. |
20
|
|
|
* |
21
|
|
|
* @since 1.0 |
22
|
|
|
* |
23
|
|
|
* @author Bernhard Schussek <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class Serializer |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Serializes a value. |
29
|
|
|
* |
30
|
|
|
* @param mixed $value The value to serialize. |
31
|
|
|
* |
32
|
|
|
* @return string The serialized value. |
33
|
|
|
* |
34
|
|
|
* @throws SerializationFailedException If the value cannot be serialized. |
35
|
|
|
*/ |
36
|
456 |
|
public static function serialize($value) |
37
|
|
|
{ |
38
|
456 |
|
if (is_resource($value)) { |
39
|
8 |
|
throw SerializationFailedException::forValue($value); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
try { |
43
|
448 |
|
$serialized = serialize($value); |
44
|
8 |
|
} catch (Exception $e) { |
45
|
8 |
|
throw SerializationFailedException::forValue($value, $e->getMessage(), $e->getCode(), $e); |
46
|
|
|
} |
47
|
|
|
|
48
|
440 |
|
return $serialized; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Unserializes a value. |
53
|
|
|
* |
54
|
|
|
* @param mixed $serialized The serialized value. |
55
|
|
|
* |
56
|
|
|
* @return string The unserialized value. |
57
|
|
|
* |
58
|
|
|
* @throws UnserializationFailedException If the value cannot be unserialized. |
59
|
|
|
*/ |
60
|
304 |
|
public static function unserialize($serialized) |
61
|
|
|
{ |
62
|
304 |
|
if (!is_string($serialized)) { |
63
|
1 |
|
throw UnserializationFailedException::forValue($serialized); |
64
|
|
|
} |
65
|
|
|
|
66
|
303 |
|
$errorMessage = null; |
67
|
303 |
|
$errorCode = 0; |
68
|
|
|
|
69
|
303 |
|
set_error_handler(function ($errno, $errstr) use (&$errorMessage, &$errorCode) { |
70
|
37 |
|
$errorMessage = $errstr; |
71
|
37 |
|
$errorCode = $errno; |
72
|
303 |
|
}); |
73
|
|
|
|
74
|
303 |
|
$value = unserialize($serialized); |
75
|
|
|
|
76
|
303 |
|
restore_error_handler(); |
77
|
|
|
|
78
|
303 |
|
if (null !== $errorMessage) { |
79
|
37 |
|
if (false !== $pos = strpos($errorMessage, '): ')) { |
80
|
|
|
// cut "unserialize(%path%):" to make message more readable |
81
|
37 |
|
$errorMessage = substr($errorMessage, $pos + 3); |
82
|
|
|
} |
83
|
|
|
|
84
|
37 |
|
throw UnserializationFailedException::forValue($serialized, $errorMessage, $errorCode); |
85
|
|
|
} |
86
|
|
|
|
87
|
266 |
|
return $value; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function __construct() |
91
|
|
|
{ |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|