web/lib/Zend/Serializer/Adapter/Igbinary.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Serializer
       
    17  * @subpackage Adapter
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Igbinary.php 20574 2010-01-24 17:39:14Z mabe $
       
    21  */
       
    22 
       
    23 /** @see Zend_Serializer_Adapter_AdapterAbstract */
       
    24 require_once 'Zend/Serializer/Adapter/AdapterAbstract.php';
       
    25 
       
    26 /**
       
    27  * @category   Zend
       
    28  * @package    Zend_Serializer
       
    29  * @subpackage Adapter
       
    30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    32  */
       
    33 class Zend_Serializer_Adapter_Igbinary extends Zend_Serializer_Adapter_AdapterAbstract
       
    34 {
       
    35     /**
       
    36      * @var string Serialized null value
       
    37      */
       
    38     private static $_serializedNull = null;
       
    39 
       
    40     /**
       
    41      * Constructor
       
    42      * 
       
    43      * @param  array|Zend_Config $opts 
       
    44      * @return void
       
    45      * @throws Zend_Serializer_Exception If igbinary extension is not present
       
    46      */
       
    47     public function __construct($opts = array()) 
       
    48     {
       
    49         if (!extension_loaded('igbinary')) {
       
    50             require_once 'Zend/Serializer/Exception.php';
       
    51             throw new Zend_Serializer_Exception('PHP extension "igbinary" is required for this adapter');
       
    52         }
       
    53 
       
    54         parent::__construct($opts);
       
    55 
       
    56         if (self::$_serializedNull === null) {
       
    57             self::$_serializedNull = igbinary_serialize(null);
       
    58         }
       
    59     }
       
    60 
       
    61     /**
       
    62      * Serialize PHP value to igbinary
       
    63      * 
       
    64      * @param  mixed $value 
       
    65      * @param  array $opts 
       
    66      * @return string
       
    67      * @throws Zend_Serializer_Exception on igbinary error
       
    68      */
       
    69     public function serialize($value, array $opts = array())
       
    70     {
       
    71         $ret = igbinary_serialize($value);
       
    72         if ($ret === false) {
       
    73             $lastErr = error_get_last();
       
    74             require_once 'Zend/Serializer/Exception.php';
       
    75             throw new Zend_Serializer_Exception($lastErr['message']);
       
    76         }
       
    77         return $ret;
       
    78     }
       
    79 
       
    80     /**
       
    81      * Deserialize igbinary string to PHP value
       
    82      * 
       
    83      * @param  string|binary $serialized 
       
    84      * @param  array $opts 
       
    85      * @return mixed
       
    86      * @throws Zend_Serializer_Exception on igbinary error
       
    87      */
       
    88     public function unserialize($serialized, array $opts = array())
       
    89     {
       
    90         $ret = igbinary_unserialize($serialized);
       
    91         if ($ret === null && $serialized !== self::$_serializedNull) {
       
    92             $lastErr = error_get_last();
       
    93             require_once 'Zend/Serializer/Exception.php';
       
    94             throw new Zend_Serializer_Exception($lastErr['message']);
       
    95         }
       
    96         return $ret;
       
    97     }
       
    98 }