Aggregation.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Search\Response;
  7. use Magento\Framework\Api\Search\AggregationInterface;
  8. use Magento\Framework\Api\Search\BucketInterface;
  9. /**
  10. * Faceted data
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Aggregation implements AggregationInterface, \IteratorAggregate
  15. {
  16. /**
  17. * Buckets array
  18. *
  19. * @var BucketInterface[]
  20. */
  21. protected $buckets;
  22. /**
  23. * @param BucketInterface[] $buckets
  24. */
  25. public function __construct(array $buckets)
  26. {
  27. $this->buckets = $buckets;
  28. }
  29. /**
  30. * Implementation of \IteratorAggregate::getIterator()
  31. *
  32. * @return \ArrayIterator
  33. */
  34. public function getIterator()
  35. {
  36. return new \ArrayIterator($this->buckets);
  37. }
  38. /**
  39. * Get Document field
  40. *
  41. * @param string $bucketName
  42. * @return BucketInterface
  43. */
  44. public function getBucket($bucketName)
  45. {
  46. return $this->buckets[$bucketName] ?? null;
  47. }
  48. /**
  49. * Get all Document fields
  50. *
  51. * @return BucketInterface[]
  52. */
  53. public function getBuckets()
  54. {
  55. return $this->buckets;
  56. }
  57. /**
  58. * Get Document field names
  59. *
  60. * @return string[]
  61. */
  62. public function getBucketNames()
  63. {
  64. return array_keys($this->buckets);
  65. }
  66. }