Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractData.php
Go to the documentation of this file.
1 <?php
8 
12 
21 abstract class AbstractData
22 {
28  protected $_attribute;
29 
35  protected $_entity;
36 
42  protected $_requestScope;
43 
49  protected $_requestScopeOnly = true;
50 
56  protected $_isAjax = false;
57 
64  protected $_extractedData = [];
65 
71  protected $_dateFilterFormat;
72 
76  protected $_localeDate;
77 
81  protected $_localeResolver;
82 
86  protected $_logger;
87 
94  public function __construct(
95  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
96  \Psr\Log\LoggerInterface $logger,
97  \Magento\Framework\Locale\ResolverInterface $localeResolver
98  ) {
99  $this->_localeDate = $localeDate;
100  $this->_logger = $logger;
101  $this->_localeResolver = $localeResolver;
102  }
103 
112  {
113  $this->_attribute = $attribute;
114  return $this;
115  }
116 
123  public function getAttribute()
124  {
125  if (!$this->_attribute) {
126  throw new CoreException(__('Attribute object is undefined'));
127  }
128  return $this->_attribute;
129  }
130 
138  public function setRequestScope($scope)
139  {
140  $this->_requestScope = $scope;
141  return $this;
142  }
143 
152  public function setRequestScopeOnly($flag)
153  {
154  $this->_requestScopeOnly = (bool)$flag;
155  return $this;
156  }
157 
165  public function setEntity(\Magento\Framework\Model\AbstractModel $entity)
166  {
167  $this->_entity = $entity;
168  return $this;
169  }
170 
177  public function getEntity()
178  {
179  if (!$this->_entity) {
180  throw new CoreException(__('Entity object is undefined'));
181  }
182  return $this->_entity;
183  }
184 
192  public function setExtractedData(array $data)
193  {
194  $this->_extractedData = $data;
195  return $this;
196  }
197 
204  public function getExtractedData($index = null)
205  {
206  if ($index !== null) {
207  if (isset($this->_extractedData[$index])) {
208  return $this->_extractedData[$index];
209  }
210  return null;
211  }
212  return $this->_extractedData;
213  }
214 
221  protected function _applyInputFilter($value)
222  {
223  if ($value === false) {
224  return false;
225  }
226 
227  $filter = $this->_getFormFilter();
228  if ($filter) {
229  $value = $filter->inputFilter($value);
230  }
231 
232  return $value;
233  }
234 
240  protected function _getFormFilter()
241  {
242  $filterCode = $this->getAttribute()->getInputFilter();
243  if ($filterCode) {
244  $filterClass = 'Magento\Framework\Data\Form\Filter\\' . ucfirst($filterCode);
245  if ($filterCode == 'date') {
246  $filter = new $filterClass($this->_dateFilterFormat(), $this->_localeResolver);
247  } else {
248  $filter = new $filterClass();
249  }
250  return $filter;
251  }
252  return false;
253  }
254 
261  protected function _dateFilterFormat($format = null)
262  {
263  if ($format === null) {
264  // get format
265  if ($this->_dateFilterFormat === null) {
266  $this->_dateFilterFormat = \IntlDateFormatter::SHORT;
267  }
268  return $this->_localeDate->getDateFormat($this->_dateFilterFormat);
269  } elseif ($format === false) {
270  // reset value
271  $this->_dateFilterFormat = null;
272  return $this;
273  }
274 
275  $this->_dateFilterFormat = $format;
276  return $this;
277  }
278 
285  protected function _applyOutputFilter($value)
286  {
287  $filter = $this->_getFormFilter();
288  if ($filter) {
289  $value = $filter->outputFilter($value);
290  }
291 
292  return $value;
293  }
294 
303  protected function _validateInputRule($value)
304  {
305  // skip validate empty value
306  if (empty($value)) {
307  return true;
308  }
309 
310  $validateRules = $this->getAttribute()->getValidateRules();
311 
312  if (!empty($validateRules['input_validation'])) {
313  $label = $this->getAttribute()->getStoreLabel();
314  switch ($validateRules['input_validation']) {
315  case 'alphanumeric':
316  $validator = new \Zend_Validate_Alnum(true);
317  $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Alnum::INVALID);
318  $validator->setMessage(
319  __('"%1" contains non-alphabetic or non-numeric characters.', $label),
321  );
322  $validator->setMessage(__('"%1" is an empty string.', $label), \Zend_Validate_Alnum::STRING_EMPTY);
323  if (!$validator->isValid($value)) {
324  return $validator->getMessages();
325  }
326  break;
327  case 'numeric':
328  $validator = new \Zend_Validate_Digits();
329  $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Digits::INVALID);
330  $validator->setMessage(
331  __('"%1" contains non-numeric characters.', $label),
333  );
334  $validator->setMessage(
335  __('"%1" is an empty string.', $label),
337  );
338  if (!$validator->isValid($value)) {
339  return $validator->getMessages();
340  }
341  break;
342  case 'alpha':
343  $validator = new \Zend_Validate_Alpha(true);
344  $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Alpha::INVALID);
345  $validator->setMessage(
346  __('"%1" contains non-alphabetic characters.', $label),
348  );
349  $validator->setMessage(__('"%1" is an empty string.', $label), \Zend_Validate_Alpha::STRING_EMPTY);
350  if (!$validator->isValid($value)) {
351  return $validator->getMessages();
352  }
353  break;
354  case 'email':
369  $validator = new EmailAddress();
370  $validator->setMessage(
371  __('"%1" invalid type entered.', $label),
373  );
374  $validator->setMessage(
375  __('"%1" is not a valid email address.', $label),
377  );
378  $validator->setMessage(
379  __('"%1" is not a valid hostname.', $label),
381  );
382  $validator->setMessage(
383  __('"%1" is not a valid hostname.', $label),
385  );
386  $validator->setMessage(
387  __('"%1" is not a valid hostname.', $label),
389  );
390  $validator->setMessage(
391  __('"%1" is not a valid email address.', $label),
393  );
394  $validator->setMessage(
395  __('"%1" is not a valid email address.', $label),
397  );
398  $validator->setMessage(
399  __('"%1" is not a valid email address.', $label),
401  );
402  $validator->setMessage(
403  __('"%1" uses too many characters.', $label),
405  );
406  $validator->setMessage(
407  __("'%value%' looks like an IP address, which is not an acceptable format."),
409  );
410  $validator->setMessage(
411  __("'%value%' looks like a DNS hostname but contains a dash in an invalid position."),
413  );
414  $validator->setMessage(
415  __(
416  "'%value%' looks like a DNS hostname but we cannot match it against the "
417  . "hostname schema for TLD '%tld%'."
418  ),
420  );
421  $validator->setMessage(
422  __("'%value%' looks like a DNS hostname but cannot extract TLD part."),
424  );
425  $validator->setMessage(
426  __("'%value%' does not look like a valid local network name."),
428  );
429  $validator->setMessage(
430  __("'%value%' looks like a local network name, which is not an acceptable format."),
432  );
433  $validator->setMessage(
434  __(
435  "'%value%' appears to be a DNS hostname, but the given punycode notation cannot be decoded."
436  ),
438  );
439  if (!$validator->isValid($value)) {
440  return array_unique($validator->getMessages());
441  }
442  break;
443  case 'url':
444  $parsedUrl = parse_url($value);
445  if ($parsedUrl === false || empty($parsedUrl['scheme']) || empty($parsedUrl['host'])) {
446  return [__('"%1" is not a valid URL.', $label)];
447  }
448  $validator = new \Zend_Validate_Hostname();
449  if (!$validator->isValid($parsedUrl['host'])) {
450  return [__('"%1" is not a valid URL.', $label)];
451  }
452  break;
453  case 'date':
454  $validator = new \Zend_Validate_Date(
455  [
456  'format' => \Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT,
457  'locale' => $this->_localeResolver->getLocale(),
458  ]
459  );
460  $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Date::INVALID);
461  $validator->setMessage(__('"%1" is not a valid date.', $label), \Zend_Validate_Date::INVALID_DATE);
462  $validator->setMessage(
463  __('"%1" does not fit the entered date format.', $label),
465  );
466  if (!$validator->isValid($value)) {
467  return array_unique($validator->getMessages());
468  }
469 
470  break;
471  }
472  }
473  return true;
474  }
475 
483  public function setIsAjaxRequest($flag = true)
484  {
485  $this->_isAjax = (bool)$flag;
486  return $this;
487  }
488 
496  public function getIsAjaxRequest()
497  {
498  return $this->_isAjax;
499  }
500 
508  {
509  $attrCode = $this->getAttribute()->getAttributeCode();
510  if ($this->_requestScope) {
511  if (strpos($this->_requestScope, '/') !== false) {
512  $params = $request->getParams();
513  $parts = explode('/', $this->_requestScope);
514  foreach ($parts as $part) {
515  if (isset($params[$part])) {
516  $params = $params[$part];
517  } else {
518  $params = [];
519  }
520  }
521  } else {
522  $params = $request->getParam($this->_requestScope);
523  }
524 
525  if (isset($params[$attrCode])) {
526  $value = $params[$attrCode];
527  } else {
528  $value = false;
529  }
530 
531  if (!$this->_requestScopeOnly && $value === false) {
532  $value = $request->getParam($attrCode, false);
533  }
534  } else {
535  $value = $request->getParam($attrCode, false);
536  }
537  return $value;
538  }
539 
546  abstract public function extractValue(RequestInterface $request);
547 
555  abstract public function validateValue($value);
556 
563  abstract public function compactValue($value);
564 
571  abstract public function restoreValue($value);
572 
579  abstract public function outputValue($format = \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_TEXT);
580 }
const FALSEFORMAT
Definition: Date.php:37
const INVALID_HOSTNAME_SCHEMA
Definition: Hostname.php:53
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Locale\ResolverInterface $localeResolver)
_getRequestValue(RequestInterface $request)
const INVALID
Definition: Date.php:35
setAttribute(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute)
outputValue($format=\Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_TEXT)
__()
Definition: __.php:13
extractValue(RequestInterface $request)
const STRING_EMPTY
Definition: Alpha.php:37
$logger
const CANNOT_DECODE_PUNYCODE
Definition: Hostname.php:49
const INVALID_DATE
Definition: Date.php:36
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
$entity
Definition: element.phtml:22
const IP_ADDRESS_NOT_ALLOWED
Definition: Hostname.php:56
const STRING_EMPTY
Definition: Alnum.php:37
setEntity(\Magento\Framework\Model\AbstractModel $entity)
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
const LOCAL_NAME_NOT_ALLOWED
Definition: Hostname.php:57
$index
Definition: list.phtml:44