Serialized.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Model\Config\Backend;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Serialize\Serializer\Json;
  9. /**
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Serialized extends \Magento\Framework\App\Config\Value
  14. {
  15. /**
  16. * @var Json
  17. */
  18. private $serializer;
  19. /**
  20. * Serialized constructor
  21. *
  22. * @param \Magento\Framework\Model\Context $context
  23. * @param \Magento\Framework\Registry $registry
  24. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  25. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  26. * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
  27. * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
  28. * @param array $data
  29. * @param Json|null $serializer
  30. */
  31. public function __construct(
  32. \Magento\Framework\Model\Context $context,
  33. \Magento\Framework\Registry $registry,
  34. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  35. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  36. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  37. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  38. array $data = [],
  39. Json $serializer = null
  40. ) {
  41. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
  42. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  43. }
  44. /**
  45. * @return void
  46. */
  47. protected function _afterLoad()
  48. {
  49. $value = $this->getValue();
  50. if (!is_array($value)) {
  51. $this->setValue(empty($value) ? false : $this->serializer->unserialize($value));
  52. }
  53. }
  54. /**
  55. * @return $this
  56. */
  57. public function beforeSave()
  58. {
  59. if (is_array($this->getValue())) {
  60. $this->setValue($this->serializer->serialize($this->getValue()));
  61. }
  62. parent::beforeSave();
  63. return $this;
  64. }
  65. }