Interval.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Search\Adapter\Mysql\Aggregation;
  7. use Magento\Framework\DB\Select;
  8. use Magento\Framework\Search\Dynamic\IntervalInterface;
  9. /**
  10. * MySQL search aggregation interval.
  11. *
  12. * @deprecated 102.0.0
  13. * @see \Magento\ElasticSearch
  14. */
  15. class Interval implements IntervalInterface
  16. {
  17. /**
  18. * Minimal possible value
  19. */
  20. const DELTA = 0.005;
  21. /**
  22. * @var Select
  23. */
  24. private $select;
  25. /**
  26. * @param Select $select
  27. */
  28. public function __construct(Select $select)
  29. {
  30. $this->select = $select;
  31. }
  32. /**
  33. * Get value field
  34. *
  35. * @return string
  36. */
  37. private function getValueFiled()
  38. {
  39. $field = $this->select->getPart(Select::COLUMNS)[0];
  40. return $field[1];
  41. }
  42. /**
  43. * @inheritdoc
  44. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  45. */
  46. public function load($limit, $offset = null, $lower = null, $upper = null)
  47. {
  48. $select = clone $this->select;
  49. $value = $this->getValueFiled();
  50. if ($lower !== null) {
  51. $select->where("${value} >= ?", $lower - self::DELTA);
  52. }
  53. if ($upper !== null) {
  54. $select->where("${value} < ?", $upper - self::DELTA);
  55. }
  56. $select->order("value ASC")
  57. ->limit($limit, $offset);
  58. return $this->arrayValuesToFloat(
  59. $this->select->getConnection()
  60. ->fetchCol($select)
  61. );
  62. }
  63. /**
  64. * @inheritdoc
  65. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  66. */
  67. public function loadPrevious($data, $index, $lower = null)
  68. {
  69. $select = clone $this->select;
  70. $value = $this->getValueFiled();
  71. $select->columns(['count' => 'COUNT(*)'])
  72. ->where("${value} < ?", $data - self::DELTA);
  73. if ($lower !== null) {
  74. $select->where("${value} >= ?", $lower - self::DELTA);
  75. }
  76. $offset = $this->select->getConnection()
  77. ->fetchRow($select)['count'];
  78. if (!$offset) {
  79. return false;
  80. }
  81. return $this->load($index - $offset + 1, $offset - 1, $lower);
  82. }
  83. /**
  84. * @inheritdoc
  85. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  86. */
  87. public function loadNext($data, $rightIndex, $upper = null)
  88. {
  89. $select = clone $this->select;
  90. $value = $this->getValueFiled();
  91. $select->columns(['count' => 'COUNT(*)'])
  92. ->where("${value} > ?", $data + self::DELTA);
  93. if ($upper !== null) {
  94. $select->where("${value} < ? ", $data - self::DELTA);
  95. }
  96. $offset = $this->select->getConnection()
  97. ->fetchRow($select)['count'];
  98. if (!$offset) {
  99. return false;
  100. }
  101. $select = clone $this->select;
  102. $select->where("${value} >= ?", $data - self::DELTA);
  103. if ($upper !== null) {
  104. $select->where("${value} < ? ", $data - self::DELTA);
  105. }
  106. $select->order("${value} DESC")
  107. ->limit($rightIndex - $offset + 1, $offset - 1);
  108. return $this->arrayValuesToFloat(
  109. array_reverse(
  110. $this->select->getConnection()
  111. ->fetchCol($select)
  112. )
  113. );
  114. }
  115. /**
  116. * Convert array values to float.
  117. *
  118. * @param array $prices
  119. * @return array
  120. */
  121. private function arrayValuesToFloat($prices)
  122. {
  123. $returnPrices = [];
  124. if (is_array($prices) && !empty($prices)) {
  125. $returnPrices = array_map('floatval', $prices);
  126. }
  127. return $returnPrices;
  128. }
  129. }