Conditions.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Widget\Helper;
  7. use Magento\Framework\Data\Wysiwyg\Normalizer;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Serialize\Serializer\Json;
  10. /**
  11. * Widget Conditions helper.
  12. */
  13. class Conditions
  14. {
  15. /**
  16. * @var Json
  17. */
  18. private $serializer;
  19. /**
  20. * @var Normalizer
  21. */
  22. private $normalizer;
  23. /**
  24. * @param Json $serializer
  25. * @param Normalizer $normalizer
  26. */
  27. public function __construct(
  28. Json $serializer = null,
  29. Normalizer $normalizer = null
  30. ) {
  31. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
  32. $this->normalizer = $normalizer ?: ObjectManager::getInstance()->get(Normalizer::class);
  33. }
  34. /**
  35. * Encode widget conditions to be used with WYSIWIG.
  36. *
  37. * @param array $value
  38. * @return string
  39. */
  40. public function encode(array $value)
  41. {
  42. return $this->normalizer->replaceReservedCharacters($this->serializer->serialize($value));
  43. }
  44. /**
  45. * Decode previously encoded widget conditions.
  46. *
  47. * @param string $value
  48. * @return array
  49. */
  50. public function decode($value)
  51. {
  52. return $this->serializer->unserialize(
  53. $this->normalizer->restoreReservedCharacters($value)
  54. );
  55. }
  56. }