Skip to content

Commit a273914

Browse files
committed
Aggregations
1 parent 4573b4d commit a273914

26 files changed

+457
-109
lines changed

src/Aggregation/AbstractLeafAggregation.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Aggregation/AggregationCollection.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,52 @@
33
namespace Spameri\ElasticQuery\Aggregation;
44

55

6-
class AggregationCollection extends AbstractLeafAggregation
6+
class AggregationCollection implements LeafAggregationInterface
77
{
88

9-
/**
10-
* @var string
11-
*/
12-
private $name;
139
/**
1410
* @var \Spameri\ElasticQuery\Filter\FilterCollection
1511
*/
1612
private $filter;
13+
1714
/**
18-
* @var \Spameri\ElasticQuery\Aggregation\AbstractLeafAggregation
15+
* @var \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection
1916
*/
2017
private $aggregations;
2118

2219

2320
public function __construct(
24-
string $name,
2521
?\Spameri\ElasticQuery\Filter\FilterCollection $filter,
26-
\Spameri\ElasticQuery\Aggregation\AbstractLeafAggregation ... $aggregations
22+
\Spameri\ElasticQuery\Aggregation\LeafAggregationCollection ... $aggregations
2723
)
2824
{
2925
if ( ! $filter) {
3026
$filter = new \Spameri\ElasticQuery\Filter\FilterCollection();
3127
}
3228

33-
$this->name = $name;
3429
$this->filter = $filter;
3530
$this->aggregations = $aggregations;
3631
}
3732

3833

39-
public function name() : string
34+
public function key() : string
4035
{
41-
return $this->name;
36+
return '';
4237
}
4338

4439

45-
public function key() : string
40+
public function filter() : \Spameri\ElasticQuery\Filter\FilterCollection
4641
{
47-
return $this->name;
42+
return $this->filter;
4843
}
4944

5045

5146
public function toArray() : array
5247
{
53-
$array['aggregations'][$this->name] = [];
54-
55-
if ($this->filter) {
56-
$array['aggregations'][$this->name] = $this->filter->toArray();
57-
}
48+
$array = [];
5849

5950
foreach ($this->aggregations as $aggregation) {
60-
$array['aggregations'][$this->name]['aggregations'][$aggregation->name()] = $aggregation->toArray();
51+
$array = $array + $aggregation->toArray();
6152
}
6253

6354
return $array;

src/Aggregation/Histogram.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Aggregation;
4+
5+
6+
class Histogram implements LeafAggregationInterface
7+
{
8+
9+
/**
10+
* @var string
11+
*/
12+
private $field;
13+
14+
/**
15+
* @var int
16+
*/
17+
private $interval;
18+
19+
20+
public function __construct(
21+
string $field,
22+
int $interval
23+
)
24+
{
25+
$this->field = $field;
26+
$this->interval = $interval;
27+
}
28+
29+
30+
public function key() : string
31+
{
32+
return $this->field;
33+
}
34+
35+
36+
public function toArray() : array
37+
{
38+
return [
39+
'histogram' => [
40+
'field' => $this->field,
41+
'interval' => $this->interval,
42+
],
43+
];
44+
}
45+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Aggregation;
4+
5+
6+
class LeafAggregationCollection implements LeafAggregationInterface
7+
{
8+
9+
/**
10+
* @var string
11+
*/
12+
private $name;
13+
14+
/**
15+
* @var \Spameri\ElasticQuery\Filter\FilterCollection
16+
*/
17+
private $filter;
18+
19+
/**
20+
* @var \Spameri\ElasticQuery\Aggregation\LeafAggregationInterface
21+
*/
22+
private $aggregations;
23+
24+
25+
public function __construct(
26+
string $name,
27+
?\Spameri\ElasticQuery\Filter\FilterCollection $filter,
28+
\Spameri\ElasticQuery\Aggregation\LeafAggregationInterface ... $aggregations
29+
)
30+
{
31+
if ( ! $filter) {
32+
$filter = new \Spameri\ElasticQuery\Filter\FilterCollection();
33+
}
34+
35+
$this->name = $name;
36+
$this->filter = $filter;
37+
$this->aggregations = $aggregations;
38+
}
39+
40+
41+
public function key() : string
42+
{
43+
return $this->name;
44+
}
45+
46+
47+
public function filter() : \Spameri\ElasticQuery\Filter\FilterCollection
48+
{
49+
return $this->filter;
50+
}
51+
52+
53+
public function toArray() : array
54+
{
55+
$array = [];
56+
57+
foreach ($this->aggregations as $aggregation) {
58+
if ($aggregation instanceof LeafAggregationCollection) {
59+
$array[$this->key()]['aggs'] = $aggregation->toArray();
60+
61+
} else {
62+
$array[$this->key()] = $array + $aggregation->toArray();
63+
}
64+
}
65+
66+
return $array;
67+
}
68+
69+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Aggregation;
4+
5+
6+
interface LeafAggregationInterface extends \Spameri\ElasticQuery\Entity\EntityInterface
7+
{
8+
9+
}

src/Aggregation/Range.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Aggregation;
4+
5+
6+
class Range implements LeafAggregationInterface
7+
{
8+
9+
/**
10+
* @var string
11+
*/
12+
private $field;
13+
14+
/**
15+
* @var bool
16+
*/
17+
private $keyed;
18+
19+
/**
20+
* @var \Spameri\ElasticQuery\Aggregation\RangeValueCollection
21+
*/
22+
private $ranges;
23+
24+
25+
public function __construct(
26+
string $field,
27+
bool $keyed = FALSE,
28+
\Spameri\ElasticQuery\Aggregation\RangeValueCollection $rangeValueCollection = NULL
29+
)
30+
{
31+
$this->field = $field;
32+
$this->keyed = $keyed;
33+
$this->ranges = $rangeValueCollection ?: new \Spameri\ElasticQuery\Aggregation\RangeValueCollection();
34+
}
35+
36+
37+
public function key() : string
38+
{
39+
return $this->field;
40+
}
41+
42+
43+
public function toArray() : array
44+
{
45+
$array = [
46+
'field' => $this->field,
47+
];
48+
49+
if ($this->keyed === TRUE) {
50+
$array['keyed'] = TRUE;
51+
}
52+
53+
foreach ($this->ranges as $range) {
54+
$array['ranges'][] = $range->toArray();
55+
}
56+
57+
return [
58+
'range' => $array,
59+
];
60+
}
61+
62+
63+
public function ranges() : \Spameri\ElasticQuery\Aggregation\RangeValueCollection
64+
{
65+
return $this->ranges;
66+
}
67+
68+
}

src/Aggregation/RangeValue.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Aggregation;
4+
5+
6+
class RangeValue implements \Spameri\ElasticQuery\Entity\EntityInterface
7+
{
8+
9+
/**
10+
* @var string
11+
*/
12+
private $key;
13+
14+
/**
15+
* @var int
16+
*/
17+
private $from;
18+
19+
/**
20+
* @var int
21+
*/
22+
private $to;
23+
24+
25+
public function __construct(
26+
string $key,
27+
int $from,
28+
int $to
29+
)
30+
{
31+
$this->key = $key;
32+
$this->from = $from;
33+
$this->to = $to;
34+
}
35+
36+
37+
public function key() : string
38+
{
39+
return $this->key;
40+
}
41+
42+
43+
public function toArray() : array
44+
{
45+
return [
46+
'key' => $this->key,
47+
'from' => $this->from,
48+
'to' => $this->to,
49+
];
50+
}
51+
52+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Aggregation;
4+
5+
6+
class RangeValueCollection implements \IteratorAggregate
7+
{
8+
9+
/**
10+
* @var \Spameri\ElasticQuery\Aggregation\RangeValue[]
11+
*/
12+
private $collection;
13+
14+
15+
public function __construct(
16+
RangeValue ... $collection
17+
)
18+
{
19+
$this->collection = $collection;
20+
}
21+
22+
23+
public function getIterator() : \ArrayIterator
24+
{
25+
return new \ArrayIterator($this->collection);
26+
}
27+
28+
29+
public function add(
30+
\Spameri\ElasticQuery\Aggregation\RangeValue $rangeValue
31+
) : void
32+
{
33+
$this->collection[] = $rangeValue;
34+
}
35+
36+
}

0 commit comments

Comments
 (0)