Amf0.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_Amf_Parse_OutputStream */
  25. require_once 'Zend/Amf/Parse/OutputStream.php';
  26. /** @see Zend_Amf_Parse_Amf0_Serializer */
  27. require_once 'Zend/Amf/Parse/Amf0/Serializer.php';
  28. /** @see Zend_Amf_Parse_InputStream */
  29. require_once 'Zend/Amf/Parse/InputStream.php';
  30. /** @see Zend_Amf_Parse_Amf0_Deserializer */
  31. require_once 'Zend/Amf/Parse/Amf0/Deserializer.php';
  32. /**
  33. * @category Zend
  34. * @package Zend_Serializer
  35. * @subpackage Adapter
  36. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Serializer_Adapter_Amf0 extends Zend_Serializer_Adapter_AdapterAbstract
  40. {
  41. /**
  42. * Serialize a PHP value to AMF0 format
  43. *
  44. * @param mixed $value
  45. * @param array $opts
  46. * @return string
  47. * @throws Zend_Serializer_Exception
  48. */
  49. public function serialize($value, array $opts = array())
  50. {
  51. try {
  52. $stream = new Zend_Amf_Parse_OutputStream();
  53. $serializer = new Zend_Amf_Parse_Amf0_Serializer($stream);
  54. $serializer->writeTypeMarker($value);
  55. return $stream->getStream();
  56. } catch (Exception $e) {
  57. require_once 'Zend/Serializer/Exception.php';
  58. throw new Zend_Serializer_Exception('Serialization failed by previous error', 0, $e);
  59. }
  60. }
  61. /**
  62. * Unserialize an AMF0 value to PHP
  63. *
  64. * @param mixed $value
  65. * @param array $opts
  66. * @return void
  67. * @throws Zend_Serializer_Exception
  68. */
  69. public function unserialize($value, array $opts = array())
  70. {
  71. try {
  72. $stream = new Zend_Amf_Parse_InputStream($value);
  73. $deserializer = new Zend_Amf_Parse_Amf0_Deserializer($stream);
  74. return $deserializer->readTypeMarker();
  75. } catch (Exception $e) {
  76. require_once 'Zend/Serializer/Exception.php';
  77. throw new Zend_Serializer_Exception('Unserialization failed by previous error', 0, $e);
  78. }
  79. }
  80. }