Skip to content

Commit

Permalink
IBX-6827: Aggregation API improvements (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs authored Dec 12, 2023
1 parent 7d102ca commit 0acd506
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\AbstractRangeAggregation;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\Range;
use Ibexa\Contracts\Core\Repository\Values\Content\Search\AggregationResult;
use Ibexa\Contracts\Core\Repository\Values\Content\Search\AggregationResult\RangeAggregationResult;
use Ibexa\Contracts\Core\Repository\Values\Content\Search\AggregationResult\RangeAggregationResultEntry;
Expand Down Expand Up @@ -45,23 +44,20 @@ public function extract(Aggregation $aggregation, array $languageFilter, stdClas
$entries = [];

foreach ($data as $key => $bucket) {
if ($key === 'count') {
if ($key === 'count' || strpos($key, '_') === false) {
continue;
}

if (strpos($key, '_') === false) {
continue;
}
$values = explode('_', $key, 2);
$a = $this->keyMapper->map($aggregation, $languageFilter, $values[0]);
$b = $this->keyMapper->map($aggregation, $languageFilter, $values[1]);

list($from, $to) = explode('_', $key, 2);

$entries[] = new RangeAggregationResultEntry(
new Range(
$this->keyMapper->map($aggregation, $languageFilter, $from),
$this->keyMapper->map($aggregation, $languageFilter, $to),
),
$bucket->count
);
foreach ($aggregation->getRanges() as $range) {
if ($range->getFrom() == $a && $range->getTo() == $b) {
$entries[] = new RangeAggregationResultEntry($range, $bucket->count);
break;
}
}
}

$this->sort($aggregation, $entries);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public function dataProviderForTestExtract(): iterable
{
$aggregation = $this->createMock(AbstractRangeAggregation::class);
$aggregation->method('getName')->willReturn(self::EXAMPLE_AGGREGATION_NAME);
$aggregation->method('getRanges')->willReturn([
new Range(Range::INF, 10, 'a'),
new Range(10, 100, 'b'),
new Range(100, Range::INF, 'c'),
]);

yield 'default' => [
$aggregation,
Expand All @@ -78,9 +83,9 @@ public function dataProviderForTestExtract(): iterable
new RangeAggregationResult(
self::EXAMPLE_AGGREGATION_NAME,
[
new RangeAggregationResultEntry(new Range(null, '10'), 10),
new RangeAggregationResultEntry(new Range('10', '100'), 100),
new RangeAggregationResultEntry(new Range('100', null), 1000),
new RangeAggregationResultEntry(new Range(Range::INF, '10', 'a'), 10),
new RangeAggregationResultEntry(new Range('10', '100', 'b'), 100),
new RangeAggregationResultEntry(new Range('100', Range::INF, 'c'), 1000),
]
),
];
Expand Down

0 comments on commit 0acd506

Please sign in to comment.