Igbinary.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /**
  25. * @category Zend
  26. * @package Zend_Serializer
  27. * @subpackage Adapter
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Serializer_Adapter_Igbinary extends Zend_Serializer_Adapter_AdapterAbstract
  32. {
  33. /**
  34. * @var string Serialized null value
  35. */
  36. private static $_serializedNull = null;
  37. /**
  38. * Constructor
  39. *
  40. * @param array|Zend_Config $opts
  41. * @return void
  42. * @throws Zend_Serializer_Exception If igbinary extension is not present
  43. */
  44. public function __construct($opts = array())
  45. {
  46. if (!extension_loaded('igbinary')) {
  47. require_once 'Zend/Serializer/Exception.php';
  48. throw new Zend_Serializer_Exception('PHP extension "igbinary" is required for this adapter');
  49. }
  50. parent::__construct($opts);
  51. if (self::$_serializedNull === null) {
  52. self::$_serializedNull = igbinary_serialize(null);
  53. }
  54. }
  55. /**
  56. * Serialize PHP value to igbinary
  57. *
  58. * @param mixed $value
  59. * @param array $opts
  60. * @return string
  61. * @throws Zend_Serializer_Exception on igbinary error
  62. */
  63. public function serialize($value, array $opts = array())
  64. {
  65. $ret = igbinary_serialize($value);
  66. if ($ret === false) {
  67. $lastErr = error_get_last();
  68. require_once 'Zend/Serializer/Exception.php';
  69. throw new Zend_Serializer_Exception($lastErr['message']);
  70. }
  71. return $ret;
  72. }
  73. /**
  74. * Deserialize igbinary string to PHP value
  75. *
  76. * @param string|binary $serialized
  77. * @param array $opts
  78. * @return mixed
  79. * @throws Zend_Serializer_Exception on igbinary error
  80. */
  81. public function unserialize($serialized, array $opts = array())
  82. {
  83. $ret = igbinary_unserialize($serialized);
  84. if ($ret === null && $serialized !== self::$_serializedNull) {
  85. $lastErr = error_get_last();
  86. require_once 'Zend/Serializer/Exception.php';
  87. throw new Zend_Serializer_Exception($lastErr['message']);
  88. }
  89. return $ret;
  90. }
  91. }