Multiline.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Form Element Multiline 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. class Multiline extends Text
  10. {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public function extractValue(\Magento\Framework\App\RequestInterface $request)
  15. {
  16. $value = $this->_getRequestValue($request);
  17. if (!is_array($value)) {
  18. $value = false;
  19. } else {
  20. $value = array_map([$this, '_applyInputFilter'], $value);
  21. }
  22. return $value;
  23. }
  24. /**
  25. * {@inheritdoc}
  26. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  27. */
  28. public function validateValue($value)
  29. {
  30. $errors = [];
  31. $attribute = $this->getAttribute();
  32. if ($value === false) {
  33. // try to load original value and validate it
  34. $value = $this->_value;
  35. if (!is_array($value)) {
  36. $value = explode("\n", $value);
  37. }
  38. }
  39. if (!is_array($value)) {
  40. $value = [$value];
  41. }
  42. for ($i = 0; $i < $attribute->getMultilineCount(); $i++) {
  43. if (!isset($value[$i])) {
  44. $value[$i] = null;
  45. }
  46. // validate first line
  47. if ($i == 0) {
  48. $result = parent::validateValue($value[$i]);
  49. if ($result !== true) {
  50. $errors = $result;
  51. }
  52. } else {
  53. if (!empty($value[$i])) {
  54. $result = parent::validateValue($value[$i]);
  55. if ($result !== true) {
  56. $errors = array_merge($errors, $result);
  57. }
  58. }
  59. }
  60. }
  61. if (count($errors) == 0) {
  62. return true;
  63. }
  64. return $errors;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function compactValue($value)
  70. {
  71. if (!is_array($value)) {
  72. $value = [$value];
  73. }
  74. return parent::compactValue($value);
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function restoreValue($value)
  80. {
  81. return $this->compactValue($value);
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function outputValue($format = \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_TEXT)
  87. {
  88. $values = $this->_value;
  89. if (!is_array($values)) {
  90. $values = explode("\n", $values);
  91. }
  92. $values = array_map([$this, '_applyOutputFilter'], $values);
  93. switch ($format) {
  94. case \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_ARRAY:
  95. $output = $values;
  96. break;
  97. case \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_HTML:
  98. $output = implode("<br />", $values);
  99. break;
  100. case \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_ONELINE:
  101. $output = implode(" ", $values);
  102. break;
  103. default:
  104. $output = implode("\n", $values);
  105. break;
  106. }
  107. return $output;
  108. }
  109. }