Skip to content

Commit

Permalink
Add support for nested aggregations
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart van Asselt committed Apr 25, 2024
1 parent 3a0283c commit 894d73f
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/Application/Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace JeroenG\Explorer\Application;

use Countable;
use function array_merge;
use function is_array;

class Results implements Countable
{
Expand All @@ -31,11 +33,7 @@ public function aggregations(): array

foreach ($this->rawResults['aggregations'] as $name => $rawAggregation) {
if (array_key_exists('doc_count', $rawAggregation)) {
foreach ($rawAggregation as $nestedAggregationName => $rawNestedAggregation) {
if (isset($rawNestedAggregation['buckets'])) {
$aggregations[] = new AggregationResult($nestedAggregationName, $rawNestedAggregation['buckets']);
}
}
$aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawAggregation));

Check warning on line 36 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $aggregations = []; foreach ($this->rawResults['aggregations'] as $name => $rawAggregation) { if (array_key_exists('doc_count', $rawAggregation)) { - $aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawAggregation)); + $aggregations = $this->parseNestedAggregation($rawAggregation); continue; } $aggregations[] = new AggregationResult($name, $rawAggregation['buckets'] ?? $rawAggregation);

Check warning on line 36 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $aggregations = []; foreach ($this->rawResults['aggregations'] as $name => $rawAggregation) { if (array_key_exists('doc_count', $rawAggregation)) { - $aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawAggregation)); + $aggregations = $this->parseNestedAggregation($rawAggregation); continue; } $aggregations[] = new AggregationResult($name, $rawAggregation['buckets'] ?? $rawAggregation);

Check warning on line 36 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.2

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $aggregations = []; foreach ($this->rawResults['aggregations'] as $name => $rawAggregation) { if (array_key_exists('doc_count', $rawAggregation)) { - $aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawAggregation)); + $aggregations = $this->parseNestedAggregation($rawAggregation); continue; } $aggregations[] = new AggregationResult($name, $rawAggregation['buckets'] ?? $rawAggregation);

Check warning on line 36 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8.1

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $aggregations = []; foreach ($this->rawResults['aggregations'] as $name => $rawAggregation) { if (array_key_exists('doc_count', $rawAggregation)) { - $aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawAggregation)); + $aggregations = $this->parseNestedAggregation($rawAggregation); continue; } $aggregations[] = new AggregationResult($name, $rawAggregation['buckets'] ?? $rawAggregation);

Check warning on line 36 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.3

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $aggregations = []; foreach ($this->rawResults['aggregations'] as $name => $rawAggregation) { if (array_key_exists('doc_count', $rawAggregation)) { - $aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawAggregation)); + $aggregations = $this->parseNestedAggregation($rawAggregation); continue; } $aggregations[] = new AggregationResult($name, $rawAggregation['buckets'] ?? $rawAggregation);

Check warning on line 36 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8.2

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $aggregations = []; foreach ($this->rawResults['aggregations'] as $name => $rawAggregation) { if (array_key_exists('doc_count', $rawAggregation)) { - $aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawAggregation)); + $aggregations = $this->parseNestedAggregation($rawAggregation); continue; } $aggregations[] = new AggregationResult($name, $rawAggregation['buckets'] ?? $rawAggregation);

Check warning on line 36 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8.3

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $aggregations = []; foreach ($this->rawResults['aggregations'] as $name => $rawAggregation) { if (array_key_exists('doc_count', $rawAggregation)) { - $aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawAggregation)); + $aggregations = $this->parseNestedAggregation($rawAggregation); continue; } $aggregations[] = new AggregationResult($name, $rawAggregation['buckets'] ?? $rawAggregation);

Check warning on line 36 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8.4

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $aggregations = []; foreach ($this->rawResults['aggregations'] as $name => $rawAggregation) { if (array_key_exists('doc_count', $rawAggregation)) { - $aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawAggregation)); + $aggregations = $this->parseNestedAggregation($rawAggregation); continue; } $aggregations[] = new AggregationResult($name, $rawAggregation['buckets'] ?? $rawAggregation);
continue;
}

Expand All @@ -49,4 +47,22 @@ public function count(): int
{
return $this->rawResults['hits']['total']['value'];
}

