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
10 
13 
17 abstract class AbstractData
18 {
24  protected $_requestScope;
25 
31  protected $_requestScopeOnly = true;
32 
38  protected $_isAjax = false;
39 
46  protected $_extractedData = [];
47 
53  protected $_dateFilterFormat;
54 
58  protected $_localeDate;
59 
63  protected $_localeResolver;
64 
68  protected $_logger;
69 
73  protected $_attribute;
74 
78  protected $_value;
79 
83  protected $_entityTypeCode;
84 
94  public function __construct(
95  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
98  \Magento\Framework\Locale\ResolverInterface $localeResolver,
99  $value,
101  $isAjax = false
102  ) {
103  $this->_localeDate = $localeDate;
104  $this->_logger = $logger;
105  $this->_attribute = $attribute;
106  $this->_localeResolver = $localeResolver;
107  $this->_value = $value;
108  $this->_entityTypeCode = $entityTypeCode;
109  $this->_isAjax = $isAjax;
110  }
111 
118  public function getAttribute()
119  {
120  if (!$this->_attribute) {
121  throw new \Magento\Framework\Exception\LocalizedException(__('Attribute object is undefined'));
122  }
123  return $this->_attribute;
124  }
125 
132  public function setRequestScope($scope)
133  {
134  $this->_requestScope = $scope;
135  return $this;
136  }
137 
145  public function setRequestScopeOnly($flag)
146  {
147  $this->_requestScopeOnly = (bool)$flag;
148  return $this;
149  }
150 
157  public function setExtractedData(array $data)
158  {
159  $this->_extractedData = $data;
160  return $this;
161  }
162 
169  public function getExtractedData($index = null)
170  {
171  if ($index !== null) {
172  if (isset($this->_extractedData[$index])) {
173  return $this->_extractedData[$index];
174  }
175  return null;
176  }
177  return $this->_extractedData;
178  }
179 
186  protected function _applyInputFilter($value)
187  {
188  if ($value === false) {
189  return false;
190  }
191 
192  $filter = $this->_getFormFilter();
193  if ($filter) {
194  $value = $filter->inputFilter($value);
195  }
196 
197  return $value;
198  }
199 
205  protected function _getFormFilter()
206  {
207  $filterCode = $this->getAttribute()->getInputFilter();
208  if ($filterCode) {
209  $filterClass = 'Magento\Framework\Data\Form\Filter\\' . ucfirst($filterCode);
210  if ($filterCode == 'date') {
211  $filter = new $filterClass($this->_dateFilterFormat(), $this->_localeResolver);
212  } else {
213  $filter = new $filterClass();
214  }
215  return $filter;
216  }
217  return false;
218  }
219 
226  protected function _dateFilterFormat($format = null)
227  {
228  if ($format === null) {
229  // get format
230  if ($this->_dateFilterFormat === null) {
231  $this->_dateFilterFormat = \IntlDateFormatter::SHORT;
232  }
233  return $this->_localeDate->getDateFormat($this->_dateFilterFormat);
234  } elseif ($format === false) {
235  // reset value
236  $this->_dateFilterFormat = null;
237  return $this;
238  }
239 
240  $this->_dateFilterFormat = $format;
241  return $this;
242  }
243 
250  protected function _applyOutputFilter($value)
251  {
252  $filter = $this->_getFormFilter();
253  if ($filter) {
254  $value = $filter->outputFilter($value);
255  }
256 
257  return $value;
258  }
259 
268  protected function _validateInputRule($value)
269  {
270  // skip validate empty value
271  if (empty($value)) {
272  return true;
273  }
274 
275  $label = $this->getAttribute()->getStoreLabel();
276  $validateRules = $this->getAttribute()->getValidationRules();
277 
278  $inputValidation = ArrayObjectSearch::getArrayElementByName(
279  $validateRules,
280  'input_validation'
281  );
282 
283  if ($inputValidation !== null) {
284  switch ($inputValidation) {
285  case 'alphanumeric':
286  $validator = new \Zend_Validate_Alnum(true);
287  $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Alnum::INVALID);
288  $validator->setMessage(
289  __('"%1" contains non-alphabetic or non-numeric characters.', $label),
291  );
292  $validator->setMessage(__('"%1" is an empty string.', $label), \Zend_Validate_Alnum::STRING_EMPTY);
293  if (!$validator->isValid($value)) {
294  return $validator->getMessages();
295  }
296  break;
297  case 'numeric':
298  $validator = new \Zend_Validate_Digits();
299  $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Digits::INVALID);
300  $validator->setMessage(
301  __('"%1" contains non-numeric characters.', $label),
303  );
304  $validator->setMessage(
305  __('"%1" is an empty string.', $label),
307  );
308  if (!$validator->isValid($value)) {
309  return $validator->getMessages();
310  }
311  break;
312  case 'alpha':
313  $validator = new \Zend_Validate_Alpha(true);
314  $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Alpha::INVALID);
315  $validator->setMessage(
316  __('"%1" contains non-alphabetic characters.', $label),
318  );
319  $validator->setMessage(__('"%1" is an empty string.', $label), \Zend_Validate_Alpha::STRING_EMPTY);
320  if (!$validator->isValid($value)) {
321  return $validator->getMessages();
322  }
323  break;
324  case 'email':
339  $validator = new EmailAddress();
340  $validator->setMessage(
341  __('"%1" invalid type entered.', $label),
343  );
344  $validator->setMessage(
345  __('"%1" is not a valid email address.', $label),
347  );
348  $validator->setMessage(
349  __('"%1" is not a valid hostname.', $label),
351  );
352  $validator->setMessage(
353  __('"%1" is not a valid hostname.', $label),
355  );
356  $validator->setMessage(
357  __('"%1" is not a valid hostname.', $label),
359  );
360  $validator->setMessage(
361  __('"%1" is not a valid email address.', $label),
363  );
364  $validator->setMessage(
365  __('"%1" is not a valid email address.', $label),
367  );
368  $validator->setMessage(
369  __('"%1" is not a valid email address.', $label),
371  );
372  $validator->setMessage(
373  __('"%1" uses too many characters.', $label),
375  );
376  $validator->setMessage(
377  __("'%value%' looks like an IP address, which is not an acceptable format."),
379  );
380  $validator->setMessage(
381  __("'%value%' looks like a DNS hostname but contains a dash in an invalid position."),
383  );
384  $validator->setMessage(
385  __(
386  "'%value%' looks like a DNS hostname but we cannot match it against "
387  . "the hostname schema for TLD '%tld%'."
388  ),
390  );
391  $validator->setMessage(
392  __("'%value%' looks like a DNS hostname but cannot extract TLD part."),
394  );
395  $validator->setMessage(
396  __("'%value%' does not look like a valid local network name."),
398  );
399  $validator->setMessage(
400  __("'%value%' looks like a local network name, which is not an acceptable format."),
402  );
403  $validator->setMessage(
404  __(
405  "'%value%' appears to be a DNS hostname, but the given punycode notation cannot be decoded."
406  ),
408  );
409  if (!$validator->isValid($value)) {
410  return array_unique($validator->getMessages());
411  }
412  break;
413  case 'url':
414  $parsedUrl = parse_url($value);
415  if ($parsedUrl === false || empty($parsedUrl['scheme']) || empty($parsedUrl['host'])) {
416  return [__('"%1" is not a valid URL.', $label)];
417  }
418  $validator = new \Zend_Validate_Hostname();
419  if (!$validator->isValid($parsedUrl['host'])) {
420  return [__('"%1" is not a valid URL.', $label)];
421  }
422  break;
423  case 'date':
424  $validator = new \Zend_Validate_Date(\Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT);
425  $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Date::INVALID);
426  $validator->setMessage(__('"%1" is not a valid date.', $label), \Zend_Validate_Date::INVALID_DATE);
427  $validator->setMessage(
428  __('"%1" does not fit the entered date format.', $label),
430  );
431  if (!$validator->isValid($value)) {
432  return array_unique($validator->getMessages());
433  }
434 
435  break;
436  }
437  }
438  return true;
439  }
440 
447  public function getIsAjaxRequest()
448  {
449  return $this->_isAjax;
450  }
451 
458  protected function _getRequestValue(\Magento\Framework\App\RequestInterface $request)
459  {
460  $attrCode = $this->getAttribute()->getAttributeCode();
461  if ($this->_requestScope) {
462  if (strpos($this->_requestScope, '/') !== false) {
463  $params = $request->getParams();
464  $parts = explode('/', $this->_requestScope);
465  foreach ($parts as $part) {
466  if (isset($params[$part])) {
467  $params = $params[$part];
468  } else {
469  $params = [];
470  }
471  }
472  } else {
473  $params = $request->getParam($this->_requestScope);
474  }
475 
476  if (isset($params[$attrCode])) {
477  $value = $params[$attrCode];
478  } else {
479  $value = false;
480  }
481 
482  if (!$this->_requestScopeOnly && $value === false) {
483  $value = $request->getParam($attrCode, false);
484  }
485  } else {
486  $value = $request->getParam($attrCode, false);
487  }
488  return $value;
489  }
490 
497  abstract public function extractValue(\Magento\Framework\App\RequestInterface $request);
498 
506  abstract public function validateValue($value);
507 
514  abstract public function compactValue($value);
515 
522  abstract public function restoreValue($value);
523 
530  abstract public function outputValue(
532  );
533 }
const FALSEFORMAT
Definition: Date.php:37
const INVALID_HOSTNAME_SCHEMA
Definition: Hostname.php:53
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
const INVALID
Definition: Date.php:35
outputValue( $format=\Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_TEXT)
__()
Definition: __.php:13
extractValue(\Magento\Framework\App\RequestInterface $request)
const STRING_EMPTY
Definition: Alpha.php:37
$logger
const CANNOT_DECODE_PUNYCODE
Definition: Hostname.php:49
_getRequestValue(\Magento\Framework\App\RequestInterface $request)
const INVALID_DATE
Definition: Date.php:36
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
const IP_ADDRESS_NOT_ALLOWED
Definition: Hostname.php:56
const STRING_EMPTY
Definition: Alnum.php:37
$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
__construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Psr\Log\LoggerInterface $logger, \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute, \Magento\Framework\Locale\ResolverInterface $localeResolver, $value, $entityTypeCode, $isAjax=false)