-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Autocomplete] Add handling extra options in the EntityAutocompleteCo…
…ntroller
- Loading branch information
1 parent
90f57b3
commit 3d24e75
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,16 +18,20 @@ | |
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\UX\Autocomplete\AutocompleteResultsExecutor; | ||
use Symfony\UX\Autocomplete\AutocompleterRegistry; | ||
use Symfony\UX\Autocomplete\Calculator\ChecksumCalculatorInterface; | ||
|
||
/** | ||
* @author Ryan Weaver <[email protected]> | ||
*/ | ||
final class EntityAutocompleteController | ||
{ | ||
public const EXTRA_OPTIONS = 'extra_options'; | ||
|
||
public function __construct( | ||
private AutocompleterRegistry $autocompleteFieldRegistry, | ||
private AutocompleteResultsExecutor $autocompleteResultsExecutor, | ||
private UrlGeneratorInterface $urlGenerator, | ||
private ChecksumCalculatorInterface $checksumCalculator, | ||
) { | ||
} | ||
|
||
|
@@ -38,6 +42,11 @@ public function __invoke(string $alias, Request $request): Response | |
throw new NotFoundHttpException(sprintf('No autocompleter found for "%s". Available autocompleters are: (%s)', $alias, implode(', ', $this->autocompleteFieldRegistry->getAutocompleterNames()))); | ||
} | ||
|
||
$extraOptions = $this->getExtraOptions($request); | ||
$autocompleter = $autocompleter->withOptions([ | ||
'extra_options' => $extraOptions, | ||
]); | ||
|
||
$page = $request->query->getInt('page', 1); | ||
$nextPage = null; | ||
|
||
|
@@ -54,4 +63,26 @@ public function __invoke(string $alias, Request $request): Response | |
'next_page' => $nextPage, | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<string, scalar|array|null> | ||
*/ | ||
private function getExtraOptions(Request $request): array | ||
{ | ||
// TODO: This method should be refactored | ||
if (!$request->query->has(self::EXTRA_OPTIONS)) { | ||
return []; | ||
} | ||
|
||
/** @var array $extraOptions */ | ||
$extraOptions = json_decode(base64_decode($request->query->get(self::EXTRA_OPTIONS)), true); | ||
$checksum = $extraOptions['@checksum'] ?? null; | ||
unset($extraOptions['@checksum']); | ||
|
||
if ($checksum !== $this->checksumCalculator->calculateForArray($extraOptions)) { | ||
throw new \InvalidArgumentException('The extra options have been tampered with.'); | ||
} | ||
|
||
return $extraOptions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters