web/enmi/Zend/Serializer/Adapter/Wddx.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     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: Wddx.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  * @link       http://www.infoloom.com/gcaconfs/WEB/chicago98/simeonov.HTM
       
    28  * @link       http://en.wikipedia.org/wiki/WDDX
       
    29  * @category   Zend
       
    30  * @package    Zend_Serializer
       
    31  * @subpackage Adapter
       
    32  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    34  */
       
    35 class Zend_Serializer_Adapter_Wddx extends Zend_Serializer_Adapter_AdapterAbstract
       
    36 {
       
    37     /**
       
    38      * @var array Default options
       
    39      */
       
    40     protected $_options = array(
       
    41         'comment' => null,
       
    42     );
       
    43 
       
    44     /**
       
    45      * Constructor
       
    46      * 
       
    47      * @param  array $opts 
       
    48      * @return void
       
    49      * @throws Zend_Serializer_Exception if wddx extension not found
       
    50      */
       
    51     public function __construct($opts = array())
       
    52     {
       
    53         if (!extension_loaded('wddx')) {
       
    54             require_once 'Zend/Serializer/Exception.php';
       
    55             throw new Zend_Serializer_Exception('PHP extension "wddx" is required for this adapter');
       
    56         }
       
    57 
       
    58         parent::__construct($opts);
       
    59     }
       
    60 
       
    61     /**
       
    62      * Serialize PHP to WDDX
       
    63      * 
       
    64      * @param  mixed $value 
       
    65      * @param  array $opts 
       
    66      * @return string
       
    67      * @throws Zend_Serializer_Exception on wddx error
       
    68      */
       
    69     public function serialize($value, array $opts = array())
       
    70     {
       
    71         $opts = $opts + $this->_options;
       
    72 
       
    73         if (isset($opts['comment']) && $opts['comment']) {
       
    74             $wddx = wddx_serialize_value($value, (string)$opts['comment']);
       
    75         } else {
       
    76             $wddx = wddx_serialize_value($value);
       
    77         }
       
    78 
       
    79         if ($wddx === false) {
       
    80             $lastErr = error_get_last();
       
    81             require_once 'Zend/Serializer/Exception.php';
       
    82             throw new Zend_Serializer_Exception($lastErr['message']);
       
    83         }
       
    84         return $wddx;
       
    85     }
       
    86 
       
    87     /**
       
    88      * Unserialize from WDDX to PHP
       
    89      * 
       
    90      * @param  string $wddx 
       
    91      * @param  array $opts 
       
    92      * @return mixed
       
    93      * @throws Zend_Serializer_Exception on wddx error
       
    94      */
       
    95     public function unserialize($wddx, array $opts = array())
       
    96     {
       
    97         $ret = wddx_deserialize($wddx);
       
    98 
       
    99         if ($ret === null) {
       
   100             // check if the returned NULL is valid
       
   101             // or based on an invalid wddx string
       
   102             try {
       
   103                 $simpleXml = new SimpleXMLElement($wddx);
       
   104                 if (isset($simpleXml->data[0]->null[0])) {
       
   105                     return null; // valid null
       
   106                 }
       
   107                 $errMsg = 'Can\'t unserialize wddx string';
       
   108             } catch (Exception $e) {
       
   109                 $errMsg = $e->getMessage();
       
   110             }
       
   111 
       
   112             require_once 'Zend/Serializer/Exception.php';
       
   113             throw new Zend_Serializer_Exception($errMsg);
       
   114         }
       
   115 
       
   116         return $ret;
       
   117     }
       
   118 }