Wddx.php 3.6 KB

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