Improved.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Search\Dynamic\Algorithm;
  7. use Magento\Framework\DB\Ddl\Table;
  8. use Magento\Framework\Search\Adapter\OptionsInterface;
  9. use Magento\Framework\Search\Dynamic\Algorithm;
  10. use Magento\Framework\Search\Dynamic\DataProviderInterface;
  11. use Magento\Framework\Search\Request\BucketInterface;
  12. class Improved implements AlgorithmInterface
  13. {
  14. /**
  15. * @var Algorithm
  16. */
  17. private $algorithm;
  18. /**
  19. * @var DataProviderInterface
  20. */
  21. private $dataProvider;
  22. /**
  23. * @var OptionsInterface
  24. */
  25. private $options;
  26. /**
  27. * @param DataProviderInterface $dataProvider
  28. * @param Algorithm $algorithm
  29. * @param OptionsInterface $options
  30. */
  31. public function __construct(
  32. DataProviderInterface $dataProvider,
  33. Algorithm $algorithm,
  34. OptionsInterface $options
  35. ) {
  36. $this->algorithm = $algorithm;
  37. $this->dataProvider = $dataProvider;
  38. $this->options = $options;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getItems(
  44. BucketInterface $bucket,
  45. array $dimensions,
  46. \Magento\Framework\Search\Dynamic\EntityStorage $entityStorage
  47. ) {
  48. $aggregations = $this->dataProvider->getAggregations($entityStorage);
  49. $options = $this->options->get();
  50. if ($aggregations['count'] < $options['interval_division_limit']) {
  51. return [];
  52. }
  53. $this->algorithm->setStatistics(
  54. $aggregations['min'],
  55. $aggregations['max'],
  56. $aggregations['std'],
  57. $aggregations['count']
  58. );
  59. $this->algorithm->setLimits($aggregations['min'], $aggregations['max'] + 0.01);
  60. $interval = $this->dataProvider->getInterval($bucket, $dimensions, $entityStorage);
  61. $data = $this->algorithm->calculateSeparators($interval);
  62. $data[0]['from'] = ''; // We should not calculate min and max value
  63. $data[count($data) - 1]['to'] = '';
  64. $dataSize = count($data);
  65. for ($key = 0; $key < $dataSize; $key++) {
  66. if (isset($data[$key + 1])) {
  67. $data[$key]['to'] = $data[$key + 1]['from'];
  68. }
  69. }
  70. return $data;
  71. }
  72. }