Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Properties.php
Go to the documentation of this file.
1 <?php
9 
12 
13 class Properties extends \Magento\Framework\Validator\AbstractValidator
14 {
18  protected $_readOnlyProperties = [];
19 
26  public function setReadOnlyProperties(array $readOnlyProperties)
27  {
28  $this->_readOnlyProperties = $readOnlyProperties;
29  }
30 
41  public function isValid($value)
42  {
43  $this->_clearMessages();
44  if (!$value instanceof AbstractModel) {
45  throw new \InvalidArgumentException('Instance of \Magento\Framework\Model\AbstractModel is expected.');
46  }
47  if ($this->_readOnlyProperties) {
48  if (!$value->hasDataChanges()) {
49  return true;
50  }
51  foreach ($this->_readOnlyProperties as $property) {
52  if ($this->_hasChanges($value->getData($property), $value->getOrigData($property))) {
53  $this->_messages[__CLASS__] = [
54  (string)new \Magento\Framework\Phrase("Read-only property cannot be changed.")
55  ];
56  break;
57  }
58  }
59  }
60  return !count($this->_messages);
61  }
62 
70  protected function _hasChanges($firstValue, $secondValue)
71  {
72  if ($firstValue === $secondValue || $firstValue == $secondValue && is_numeric(
73  $firstValue
74  ) && is_numeric(
75  $secondValue
76  )
77  ) {
78  return false;
79  }
80  return true;
81  }
82 }
$value
Definition: gender.phtml:16
_hasChanges($firstValue, $secondValue)
Definition: Properties.php:70
setReadOnlyProperties(array $readOnlyProperties)
Definition: Properties.php:26