Serialized.php 2.0 KB

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