Derivative.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Elastica\Aggregation;
  3. use Elastica\Exception\InvalidException;
  4. /**
  5. * Class Derivative.
  6. *
  7. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-derivative-aggregation.html
  8. */
  9. class Derivative extends AbstractAggregation
  10. {
  11. /**
  12. * @param string $name
  13. * @param string|null $bucketsPath
  14. */
  15. public function __construct(string $name, string $bucketsPath = null)
  16. {
  17. parent::__construct($name);
  18. if (null !== $bucketsPath) {
  19. $this->setBucketsPath($bucketsPath);
  20. }
  21. }
  22. /**
  23. * Set the buckets_path for this aggregation.
  24. *
  25. * @param string $bucketsPath
  26. *
  27. * @return $this
  28. */
  29. public function setBucketsPath(string $bucketsPath)
  30. {
  31. return $this->setParam('buckets_path', $bucketsPath);
  32. }
  33. /**
  34. * Set the gap policy for this aggregation.
  35. *
  36. * @param string $gapPolicy
  37. *
  38. * @return $this
  39. */
  40. public function setGapPolicy(string $gapPolicy = 'skip')
  41. {
  42. return $this->setParam('gap_policy', $gapPolicy);
  43. }
  44. /**
  45. * Set the format for this aggregation.
  46. *
  47. * @param string $format
  48. *
  49. * @return $this
  50. */
  51. public function setFormat(string $format)
  52. {
  53. return $this->setParam('format', $format);
  54. }
  55. /**
  56. * @throws InvalidException If buckets path or script is not set
  57. *
  58. * @return array
  59. */
  60. public function toArray(): array
  61. {
  62. if (!$this->hasParam('buckets_path')) {
  63. throw new InvalidException('Buckets path is required');
  64. }
  65. return parent::toArray();
  66. }
  67. }