File indexing completed on 2024-12-22 05:37:00

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Serializer
0017  * @subpackage Adapter
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /** @see Zend_Serializer_Adapter_AdapterAbstract */
0024 // require_once 'Zend/Serializer/Adapter/AdapterAbstract.php';
0025 
0026 /**
0027  * @category   Zend
0028  * @package    Zend_Serializer
0029  * @subpackage Adapter
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Serializer_Adapter_Igbinary extends Zend_Serializer_Adapter_AdapterAbstract
0034 {
0035     /**
0036      * @var string Serialized null value
0037      */
0038     private static $_serializedNull = null;
0039 
0040     /**
0041      * Constructor
0042      *
0043      * @param  array|Zend_Config $opts
0044      * @return void
0045      * @throws Zend_Serializer_Exception If igbinary extension is not present
0046      */
0047     public function __construct($opts = array())
0048     {
0049         if (!extension_loaded('igbinary')) {
0050             // require_once 'Zend/Serializer/Exception.php';
0051             throw new Zend_Serializer_Exception('PHP extension "igbinary" is required for this adapter');
0052         }
0053 
0054         parent::__construct($opts);
0055 
0056         if (self::$_serializedNull === null) {
0057             self::$_serializedNull = igbinary_serialize(null);
0058         }
0059     }
0060 
0061     /**
0062      * Serialize PHP value to igbinary
0063      *
0064      * @param  mixed $value
0065      * @param  array $opts
0066      * @return string
0067      * @throws Zend_Serializer_Exception on igbinary error
0068      */
0069     public function serialize($value, array $opts = array())
0070     {
0071         $ret = igbinary_serialize($value);
0072         if ($ret === false) {
0073             $lastErr = error_get_last();
0074             // require_once 'Zend/Serializer/Exception.php';
0075             throw new Zend_Serializer_Exception($lastErr['message']);
0076         }
0077         return $ret;
0078     }
0079 
0080     /**
0081      * Deserialize igbinary string to PHP value
0082      *
0083      * @param  string|binary $serialized
0084      * @param  array $opts
0085      * @return mixed
0086      * @throws Zend_Serializer_Exception on igbinary error
0087      */
0088     public function unserialize($serialized, array $opts = array())
0089     {
0090         $ret = igbinary_unserialize($serialized);
0091         if ($ret === null && $serialized !== self::$_serializedNull) {
0092             $lastErr = error_get_last();
0093             // require_once 'Zend/Serializer/Exception.php';
0094             throw new Zend_Serializer_Exception($lastErr['message']);
0095         }
0096         return $ret;
0097     }
0098 }