SerializedDataConverter.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Setup;
  7. use Magento\Framework\DB\DataConverter\DataConversionException;
  8. use Magento\Framework\DB\DataConverter\SerializedToJson;
  9. /**
  10. * Serializer used to update nested serialized data in product_options field.
  11. */
  12. class SerializedDataConverter extends SerializedToJson
  13. {
  14. /**
  15. * Convert from serialized to JSON format.
  16. *
  17. * @param string $value
  18. * @return string
  19. *
  20. * @throws DataConversionException
  21. */
  22. public function convert($value)
  23. {
  24. if ($this->isValidJsonValue($value)) {
  25. return $value;
  26. }
  27. $valueUnserialized = $this->unserializeValue($value);
  28. if (isset($valueUnserialized['options'])) {
  29. foreach ($valueUnserialized['options'] as $key => $option) {
  30. if ($option['option_type'] === 'file') {
  31. $valueUnserialized['options'][$key]['option_value'] = parent::convert($option['option_value']);
  32. }
  33. }
  34. }
  35. if (isset($valueUnserialized['bundle_selection_attributes'])) {
  36. $valueUnserialized['bundle_selection_attributes'] = parent::convert(
  37. $valueUnserialized['bundle_selection_attributes']
  38. );
  39. }
  40. return $this->encodeJson($valueUnserialized);
  41. }
  42. }