Serialize.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Serialize\Serializer;
  7. use Magento\Framework\Serialize\SerializerInterface;
  8. /**
  9. * Less secure than Json implementation, but gives higher performance on big arrays. Does not unserialize objects.
  10. * Using this implementation is discouraged as it may lead to security vulnerabilities
  11. */
  12. class Serialize implements SerializerInterface
  13. {
  14. /**
  15. * {@inheritDoc}
  16. */
  17. public function serialize($data)
  18. {
  19. if (is_resource($data)) {
  20. throw new \InvalidArgumentException('Unable to serialize value.');
  21. }
  22. return serialize($data);
  23. }
  24. /**
  25. * {@inheritDoc}
  26. */
  27. public function unserialize($string)
  28. {
  29. if (false === $string || null === $string || '' === $string) {
  30. throw new \InvalidArgumentException('Unable to unserialize value.');
  31. }
  32. set_error_handler(
  33. function () {
  34. restore_error_handler();
  35. throw new \InvalidArgumentException('Unable to unserialize value, string is corrupted.');
  36. },
  37. E_NOTICE
  38. );
  39. $result = unserialize($string, ['allowed_classes' => false]);
  40. restore_error_handler();
  41. return $result;
  42. }
  43. }