Engine.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogSearch\Model\ResourceModel;
  7. /**
  8. * CatalogSearch Fulltext Index Engine resource model
  9. *
  10. * @deprecated 101.0.0
  11. * @see \Magento\ElasticSearch
  12. */
  13. class Engine implements EngineInterface
  14. {
  15. /**
  16. * @deprecated
  17. * @see EngineInterface::FIELD_PREFIX
  18. */
  19. const ATTRIBUTE_PREFIX = 'attr_';
  20. /**
  21. * Scope identifier
  22. *
  23. * @deprecated
  24. * @see EngineInterface::SCOPE_IDENTIFIER
  25. */
  26. const SCOPE_FIELD_NAME = 'scope';
  27. /**
  28. * Catalog product visibility
  29. *
  30. * @var \Magento\Catalog\Model\Product\Visibility
  31. */
  32. protected $catalogProductVisibility;
  33. /**
  34. * @var \Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver
  35. */
  36. private $indexScopeResolver;
  37. /**
  38. * Construct
  39. *
  40. * @param \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility
  41. * @param \Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver $indexScopeResolver
  42. */
  43. public function __construct(
  44. \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
  45. \Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver $indexScopeResolver
  46. ) {
  47. $this->catalogProductVisibility = $catalogProductVisibility;
  48. $this->indexScopeResolver = $indexScopeResolver;
  49. }
  50. /**
  51. * Retrieve allowed visibility values for current engine
  52. *
  53. * @return int[]
  54. */
  55. public function getAllowedVisibility()
  56. {
  57. return $this->catalogProductVisibility->getVisibleInSiteIds();
  58. }
  59. /**
  60. * Define if current search engine supports advanced index
  61. *
  62. * @return bool
  63. */
  64. public function allowAdvancedIndex()
  65. {
  66. return true;
  67. }
  68. /**
  69. * Is attribute filterable as term cache
  70. *
  71. * @var array
  72. */
  73. private $termFilterableAttributeAttributeCache = [];
  74. /**
  75. * Is Attribute Filterable as Term
  76. *
  77. * @param \Magento\Catalog\Model\Entity\Attribute $attribute
  78. * @return bool
  79. */
  80. private function isTermFilterableAttribute($attribute)
  81. {
  82. $attributeId = $attribute->getAttributeId();
  83. if (!isset($this->termFilterableAttributeAttributeCache[$attributeId])) {
  84. $this->termFilterableAttributeAttributeCache[$attributeId] =
  85. in_array($attribute->getFrontendInput(), ['select', 'multiselect'], true)
  86. && ($attribute->getIsVisibleInAdvancedSearch()
  87. || $attribute->getIsFilterable()
  88. || $attribute->getIsFilterableInSearch());
  89. }
  90. return $this->termFilterableAttributeAttributeCache[$attributeId];
  91. }
  92. /**
  93. * @inheritdoc
  94. */
  95. public function processAttributeValue($attribute, $value)
  96. {
  97. $result = false;
  98. if ($attribute->getIsSearchable()
  99. && in_array($attribute->getFrontendInput(), ['text', 'textarea'])
  100. ) {
  101. $result = $value;
  102. } elseif ($this->isTermFilterableAttribute($attribute)
  103. || ($attribute->getIsSearchable() && in_array($attribute->getFrontendInput(), ['select', 'multiselect']))
  104. ) {
  105. $result = '';
  106. }
  107. return $result;
  108. }
  109. /**
  110. * Prepare index array as a string glued by separator
  111. *
  112. * Support 2 level array gluing
  113. *
  114. * @param array $index
  115. * @param string $separator
  116. * @return array
  117. */
  118. public function prepareEntityIndex($index, $separator = ' ')
  119. {
  120. $indexData = [];
  121. foreach ($index as $attributeId => $value) {
  122. $indexData[$attributeId] = is_array($value) ? implode($separator, $value) : $value;
  123. }
  124. return $indexData;
  125. }
  126. /**
  127. * @inheritdoc
  128. */
  129. public function isAvailable()
  130. {
  131. return true;
  132. }
  133. }