Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Flag.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Framework;
7 
18 class Flag extends Model\AbstractModel
19 {
25  protected $_flagCode = null;
26 
32  private $json;
33 
39  private $serialize;
40 
50  public function __construct(
51  \Magento\Framework\Model\Context $context,
52  \Magento\Framework\Registry $registry,
53  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
54  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
55  array $data = [],
56  \Magento\Framework\Serialize\Serializer\Json $json = null,
57  \Magento\Framework\Serialize\Serializer\Serialize $serialize = null
58  ) {
60  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
62  ->get(\Magento\Framework\Serialize\Serializer\Serialize::class);
63  parent::__construct(
64  $context,
65  $registry,
66  $resource,
67  $resourceCollection,
68  $data
69  );
70  }
71 
78  protected function _construct()
79  {
80  if ($this->hasData('flag_code')) {
81  $this->_flagCode = $this->getData('flag_code');
82  }
83  $this->_init(\Magento\Framework\Flag\FlagResource::class);
84  }
85 
92  public function beforeSave()
93  {
94  if ($this->_flagCode === null) {
95  throw new Exception\LocalizedException(new \Magento\Framework\Phrase('Please define flag code.'));
96  }
97 
98  $this->setFlagCode($this->_flagCode);
99  if (!$this->hasKeepUpdateDate()) {
100  $this->setLastUpdate(date('Y-m-d H:i:s'));
101  }
102 
103  return parent::beforeSave();
104  }
105 
111  public function getFlagData()
112  {
113  if ($this->hasFlagData()) {
114  $flagData = $this->getData('flag_data');
115  try {
116  $data = $this->json->unserialize($flagData);
117  } catch (\InvalidArgumentException $exception) {
118  $data = $this->serialize->unserialize($flagData);
119  }
120  return $data;
121  }
122  }
123 
130  public function setFlagData($value)
131  {
132  return $this->setData('flag_data', $this->json->serialize($value));
133  }
134 
141  public function loadSelf()
142  {
143  if ($this->_flagCode === null) {
144  throw new Exception\LocalizedException(new \Magento\Framework\Phrase('Please define flag code.'));
145  }
146 
147  return $this->load($this->_flagCode, 'flag_code');
148  }
149 }
getData($key='', $index=null)
Definition: DataObject.php:119
setFlagData($value)
Definition: Flag.php:130
$resource
Definition: bulk.php:12
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[], \Magento\Framework\Serialize\Serializer\Json $json=null, \Magento\Framework\Serialize\Serializer\Serialize $serialize=null)
Definition: Flag.php:50
$value
Definition: gender.phtml:16
serialize($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
Definition: DataObject.php:446