Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Text.php
Go to the documentation of this file.
1 <?php
8 
10 
16 class Text extends \Magento\Eav\Model\Attribute\Data\AbstractData
17 {
21  protected $_string;
22 
30  public function __construct(
31  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
32  \Psr\Log\LoggerInterface $logger,
33  \Magento\Framework\Locale\ResolverInterface $localeResolver,
34  \Magento\Framework\Stdlib\StringUtils $stringHelper
35  ) {
36  parent::__construct($localeDate, $logger, $localeResolver);
37  $this->_string = $stringHelper;
38  }
39 
47  {
48  $value = $this->_getRequestValue($request);
49  return $this->_applyInputFilter($value);
50  }
51 
61  public function validateValue($value)
62  {
63  $errors = [];
64  $attribute = $this->getAttribute();
65 
66  if ($value === false) {
67  // try to load original value and validate it
68  $value = $this->getEntity()->getDataUsingMethod($attribute->getAttributeCode());
69  }
70 
71  if ($attribute->getIsRequired() && empty($value) && $value !== '0') {
72  $label = __($attribute->getStoreLabel());
73  $errors[] = __('"%1" is a required value.', $label);
74  }
75 
76  if (!$errors && !$attribute->getIsRequired() && empty($value)) {
77  return true;
78  }
79 
80  $result = $this->validateLength($attribute, $value);
81  if (count($result) !== 0) {
82  $errors = array_merge($errors, $result);
83  }
84 
86  if ($result !== true) {
87  $errors = array_merge($errors, $result);
88  }
89  if (count($errors) == 0) {
90  return true;
91  }
92 
93  return $errors;
94  }
95 
102  public function compactValue($value)
103  {
104  if ($value !== false) {
105  $this->getEntity()->setDataUsingMethod($this->getAttribute()->getAttributeCode(), $value);
106  }
107  return $this;
108  }
109 
117  public function restoreValue($value)
118  {
119  return $this->compactValue($value);
120  }
121 
130  {
131  $value = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
132  $value = $this->_applyOutputFilter($value);
133 
134  return $value;
135  }
136 
144  private function validateLength(\Magento\Eav\Model\Attribute $attribute, $value): array
145  {
146  $errors = [];
147  $length = $this->_string->strlen(trim($value));
148  $validateRules = $attribute->getValidateRules();
149 
150  if (!empty($validateRules['input_validation'])) {
151  if (!empty($validateRules['min_text_length']) && $length < $validateRules['min_text_length']) {
152  $label = __($attribute->getStoreLabel());
153  $v = $validateRules['min_text_length'];
154  $errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $v);
155  }
156  if (!empty($validateRules['max_text_length']) && $length > $validateRules['max_text_length']) {
157  $label = __($attribute->getStoreLabel());
158  $v = $validateRules['max_text_length'];
159  $errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $v);
160  }
161  }
162 
163  return $errors;
164  }
165 }
_getRequestValue(RequestInterface $request)
__()
Definition: __.php:13
$logger
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
extractValue(RequestInterface $request)
Definition: Text.php:46
$errors
Definition: overview.phtml:9
outputValue($format=\Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_TEXT)
Definition: Text.php:129
__construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Locale\ResolverInterface $localeResolver, \Magento\Framework\Stdlib\StringUtils $stringHelper)
Definition: Text.php:30