/Zend/Serializer/Adapter/Wddx.php

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 124 lines · 51 code · 12 blank · 61 comment · 7 complexity · fa51a67ac2ca04ac6c920e5fd03aac59 MD5 · raw file

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