Json.php 916 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\Config;
  3. /**
  4. * Class for serializing data to json string and unserializing json string to data.
  5. *
  6. */
  7. class Json
  8. {
  9. /**
  10. * @var null|string
  11. */
  12. public $jsonError;
  13. /**
  14. * @param array|bool|float|int|null|string $data
  15. * @return string
  16. */
  17. public function serialize($data)
  18. {
  19. $json = json_encode($data);
  20. $this->jsonError = null;
  21. if (json_last_error_msg() != "No error") {
  22. $this->jsonLastError = json_last_error_msg();
  23. }
  24. return $json;
  25. }
  26. /**
  27. * @param string $string
  28. * @return mixed
  29. */
  30. public function unserialize($string)
  31. {
  32. $data = json_decode($string, true);
  33. $this->jsonError = null;
  34. if (json_last_error_msg() != "No error") {
  35. $this->jsonLastError = json_last_error_msg();
  36. }
  37. return $data;
  38. }
  39. }