Flag.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework;
  7. /**
  8. * Flag model
  9. *
  10. * @method string getFlagCode()
  11. * @method \Magento\Framework\Flag setFlagCode(string $value)
  12. * @method int getState()
  13. * @method \Magento\Framework\Flag setState(int $value)
  14. * @method string getLastUpdate()
  15. * @method \Magento\Framework\Flag setLastUpdate(string $value)
  16. */
  17. class Flag extends Model\AbstractModel
  18. {
  19. /**
  20. * Flag code
  21. *
  22. * @var string
  23. */
  24. protected $_flagCode = null;
  25. /**
  26. * Serializer for encode/decode string/data.
  27. *
  28. * @var \Magento\Framework\Serialize\Serializer\Json
  29. */
  30. private $json;
  31. /**
  32. * Serializer for encode/decode string/data.
  33. *
  34. * @var \Magento\Framework\Serialize\Serializer\Serialize
  35. */
  36. private $serialize;
  37. /**
  38. * @param \Magento\Framework\Model\Context $context
  39. * @param \Magento\Framework\Registry $registry
  40. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  41. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  42. * @param array $data
  43. * @param \Magento\Framework\Serialize\Serializer\Json $json
  44. * @param \Magento\Framework\Serialize\Serializer\Serialize $serialize
  45. */
  46. public function __construct(
  47. \Magento\Framework\Model\Context $context,
  48. \Magento\Framework\Registry $registry,
  49. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  50. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  51. array $data = [],
  52. \Magento\Framework\Serialize\Serializer\Json $json = null,
  53. \Magento\Framework\Serialize\Serializer\Serialize $serialize = null
  54. ) {
  55. $this->json = $json ?: \Magento\Framework\App\ObjectManager::getInstance()
  56. ->get(\Magento\Framework\Serialize\Serializer\Json::class);
  57. $this->serialize = $serialize ?: \Magento\Framework\App\ObjectManager::getInstance()
  58. ->get(\Magento\Framework\Serialize\Serializer\Serialize::class);
  59. parent::__construct(
  60. $context,
  61. $registry,
  62. $resource,
  63. $resourceCollection,
  64. $data
  65. );
  66. }
  67. /**
  68. * Init resource model
  69. * Set flag_code if it is specified in arguments
  70. *
  71. * @return void
  72. */
  73. protected function _construct()
  74. {
  75. if ($this->hasData('flag_code')) {
  76. $this->_flagCode = $this->getData('flag_code');
  77. }
  78. $this->_init(\Magento\Framework\Flag\FlagResource::class);
  79. }
  80. /**
  81. * Processing object before save data
  82. *
  83. * @throws \Magento\Framework\Exception\LocalizedException
  84. * @return $this
  85. */
  86. public function beforeSave()
  87. {
  88. if ($this->_flagCode === null) {
  89. throw new Exception\LocalizedException(new \Magento\Framework\Phrase('Please define flag code.'));
  90. }
  91. $this->setFlagCode($this->_flagCode);
  92. if (!$this->hasKeepUpdateDate()) {
  93. $this->setLastUpdate(date('Y-m-d H:i:s'));
  94. }
  95. return parent::beforeSave();
  96. }
  97. /**
  98. * Retrieve flag data
  99. *
  100. * @return mixed
  101. */
  102. public function getFlagData()
  103. {
  104. if ($this->hasFlagData()) {
  105. $flagData = $this->getData('flag_data');
  106. try {
  107. $data = $this->json->unserialize($flagData);
  108. } catch (\InvalidArgumentException $exception) {
  109. $data = $this->serialize->unserialize($flagData);
  110. }
  111. return $data;
  112. }
  113. }
  114. /**
  115. * Set flag data
  116. *
  117. * @param mixed $value
  118. * @return $this
  119. */
  120. public function setFlagData($value)
  121. {
  122. return $this->setData('flag_data', $this->json->serialize($value));
  123. }
  124. /**
  125. * load self (load by flag code)
  126. *
  127. * @throws \Magento\Framework\Exception\LocalizedException
  128. * @return $this
  129. */
  130. public function loadSelf()
  131. {
  132. if ($this->_flagCode === null) {
  133. throw new Exception\LocalizedException(new \Magento\Framework\Phrase('Please define flag code.'));
  134. }
  135. return $this->load($this->_flagCode, 'flag_code');
  136. }
  137. }