PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/site/library/Zend/Serializer/Adapter/Wddx.php

https://github.com/Piirka/PicLeaks
PHP | 118 lines | 49 code | 11 blank | 58 comment | 7 complexity | 798f85828749641e448e86d95d46e940 MD5 | raw file
Possible License(s): GPL-2.0
  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. /** @see Zend_Serializer_Adapter_AdapterAbstract */
  23. require_once 'Zend/Serializer/Adapter/AdapterAbstract.php';
  24. /**
  25. * @link http://www.infoloom.com/gcaconfs/WEB/chicago98/simeonov.HTM
  26. * @link http://en.wikipedia.org/wiki/WDDX
  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_Wddx extends Zend_Serializer_Adapter_AdapterAbstract
  34. {
  35. /**
  36. * @var array Default options
  37. */
  38. protected $_options = array(
  39. 'comment' => null,
  40. );
  41. /**
  42. * Constructor
  43. *
  44. * @param array $opts
  45. * @return void
  46. * @throws Zend_Serializer_Exception if wddx extension not found
  47. */
  48. public function __construct($opts = array())
  49. {
  50. if (!extension_loaded('wddx')) {
  51. require_once 'Zend/Serializer/Exception.php';
  52. throw new Zend_Serializer_Exception('PHP extension "wddx" is required for this adapter');
  53. }
  54. parent::__construct($opts);
  55. }
  56. /**
  57. * Serialize PHP to WDDX
  58. *
  59. * @param mixed $value
  60. * @param array $opts
  61. * @return string
  62. * @throws Zend_Serializer_Exception on wddx error
  63. */
  64. public function serialize($value, array $opts = array())
  65. {
  66. $opts = $opts + $this->_options;
  67. if (isset($opts['comment']) && $opts['comment']) {
  68. $wddx = wddx_serialize_value($value, (string)$opts['comment']);
  69. } else {
  70. $wddx = wddx_serialize_value($value);
  71. }
  72. if ($wddx === false) {
  73. $lastErr = error_get_last();
  74. require_once 'Zend/Serializer/Exception.php';
  75. throw new Zend_Serializer_Exception($lastErr['message']);
  76. }
  77. return $wddx;
  78. }
  79. /**
  80. * Unserialize from WDDX to PHP
  81. *
  82. * @param string $wddx
  83. * @param array $opts
  84. * @return mixed
  85. * @throws Zend_Serializer_Exception on wddx error
  86. */
  87. public function unserialize($wddx, array $opts = array())
  88. {
  89. $ret = wddx_deserialize($wddx);
  90. if ($ret === null) {
  91. // check if the returned NULL is valid
  92. // or based on an invalid wddx string
  93. try {
  94. $simpleXml = new SimpleXMLElement($wddx);
  95. if (isset($simpleXml->data[0]->null[0])) {
  96. return null; // valid null
  97. }
  98. $errMsg = 'Can\'t unserialize wddx string';
  99. } catch (Exception $e) {
  100. $errMsg = $e->getMessage();
  101. }
  102. require_once 'Zend/Serializer/Exception.php';
  103. throw new Zend_Serializer_Exception($errMsg);
  104. }
  105. return $ret;
  106. }
  107. }