AbstractSource.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\Entity\Attribute\Source;
  7. /**
  8. * Entity/Attribute/Model - attribute selection source abstract
  9. *
  10. * @api
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. * @SuppressWarnings(PHPMD.NumberOfChildren)
  13. * @since 100.0.2
  14. */
  15. abstract class AbstractSource implements
  16. \Magento\Eav\Model\Entity\Attribute\Source\SourceInterface,
  17. \Magento\Framework\Option\ArrayInterface
  18. {
  19. /**
  20. * Reference to the attribute instance
  21. *
  22. * @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute
  23. */
  24. protected $_attribute;
  25. /**
  26. * Options array
  27. *
  28. * @var array
  29. */
  30. protected $_options = null;
  31. /**
  32. * Set attribute instance
  33. *
  34. * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
  35. * @return \Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend
  36. * @codeCoverageIgnore
  37. */
  38. public function setAttribute($attribute)
  39. {
  40. $this->_attribute = $attribute;
  41. return $this;
  42. }
  43. /**
  44. * Get attribute instance
  45. *
  46. * @return \Magento\Eav\Model\Entity\Attribute\AbstractAttribute
  47. * @codeCoverageIgnore
  48. */
  49. public function getAttribute()
  50. {
  51. return $this->_attribute;
  52. }
  53. /**
  54. * Get a text for option value
  55. *
  56. * @param string|int $value
  57. * @return string|bool
  58. */
  59. public function getOptionText($value)
  60. {
  61. $options = $this->getAllOptions();
  62. // Fixed for tax_class_id and custom_design
  63. if (sizeof($options) > 0) {
  64. foreach ($options as $option) {
  65. if (isset($option['value']) && $option['value'] == $value) {
  66. return isset($option['label']) ? $option['label'] : $option['value'];
  67. }
  68. }
  69. }
  70. // End
  71. if (isset($options[$value])) {
  72. return $options[$value];
  73. }
  74. return false;
  75. }
  76. /**
  77. * Get option id.
  78. *
  79. * @param string $value
  80. * @return null|string
  81. */
  82. public function getOptionId($value)
  83. {
  84. foreach ($this->getAllOptions() as $option) {
  85. if (strcasecmp($option['label'], $value) == 0 || $option['value'] == $value) {
  86. return $option['value'];
  87. }
  88. }
  89. return null;
  90. }
  91. /**
  92. * Add Value Sort To Collection Select
  93. *
  94. * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection
  95. * @param string $dir direction
  96. * @return $this
  97. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  98. * @codeCoverageIgnore
  99. */
  100. public function addValueSortToCollection($collection, $dir = \Magento\Framework\Data\Collection::SORT_ORDER_DESC)
  101. {
  102. return $this;
  103. }
  104. /**
  105. * Retrieve flat column definition
  106. *
  107. * @codeCoverageIgnore
  108. * @return array
  109. */
  110. public function getFlatColumns()
  111. {
  112. return [];
  113. }
  114. /**
  115. * Retrieve Indexes(s) for Flat
  116. *
  117. * @return array
  118. * @codeCoverageIgnore
  119. */
  120. public function getFlatIndexes()
  121. {
  122. return [];
  123. }
  124. /**
  125. * Retrieve Select For Flat Attribute update
  126. *
  127. * @param int $store
  128. * @return \Magento\Framework\DB\Select|null
  129. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  130. * @codeCoverageIgnore
  131. */
  132. public function getFlatUpdateSelect($store)
  133. {
  134. return null;
  135. }
  136. /**
  137. * Get a text for index option value
  138. *
  139. * @param string|int $value
  140. * @return string|bool
  141. * @codeCoverageIgnore
  142. */
  143. public function getIndexOptionText($value)
  144. {
  145. return $this->getOptionText($value);
  146. }
  147. /**
  148. * Get options as array
  149. *
  150. * @return array
  151. * @codeCoverageIgnore
  152. */
  153. public function toOptionArray()
  154. {
  155. return $this->getAllOptions();
  156. }
  157. }