Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Data.php
Go to the documentation of this file.
1 <?php
13 
15 
16 class Data extends \Magento\Framework\Validator\AbstractValidator
17 {
21  protected $_attributes = [];
22 
26  protected $_attributesWhiteList = [];
27 
31  protected $_attributesBlackList = [];
32 
36  protected $_data = [];
37 
41  protected $_attrDataFactory;
42 
46  public function __construct(\Magento\Eav\Model\AttributeDataFactory $attrDataFactory)
47  {
48  $this->_attrDataFactory = $attrDataFactory;
49  }
50 
57  public function setAttributes(array $attributes)
58  {
59  $this->_attributes = $attributes;
60  return $this;
61  }
62 
71  public function setAttributesWhiteList(array $attributesCodes)
72  {
73  $this->_attributesWhiteList = $attributesCodes;
74  return $this;
75  }
76 
85  public function setAttributesBlackList(array $attributesCodes)
86  {
87  $this->_attributesBlackList = $attributesCodes;
88  return $this;
89  }
90 
97  public function setData(array $data)
98  {
99  $this->_data = $data;
100  return $this;
101  }
102 
109  public function isValid($entity)
110  {
112  $attributes = $this->_getAttributes($entity);
113 
114  $data = [];
115  if ($this->_data) {
117  } elseif ($entity instanceof \Magento\Framework\DataObject) {
118  $data = $entity->getData();
119  }
120 
121  foreach ($attributes as $attribute) {
122  $attributeCode = $attribute->getAttributeCode();
123  if (!$attribute->getDataModel() && !$attribute->getFrontendInput()) {
124  continue;
125  }
126  $dataModel = $this->_attrDataFactory->create($attribute, $entity);
127  $dataModel->setExtractedData($data);
128  if (!isset($data[$attributeCode])) {
129  $data[$attributeCode] = null;
130  }
131  $result = $dataModel->validateValue($data[$attributeCode]);
132  if (true !== $result) {
133  $this->_addErrorMessages($attributeCode, (array)$result);
134  }
135  }
136  return count($this->_messages) == 0;
137  }
138 
148  protected function _getAttributes($entity)
149  {
151  $attributes = [];
152 
153  if ($this->_attributes) {
155  } elseif ($entity instanceof \Magento\Framework\Model\AbstractModel &&
156  $entity->getResource() instanceof \Magento\Eav\Model\Entity\AbstractEntity
157  ) { // $entity is EAV-model
159  $entityType = $entity->getEntityType();
160  $attributes = $entityType->getAttributeCollection()->getItems();
161  }
162 
163  $attributesByCode = [];
164  $attributesCodes = [];
165  foreach ($attributes as $attribute) {
166  if (!$attribute->getIsVisible()) {
167  continue;
168  }
169  $attributeCode = $attribute->getAttributeCode();
170  $attributesByCode[$attributeCode] = $attribute;
171  $attributesCodes[] = $attributeCode;
172  }
173 
174  $ignoreAttributes = $this->_attributesBlackList;
175  if ($this->_attributesWhiteList) {
176  $ignoreAttributes = array_merge(
177  $ignoreAttributes,
178  array_diff($attributesCodes, $this->_attributesWhiteList)
179  );
180  }
181 
182  foreach ($ignoreAttributes as $attributeCode) {
183  unset($attributesByCode[$attributeCode]);
184  }
185 
186  return $attributesByCode;
187  }
188 
196  protected function _addErrorMessages($code, array $messages)
197  {
198  if (!array_key_exists($code, $this->_messages)) {
199  $this->_messages[$code] = $messages;
200  } else {
201  $this->_messages[$code] = array_merge($this->_messages[$code], $messages);
202  }
203  }
204 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(\Magento\Eav\Model\AttributeDataFactory $attrDataFactory)
Definition: Data.php:46
$attributeCode
Definition: extend.phtml:12
$entity
Definition: element.phtml:22
$attributes
Definition: matrix.phtml:13
setAttributesBlackList(array $attributesCodes)
Definition: Data.php:85
setAttributesWhiteList(array $attributesCodes)
Definition: Data.php:71
_addErrorMessages($code, array $messages)
Definition: Data.php:196
$code
Definition: info.phtml:12