From 008045cbad307a699bc0490074764ecfc50b380b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Wed, 1 Nov 2023 13:28:16 +0100 Subject: [PATCH 1/3] IBX-6827: Allow to add label aggregation ranges --- .../RangeAggregationResultExtractor.php | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/lib/ResultExtractor/AggregationResultExtractor/RangeAggregationResultExtractor.php b/src/lib/ResultExtractor/AggregationResultExtractor/RangeAggregationResultExtractor.php index a3757fc8..efb3ec02 100644 --- a/src/lib/ResultExtractor/AggregationResultExtractor/RangeAggregationResultExtractor.php +++ b/src/lib/ResultExtractor/AggregationResultExtractor/RangeAggregationResultExtractor.php @@ -45,23 +45,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); From a23236bc18e88963e52ae485370a70c1a6210087 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Wed, 1 Nov 2023 14:07:47 +0100 Subject: [PATCH 2/3] IBX-6827: Allow to add label aggregation ranges (unit tests) --- .../RangeAggregationResultExtractorTest.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/lib/Search/ResultExtractor/AggregationResultExtractor/RangeAggregationResultExtractorTest.php b/tests/lib/Search/ResultExtractor/AggregationResultExtractor/RangeAggregationResultExtractorTest.php index 30ac3117..51c31c2e 100644 --- a/tests/lib/Search/ResultExtractor/AggregationResultExtractor/RangeAggregationResultExtractorTest.php +++ b/tests/lib/Search/ResultExtractor/AggregationResultExtractor/RangeAggregationResultExtractorTest.php @@ -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, @@ -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), ] ), ]; From 83fc34c15b3f5fae94c620f379e81d4db817c3e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Wed, 1 Nov 2023 14:08:08 +0100 Subject: [PATCH 3/3] fixup! IBX-6827: Allow to add label aggregation ranges --- .../RangeAggregationResultExtractor.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/ResultExtractor/AggregationResultExtractor/RangeAggregationResultExtractor.php b/src/lib/ResultExtractor/AggregationResultExtractor/RangeAggregationResultExtractor.php index efb3ec02..df775b35 100644 --- a/src/lib/ResultExtractor/AggregationResultExtractor/RangeAggregationResultExtractor.php +++ b/src/lib/ResultExtractor/AggregationResultExtractor/RangeAggregationResultExtractor.php @@ -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;