Skip to content

Commit 0711e7a

Browse files
committed
refactor: add optgroups on AutocompleteResults
1 parent 202ccce commit 0711e7a

File tree

4 files changed

+30
-34
lines changed

4 files changed

+30
-34
lines changed

src/Autocomplete/src/AutocompleteResults.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
final class AutocompleteResults
1515
{
1616
/**
17-
* @param list<array{label: string, value: mixed}> $results
17+
* @param list<array{text: string, value: mixed, group_by?: string}> $options
18+
* @param list<array{label: string, value: string}> $optgroups
1819
*/
1920
public function __construct(
20-
public array $results,
21+
public array $options,
22+
public array $optgroups,
2123
public bool $hasNextPage,
2224
) {
2325
}

src/Autocomplete/src/AutocompleteResultsExecutor.php

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@ public function fetchResults(EntityAutocompleterInterface $autocompleter, string
5858

5959
$nbPages = (int) ceil($paginator->count() / $queryBuilder->getMaxResults());
6060

61-
$results = [];
62-
6361
$options = [];
64-
$optgroupLabels = [];
62+
$optgroups = [];
6563

6664
if (null !== $groupBy = $autocompleter->getGroupBy()) {
6765
if (\is_string($groupBy)) {
@@ -78,42 +76,38 @@ public function fetchResults(EntityAutocompleterInterface $autocompleter, string
7876
}
7977
};
8078
}
79+
}
8180

82-
if (\is_callable($groupBy)) {
83-
foreach ($paginator as $entity) {
84-
$option = [
85-
'value' => $autocompleter->getValue($entity),
86-
'text' => $autocompleter->getLabel($entity),
87-
];
81+
if (\is_callable($groupBy)) {
82+
$optgroupLabels = [];
8883

89-
$groupLabels = $groupBy($entity, $option['value'], $option['text']);
84+
foreach ($paginator as $entity) {
85+
$option = [
86+
'value' => $autocompleter->getValue($entity),
87+
'text' => $autocompleter->getLabel($entity),
88+
];
9089

91-
if (null !== $groupLabels) {
92-
$groupLabels = \is_array($groupLabels) ? array_map('strval', $groupLabels) : [(string) $groupLabels];
93-
$option['group_by'] = $groupLabels;
94-
$optgroupLabels = array_merge($optgroupLabels, $groupLabels);
95-
}
90+
$groupLabels = $groupBy($entity, $option['value'], $option['text']);
9691

97-
$options[] = $option;
92+
if (null !== $groupLabels) {
93+
$groupLabels = \is_array($groupLabels) ? array_map('strval', $groupLabels) : [(string) $groupLabels];
94+
$option['group_by'] = $groupLabels;
95+
$optgroupLabels = array_merge($optgroupLabels, $groupLabels);
9896
}
9997

100-
$results = [
101-
'options' => $options,
102-
'optgroups' => array_map(fn (string $label) => ['value' => $label, 'label' => $label],
103-
array_unique($optgroupLabels)),
104-
];
98+
$options[] = $option;
10599
}
100+
101+
$optgroups = array_map(fn (string $label) => ['value' => $label, 'label' => $label], array_unique($optgroupLabels));
106102
} else {
107103
foreach ($paginator as $entity) {
108104
$options[] = [
109105
'value' => $autocompleter->getValue($entity),
110106
'text' => $autocompleter->getLabel($entity),
111107
];
112108
}
113-
114-
$results = ['options' => $options];
115109
}
116110

117-
return new AutocompleteResults($results, $page < $nbPages);
111+
return new AutocompleteResults($options, $optgroups, $page < $nbPages);
118112
}
119113
}

src/Autocomplete/src/Controller/EntityAutocompleteController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __invoke(string $alias, Request $request): Response
5252
}
5353

5454
return new JsonResponse([
55-
'results' => $data->results,
55+
'results' => ['options' => $data->options, 'optgroups' => $data->optgroups],
5656
'next_page' => $nextPage,
5757
]);
5858
}

src/Autocomplete/tests/Integration/WiringTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testWiringWithoutForm(): void
4747
$executor = $kernel->getContainer()->get('public.results_executor');
4848
$autocompleter = $kernel->getContainer()->get(CustomProductAutocompleter::class);
4949
$data = $executor->fetchResults($autocompleter, '', 1);
50-
$this->assertCount(3, $data->results['options']);
50+
$this->assertCount(3, $data->options);
5151
$this->assertFalse($data->hasNextPage);
5252
}
5353

@@ -63,16 +63,16 @@ public function testWiringWithManyResults(): void
6363
$executor = $kernel->getContainer()->get('public.results_executor');
6464
$autocompleter = $kernel->getContainer()->get(CustomProductAutocompleter::class);
6565
$data = $executor->fetchResults($autocompleter, '', 1);
66-
$this->assertCount(10, $data->results['options']);
66+
$this->assertCount(10, $data->options);
6767
$this->assertTrue($data->hasNextPage);
6868
$data = $executor->fetchResults($autocompleter, '', 2);
69-
$this->assertCount(10, $data->results['options']);
69+
$this->assertCount(10, $data->options);
7070
$this->assertTrue($data->hasNextPage);
7171
$data = $executor->fetchResults($autocompleter, '', 3);
72-
$this->assertCount(2, $data->results['options']);
72+
$this->assertCount(2, $data->options);
7373
$this->assertFalse($data->hasNextPage);
7474
$data = $executor->fetchResults($autocompleter, '', 4);
75-
$this->assertCount(0, $data->results['options']);
75+
$this->assertCount(0, $data->options);
7676
$this->assertFalse($data->hasNextPage);
7777
}
7878

@@ -92,7 +92,7 @@ public function testWiringWithoutFormAndGroupByOption(): void
9292
$executor = $kernel->getContainer()->get('public.results_executor');
9393
$autocompleter = $kernel->getContainer()->get(CustomGroupByProductAutocompleter::class);
9494
$data = $executor->fetchResults($autocompleter, '', 1);
95-
$this->assertCount(3, $data->results['options']);
96-
$this->assertCount(2, $data->results['optgroups']);
95+
$this->assertCount(3, $data->options);
96+
$this->assertCount(2, $data->optgroups);
9797
}
9898
}

0 commit comments

Comments
 (0)