Text.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Form Element Text Data Model
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Customer\Model\Metadata\Form;
  9. use Magento\Customer\Api\Data\AttributeMetadataInterface;
  10. use Magento\Framework\Api\ArrayObjectSearch;
  11. /**
  12. * Form Text metadata
  13. */
  14. class Text extends AbstractData
  15. {
  16. /**
  17. * @var \Magento\Framework\Stdlib\StringUtils
  18. */
  19. protected $_string;
  20. /**
  21. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  22. * @param \Psr\Log\LoggerInterface $logger
  23. * @param AttributeMetadataInterface $attribute
  24. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  25. * @param string $value
  26. * @param string $entityTypeCode
  27. * @param bool $isAjax
  28. * @param \Magento\Framework\Stdlib\StringUtils $stringHelper
  29. */
  30. public function __construct(
  31. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  32. \Psr\Log\LoggerInterface $logger,
  33. AttributeMetadataInterface $attribute,
  34. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  35. $value,
  36. $entityTypeCode,
  37. $isAjax,
  38. \Magento\Framework\Stdlib\StringUtils $stringHelper
  39. ) {
  40. parent::__construct($localeDate, $logger, $attribute, $localeResolver, $value, $entityTypeCode, $isAjax);
  41. $this->_string = $stringHelper;
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. public function extractValue(\Magento\Framework\App\RequestInterface $request)
  47. {
  48. return $this->_applyInputFilter($this->_getRequestValue($request));
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function validateValue($value)
  54. {
  55. $errors = [];
  56. $attribute = $this->getAttribute();
  57. $label = __($attribute->getStoreLabel());
  58. if ($value === false) {
  59. // try to load original value and validate it
  60. $value = $this->_value;
  61. }
  62. if (!$attribute->isRequired() && empty($value)) {
  63. return true;
  64. }
  65. if (empty($value) && $value !== '0') {
  66. $errors[] = __('"%1" is a required value.', $label);
  67. }
  68. $errors = $this->validateLength($value, $attribute, $errors);
  69. $result = $this->_validateInputRule($value);
  70. if ($result !== true) {
  71. $errors = array_merge($errors, $result);
  72. }
  73. if (count($errors) == 0) {
  74. return true;
  75. }
  76. return $errors;
  77. }
  78. /**
  79. * @inheritdoc
  80. */
  81. public function compactValue($value)
  82. {
  83. return $value;
  84. }
  85. /**
  86. * @inheritdoc
  87. */
  88. public function restoreValue($value)
  89. {
  90. return $this->compactValue($value);
  91. }
  92. /**
  93. * @inheritdoc
  94. */
  95. public function outputValue($format = \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_TEXT)
  96. {
  97. return $this->_applyOutputFilter($this->_value);
  98. }
  99. /**
  100. * Length validation
  101. *
  102. * @param mixed $value
  103. * @param AttributeMetadataInterface $attribute
  104. * @param array $errors
  105. * @return array
  106. */
  107. private function validateLength($value, AttributeMetadataInterface $attribute, array $errors): array
  108. {
  109. // validate length
  110. $label = __($attribute->getStoreLabel());
  111. $length = $this->_string->strlen(trim($value));
  112. $validateRules = $attribute->getValidationRules();
  113. if (!empty(ArrayObjectSearch::getArrayElementByName($validateRules, 'input_validation'))) {
  114. $minTextLength = ArrayObjectSearch::getArrayElementByName(
  115. $validateRules,
  116. 'min_text_length'
  117. );
  118. if ($minTextLength !== null && $length < $minTextLength) {
  119. $errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $minTextLength);
  120. }
  121. $maxTextLength = ArrayObjectSearch::getArrayElementByName(
  122. $validateRules,
  123. 'max_text_length'
  124. );
  125. if ($maxTextLength !== null && $length > $maxTextLength) {
  126. $errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $maxTextLength);
  127. }
  128. }
  129. return $errors;
  130. }
  131. }