1 | <?php |
||
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 | 249 | public static function serialize($value) |
|
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 | 170 | public static function unserialize($serialized) |
|
61 | { |
||
62 | 170 | if (!is_string($serialized)) { |
|
63 | 1 | throw UnserializationFailedException::forValue($serialized); |
|
64 | } |
||
65 | |||
66 | 169 | $errorMessage = null; |
|
67 | 169 | $errorCode = 0; |
|
68 | |||
69 | 169 | set_error_handler(function ($errno, $errstr) use (&$errorMessage, &$errorCode) { |
|
70 | 21 | $errorMessage = $errstr; |
|
71 | 21 | $errorCode = $errno; |
|
72 | 169 | }); |
|
73 | |||
74 | 169 | $value = unserialize($serialized); |
|
75 | |||
76 | 169 | restore_error_handler(); |
|
77 | |||
78 | 169 | if (null !== $errorMessage) { |
|
79 | 21 | if (false !== $pos = strpos($errorMessage, '): ')) { |
|
80 | // cut "unserialize(%path%):" to make message more readable |
||
81 | 21 | $errorMessage = substr($errorMessage, $pos + 3); |
|
82 | } |
||
83 | |||
84 | 21 | throw UnserializationFailedException::forValue($serialized, $errorMessage, $errorCode); |
|
85 | } |
||
86 | |||
87 | 148 | return $value; |
|
88 | } |
||
89 | |||
90 | private function __construct() |
||
93 | } |
||
94 |