/** @return AggregationResult[] */
private function parseNestedAggregation(array $rawAggregation): array {
$aggregations = [];
if (isset($rawAggregation['doc_count'])) {
foreach ($rawAggregation as $nestedAggregationName => $rawNestedAggregation) {
if (isset($rawNestedAggregation['buckets'])) {
$aggregations[] = new AggregationResult($nestedAggregationName, $rawNestedAggregation['buckets']);
}

if(is_array($rawNestedAggregation)) {
$aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawNestedAggregation));

Check warning on line 61 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $aggregations[] = new AggregationResult($nestedAggregationName, $rawNestedAggregation['buckets']); } if (is_array($rawNestedAggregation)) { - $aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawNestedAggregation)); + $aggregations = $aggregations; } } }

Check warning on line 61 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $aggregations[] = new AggregationResult($nestedAggregationName, $rawNestedAggregation['buckets']); } if (is_array($rawNestedAggregation)) { - $aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawNestedAggregation)); + $aggregations = $aggregations; } } }

Check warning on line 61 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.2

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $aggregations[] = new AggregationResult($nestedAggregationName, $rawNestedAggregation['buckets']); } if (is_array($rawNestedAggregation)) { - $aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawNestedAggregation)); + $aggregations = $aggregations; } } }

Check warning on line 61 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8.1

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $aggregations[] = new AggregationResult($nestedAggregationName, $rawNestedAggregation['buckets']); } if (is_array($rawNestedAggregation)) { - $aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawNestedAggregation)); + $aggregations = $aggregations; } } }

Check warning on line 61 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.3

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $aggregations[] = new AggregationResult($nestedAggregationName, $rawNestedAggregation['buckets']); } if (is_array($rawNestedAggregation)) { - $aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawNestedAggregation)); + $aggregations = $aggregations; } } }

Check warning on line 61 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8.2

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $aggregations[] = new AggregationResult($nestedAggregationName, $rawNestedAggregation['buckets']); } if (is_array($rawNestedAggregation)) { - $aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawNestedAggregation)); + $aggregations = $aggregations; } } }

Check warning on line 61 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8.3

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $aggregations[] = new AggregationResult($nestedAggregationName, $rawNestedAggregation['buckets']); } if (is_array($rawNestedAggregation)) { - $aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawNestedAggregation)); + $aggregations = $aggregations; } } }

Check warning on line 61 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8.4

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $aggregations[] = new AggregationResult($nestedAggregationName, $rawNestedAggregation['buckets']); } if (is_array($rawNestedAggregation)) { - $aggregations = array_merge($aggregations, $this->parseNestedAggregation($rawNestedAggregation)); + $aggregations = $aggregations; } } }
}
}
}

return $aggregations;

Check warning on line 66 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ } } } - return $aggregations; + return count($aggregations) > 1 ? array_slice($aggregations, 0, 1, true) : $aggregations; } }

Check warning on line 66 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ } } } - return $aggregations; + return count($aggregations) > 1 ? array_slice($aggregations, 0, 1, true) : $aggregations; } }

Check warning on line 66 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.2

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ } } } - return $aggregations; + return count($aggregations) > 1 ? array_slice($aggregations, 0, 1, true) : $aggregations; } }

Check warning on line 66 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8.1

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ } } } - return $aggregations; + return count($aggregations) > 1 ? array_slice($aggregations, 0, 1, true) : $aggregations; } }

Check warning on line 66 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.3

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ } } } - return $aggregations; + return count($aggregations) > 1 ? array_slice($aggregations, 0, 1, true) : $aggregations; } }

Check warning on line 66 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8.2

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ } } } - return $aggregations; + return count($aggregations) > 1 ? array_slice($aggregations, 0, 1, true) : $aggregations; } }

Check warning on line 66 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8.3

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ } } } - return $aggregations; + return count($aggregations) > 1 ? array_slice($aggregations, 0, 1, true) : $aggregations; } }

Check warning on line 66 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8.4

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ } } } - return $aggregations; + return count($aggregations) > 1 ? array_slice($aggregations, 0, 1, true) : $aggregations; } }
}
}

0 comments on commit 894d73f

Please sign in to comment.