Text.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\Attribute\Data;
  7. use Magento\Framework\App\RequestInterface;
  8. /**
  9. * EAV Entity Attribute Text Data Model
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. class Text extends \Magento\Eav\Model\Attribute\Data\AbstractData
  14. {
  15. /**
  16. * @var \Magento\Framework\Stdlib\StringUtils
  17. */
  18. protected $_string;
  19. /**
  20. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  21. * @param \Psr\Log\LoggerInterface $logger
  22. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  23. * @param \Magento\Framework\Stdlib\StringUtils $stringHelper
  24. * @codeCoverageIgnore
  25. */
  26. public function __construct(
  27. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  28. \Psr\Log\LoggerInterface $logger,
  29. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  30. \Magento\Framework\Stdlib\StringUtils $stringHelper
  31. ) {
  32. parent::__construct($localeDate, $logger, $localeResolver);
  33. $this->_string = $stringHelper;
  34. }
  35. /**
  36. * Extract data from request and return value
  37. *
  38. * @param RequestInterface $request
  39. * @return array|string
  40. */
  41. public function extractValue(RequestInterface $request)
  42. {
  43. $value = $this->_getRequestValue($request);
  44. return $this->_applyInputFilter($value);
  45. }
  46. /**
  47. * Validate data
  48. *
  49. * Return true or array of errors
  50. *
  51. * @param array|string $value
  52. * @return bool|array
  53. * @throws \Magento\Framework\Exception\LocalizedException
  54. */
  55. public function validateValue($value)
  56. {
  57. $errors = [];
  58. $attribute = $this->getAttribute();
  59. if ($value === false) {
  60. // try to load original value and validate it
  61. $value = $this->getEntity()->getDataUsingMethod($attribute->getAttributeCode());
  62. }
  63. if (!$attribute->getIsRequired() && empty($value)) {
  64. return true;
  65. }
  66. if (empty($value) && $value !== '0' && $attribute->getDefaultValue() === null) {
  67. $label = __($attribute->getStoreLabel());
  68. $errors[] = __('"%1" is a required value.', $label);
  69. }
  70. $validateLengthResult = $this->validateLength($attribute, $value);
  71. $errors = array_merge($errors, $validateLengthResult);
  72. $validateInputRuleResult = $this->validateInputRule($value);
  73. $errors = array_merge($errors, $validateInputRuleResult);
  74. if (count($errors) == 0) {
  75. return true;
  76. }
  77. return $errors;
  78. }
  79. /**
  80. * Export attribute value to entity model
  81. *
  82. * @param array|string $value
  83. * @return $this
  84. */
  85. public function compactValue($value)
  86. {
  87. if ($value !== false) {
  88. $this->getEntity()->setDataUsingMethod($this->getAttribute()->getAttributeCode(), $value);
  89. }
  90. return $this;
  91. }
  92. /**
  93. * Restore attribute value from SESSION to entity model
  94. *
  95. * @param array|string $value
  96. * @return $this
  97. * @codeCoverageIgnore
  98. */
  99. public function restoreValue($value)
  100. {
  101. return $this->compactValue($value);
  102. }
  103. /**
  104. * Return formatted attribute value from entity model
  105. *
  106. * @param string $format
  107. * @return string|array
  108. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  109. */
  110. public function outputValue($format = \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_TEXT)
  111. {
  112. $value = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
  113. $value = $this->_applyOutputFilter($value);
  114. return $value;
  115. }
  116. /**
  117. * Validates value length by attribute rules
  118. *
  119. * @param \Magento\Eav\Model\Attribute $attribute
  120. * @param string $value
  121. * @return array errors
  122. */
  123. private function validateLength(\Magento\Eav\Model\Attribute $attribute, string $value): array
  124. {
  125. $errors = [];
  126. $length = $this->_string->strlen(trim($value));
  127. $validateRules = $attribute->getValidateRules();
  128. if (!empty($validateRules['input_validation'])) {
  129. if (!empty($validateRules['min_text_length']) && $length < $validateRules['min_text_length']) {
  130. $label = __($attribute->getStoreLabel());
  131. $v = $validateRules['min_text_length'];
  132. $errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $v);
  133. }
  134. if (!empty($validateRules['max_text_length']) && $length > $validateRules['max_text_length']) {
  135. $label = __($attribute->getStoreLabel());
  136. $v = $validateRules['max_text_length'];
  137. $errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $v);
  138. }
  139. }
  140. return $errors;
  141. }
  142. /**
  143. * Validate value by attribute input validation rule.
  144. *
  145. * @param string $value
  146. * @return array
  147. */
  148. private function validateInputRule(string $value): array
  149. {
  150. $result = $this->_validateInputRule($value);
  151. return \is_array($result) ? $result : [];
  152. }
  153. }