Multiline.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 Multiply line Data Model
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. class Multiline extends \Magento\Eav\Model\Attribute\Data\Text
  14. {
  15. /**
  16. * Extract data from request and return value
  17. *
  18. * @param RequestInterface $request
  19. * @return array|string
  20. */
  21. public function extractValue(RequestInterface $request)
  22. {
  23. $value = $this->_getRequestValue($request);
  24. if (!is_array($value)) {
  25. $value = false;
  26. } else {
  27. $value = array_map([$this, '_applyInputFilter'], $value);
  28. }
  29. return $value;
  30. }
  31. /**
  32. * Validate data
  33. * Return true or array of errors
  34. *
  35. * @param array|string $value
  36. * @return bool|array
  37. */
  38. public function validateValue($value)
  39. {
  40. $errors = [];
  41. $lines = $this->processValue($value);
  42. $attribute = $this->getAttribute();
  43. if ($attribute->getIsRequired() && empty($lines)) {
  44. $attributeLabel = __($attribute->getStoreLabel());
  45. $errors[] = __('"%1" is a required value.', $attributeLabel);
  46. }
  47. $maxAllowedLineCount = $attribute->getMultilineCount();
  48. if (count($lines) > $maxAllowedLineCount) {
  49. $attributeLabel = __($attribute->getStoreLabel());
  50. $errors[] = __('"%1" cannot contain more than %2 lines.', $attributeLabel, $maxAllowedLineCount);
  51. }
  52. foreach ($lines as $lineIndex => $line) {
  53. // First line must be always validated
  54. if ($lineIndex == 0 || !empty($line)) {
  55. $result = parent::validateValue($line);
  56. if ($result !== true) {
  57. $errors = array_merge($errors, $result);
  58. }
  59. }
  60. }
  61. return (count($errors) == 0) ? true : $errors;
  62. }
  63. /**
  64. * Process value before validation
  65. *
  66. * @param bool|string|array $value
  67. * @return array list of lines represented by given value
  68. */
  69. protected function processValue($value)
  70. {
  71. if ($value === false) {
  72. // try to load original value and validate it
  73. $attribute = $this->getAttribute();
  74. $entity = $this->getEntity();
  75. $value = $entity->getDataUsingMethod($attribute->getAttributeCode());
  76. }
  77. if (!is_array($value)) {
  78. $value = explode("\n", $value);
  79. }
  80. return $value;
  81. }
  82. /**
  83. * Export attribute value to entity model
  84. *
  85. * @param array|string $value
  86. * @return $this
  87. */
  88. public function compactValue($value)
  89. {
  90. if (is_array($value)) {
  91. $value = trim(implode("\n", $value));
  92. }
  93. return parent::compactValue($value);
  94. }
  95. /**
  96. * Restore attribute value from SESSION to entity model
  97. *
  98. * @param array|string $value
  99. * @return $this
  100. * @codeCoverageIgnore
  101. */
  102. public function restoreValue($value)
  103. {
  104. return $this->compactValue($value);
  105. }
  106. /**
  107. * Return formatted attribute value from entity model
  108. *
  109. * @param string $format
  110. * @return array|string
  111. */
  112. public function outputValue($format = \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_TEXT)
  113. {
  114. $values = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
  115. if (!is_array($values)) {
  116. $values = explode("\n", $values);
  117. }
  118. $values = array_map([$this, '_applyOutputFilter'], $values);
  119. switch ($format) {
  120. case \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_ARRAY:
  121. $output = $values;
  122. break;
  123. case \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_HTML:
  124. $output = implode("<br />", $values);
  125. break;
  126. case \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_ONELINE:
  127. $output = implode(" ", $values);
  128. break;
  129. default:
  130. $output = implode("\n", $values);
  131. break;
  132. }
  133. return $output;
  134. }
  135. }