Select.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 Select Data Model
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. class Select extends \Magento\Eav\Model\Attribute\Data\AbstractData
  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. return $this->_getRequestValue($request);
  24. }
  25. /**
  26. * Validate data
  27. * Return true or array of errors
  28. *
  29. * @param array|string $value
  30. * @return bool|array
  31. */
  32. public function validateValue($value)
  33. {
  34. $errors = [];
  35. $attribute = $this->getAttribute();
  36. if ($value === false) {
  37. // try to load original value and validate it
  38. $value = $this->getEntity()->getData($attribute->getAttributeCode());
  39. }
  40. if ($attribute->getIsRequired() && empty($value) && $value != '0') {
  41. $label = __($attribute->getStoreLabel());
  42. $errors[] = __('"%1" is a required value.', $label);
  43. }
  44. if (!$errors && !$attribute->getIsRequired() && empty($value)) {
  45. return true;
  46. }
  47. if (count($errors) == 0) {
  48. return true;
  49. }
  50. return $errors;
  51. }
  52. /**
  53. * Export attribute value to entity model
  54. *
  55. * @param array|string $value
  56. * @return $this
  57. */
  58. public function compactValue($value)
  59. {
  60. if ($value !== false) {
  61. $this->getEntity()->setData($this->getAttribute()->getAttributeCode(), $value);
  62. }
  63. return $this;
  64. }
  65. /**
  66. * Restore attribute value from SESSION to entity model
  67. *
  68. * @param array|string $value
  69. * @return $this
  70. * @codeCoverageIgnore
  71. */
  72. public function restoreValue($value)
  73. {
  74. return $this->compactValue($value);
  75. }
  76. /**
  77. * Return a text for option value
  78. *
  79. * @param int $value
  80. * @return string
  81. */
  82. protected function _getOptionText($value)
  83. {
  84. return $this->getAttribute()->getSource()->getOptionText($value);
  85. }
  86. /**
  87. * Return formatted attribute value from entity model
  88. *
  89. * @param string $format
  90. * @return string|array
  91. */
  92. public function outputValue($format = \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_TEXT)
  93. {
  94. $value = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
  95. switch ($format) {
  96. case \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON:
  97. $output = $value;
  98. break;
  99. default:
  100. if ($value != '') {
  101. $output = $this->_getOptionText($value);
  102. } else {
  103. $output = '';
  104. }
  105. break;
  106. }
  107. return $output;
  108. }
  109. }