File indexing completed on 2024-12-22 05:37:01
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 /** @see Zend_Xml_Security */ 0027 // require_once 'Zend/Xml/Security.php'; 0028 0029 /** @see Zend_Xml_Exception */ 0030 // require_once 'Zend/Xml/Exception.php'; 0031 0032 /** 0033 * @link http://www.infoloom.com/gcaconfs/WEB/chicago98/simeonov.HTM 0034 * @link http://en.wikipedia.org/wiki/WDDX 0035 * @category Zend 0036 * @package Zend_Serializer 0037 * @subpackage Adapter 0038 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0039 * @license http://framework.zend.com/license/new-bsd New BSD License 0040 */ 0041 class Zend_Serializer_Adapter_Wddx extends Zend_Serializer_Adapter_AdapterAbstract 0042 { 0043 /** 0044 * @var array Default options 0045 */ 0046 protected $_options = array( 0047 'comment' => null, 0048 ); 0049 0050 /** 0051 * Constructor 0052 * 0053 * @param array $opts 0054 * @return void 0055 * @throws Zend_Serializer_Exception if wddx extension not found 0056 */ 0057 public function __construct($opts = array()) 0058 { 0059 if (!extension_loaded('wddx')) { 0060 // require_once 'Zend/Serializer/Exception.php'; 0061 throw new Zend_Serializer_Exception('PHP extension "wddx" is required for this adapter'); 0062 } 0063 0064 parent::__construct($opts); 0065 } 0066 0067 /** 0068 * Serialize PHP to WDDX 0069 * 0070 * @param mixed $value 0071 * @param array $opts 0072 * @return string 0073 * @throws Zend_Serializer_Exception on wddx error 0074 */ 0075 public function serialize($value, array $opts = array()) 0076 { 0077 $opts = $opts + $this->_options; 0078 0079 if (isset($opts['comment']) && $opts['comment']) { 0080 $wddx = wddx_serialize_value($value, (string)$opts['comment']); 0081 } else { 0082 $wddx = wddx_serialize_value($value); 0083 } 0084 0085 if ($wddx === false) { 0086 $lastErr = error_get_last(); 0087 // require_once 'Zend/Serializer/Exception.php'; 0088 throw new Zend_Serializer_Exception($lastErr['message']); 0089 } 0090 return $wddx; 0091 } 0092 0093 /** 0094 * Unserialize from WDDX to PHP 0095 * 0096 * @param string $wddx 0097 * @param array $opts 0098 * @return mixed 0099 * @throws Zend_Serializer_Exception on wddx error 0100 */ 0101 public function unserialize($wddx, array $opts = array()) 0102 { 0103 $ret = wddx_deserialize($wddx); 0104 0105 if ($ret === null) { 0106 // check if the returned NULL is valid 0107 // or based on an invalid wddx string 0108 try { 0109 $simpleXml = Zend_Xml_Security::scan($wddx); 0110 if (isset($simpleXml->data[0]->null[0])) { 0111 return null; // valid null 0112 } 0113 $errMsg = 'Can\'t unserialize wddx string'; 0114 } catch (Zend_Xml_Exception $e) { 0115 $errMsg = $e->getMessage(); 0116 } 0117 0118 // require_once 'Zend/Serializer/Exception.php'; 0119 throw new Zend_Serializer_Exception($errMsg); 0120 } 0121 0122 return $ret; 0123 } 0124 }