3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Serializer\Adapter
;
12 use Zend\Serializer\Exception
;
13 use Zend\Stdlib\ErrorHandler
;
15 class PhpSerialize
extends AbstractAdapter
18 * Serialized boolean false value
22 private static $serializedFalse = null;
27 public function __construct($options = null)
29 // needed to check if a returned false is based on a serialize false
30 // or based on failure (igbinary can overwrite [un]serialize functions)
31 if (static::$serializedFalse === null) {
32 static::$serializedFalse = serialize(false);
35 parent
::__construct($options);
39 * Serialize using serialize()
43 * @throws Exception\RuntimeException On serialize error
45 public function serialize($value)
47 ErrorHandler
::start();
48 $ret = serialize($value);
49 $err = ErrorHandler
::stop();
51 throw new Exception\
RuntimeException(
52 'Serialization failed', 0, $err
62 * @todo Allow integration with unserialize_callback_func
63 * @param string $serialized
65 * @throws Exception\RuntimeException on unserialize error
67 public function unserialize($serialized)
69 if (!is_string($serialized) ||
!preg_match('/^((s|i|d|b|a|O|C):|N;)/', $serialized)) {
73 // If we have a serialized boolean false value, just return false;
74 // prevents the unserialize handler from creating an error.
75 if ($serialized === static::$serializedFalse) {
79 ErrorHandler
::start(E_NOTICE
);
80 $ret = unserialize($serialized);
81 $err = ErrorHandler
::stop();
83 throw new Exception\
RuntimeException('Unserialization failed', 0, $err);