1 | <?php |
||
23 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html |
||
24 | */ |
||
25 | class FiltersAggregation extends AbstractAggregation |
||
26 | { |
||
27 | use BucketingTrait; |
||
28 | |||
29 | public function __construct( |
||
30 | private string $name, |
||
|
|||
31 | private array $filters = [], |
||
32 | private bool $anonymous = false |
||
33 | ) { |
||
34 | parent::__construct($name); |
||
35 | |||
36 | $this->setAnonymous($anonymous); |
||
37 | foreach ($filters as $name => $filter) { |
||
38 | if ($anonymous) { |
||
39 | $this->addFilter($filter); |
||
40 | } else { |
||
41 | $this->addFilter($filter, $name); |
||
42 | } |
||
43 | } |
||
44 | } |
||
45 | |||
46 | public function setAnonymous(bool $anonymous): static |
||
47 | { |
||
48 | $this->anonymous = $anonymous; |
||
49 | |||
50 | return $this; |
||
51 | } |
||
52 | |||
53 | public function addFilter(?BuilderInterface $filter = null, string $name = ''): static |
||
54 | { |
||
55 | if ($this->anonymous === false && empty($name)) { |
||
56 | throw new \LogicException('In not anonymous filters filter name must be set.'); |
||
57 | } elseif ($this->anonymous === false && !empty($name)) { |
||
58 | $this->filters['filters'][$name] = $filter->toArray(); |
||
59 | } else { |
||
60 | $this->filters['filters'][] = $filter->toArray(); |
||
61 | } |
||
62 | |||
63 | return $this; |
||
64 | } |
||
65 | |||
66 | public function getArray(): array |
||
67 | { |
||
68 | return $this->filters; |
||
69 | } |
||
70 | |||
71 | public function getType(): string |
||
72 | { |
||
73 | return 'filters'; |
||
74 | } |
||
75 | } |
||
76 |