LayoutUpdateConverter.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Widget\Setup;
  7. use Magento\Framework\Serialize\Serializer\Json;
  8. use Magento\Framework\Serialize\Serializer\Serialize;
  9. use Magento\Framework\Data\Wysiwyg\Normalizer;
  10. use Magento\Framework\DB\DataConverter\DataConversionException;
  11. use Magento\Framework\DB\DataConverter\SerializedToJson;
  12. /**
  13. * Convert conditions_encoded part of layout update data from serialized to JSON format
  14. */
  15. class LayoutUpdateConverter extends SerializedToJson
  16. {
  17. /**
  18. * @var Normalizer
  19. */
  20. private $normalizer;
  21. /**
  22. * Constructor
  23. *
  24. * @param Serialize $serialize
  25. * @param Json $json
  26. * @param Normalizer $normalizer
  27. */
  28. public function __construct(
  29. Serialize $serialize,
  30. Json $json,
  31. Normalizer $normalizer
  32. ) {
  33. $this->normalizer = $normalizer;
  34. parent::__construct($serialize, $json);
  35. }
  36. /**
  37. * Convert conditions_encoded part of layout update data from serialized to JSON format
  38. *
  39. * @param string $value
  40. * @return string
  41. * @throws DataConversionException
  42. */
  43. public function convert($value)
  44. {
  45. preg_match_all(
  46. '/^(.*?conditions_encoded<\/argument><argument [^<]*>)(.*?)(<\/argument>.*?)$/si',
  47. $value,
  48. $matches,
  49. PREG_SET_ORDER
  50. );
  51. if (isset($matches[0])) {
  52. $matchSegments = $matches[0];
  53. $matchSegments[2] = parent::convert($matchSegments[2]);
  54. return $matchSegments[1] . $matchSegments[2] . $matchSegments[3];
  55. } else {
  56. return $value;
  57. }
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. protected function isValidJsonValue($value)
  63. {
  64. $value = $this->normalizer->restoreReservedCharacters($value);
  65. return parent::isValidJsonValue($value);
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. protected function unserializeValue($value)
  71. {
  72. $value = htmlspecialchars_decode($value);
  73. $value = $this->restoreReservedCharactersInSerializedContent($value);
  74. return parent::unserializeValue($value);
  75. }
  76. /**
  77. * @inheritdoc
  78. */
  79. protected function encodeJson($value)
  80. {
  81. return htmlspecialchars(
  82. $this->normalizer->replaceReservedCharacters(parent::encodeJson($value))
  83. );
  84. }
  85. /**
  86. * Restore the reserved characters in the existing serialized content
  87. *
  88. * @param string $serializedContent
  89. * @return string
  90. */
  91. private function restoreReservedCharactersInSerializedContent($serializedContent)
  92. {
  93. $map = [
  94. '{' => '[',
  95. '}' => ']',
  96. '"' => '`',
  97. '\\' => '|',
  98. ];
  99. return str_replace(
  100. array_values($map),
  101. array_keys($map),
  102. $serializedContent
  103. );
  104. }
  105. }