. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fisharebest\Webtrees\Module\ModuleMapAutocompleteInterface; use Fisharebest\Webtrees\Place; use Fisharebest\Webtrees\Services\ModuleService; use Fisharebest\Webtrees\Services\SearchService; use Fisharebest\Webtrees\Tree; use Illuminate\Support\Collection; use Psr\Http\Message\ServerRequestInterface; use function assert; /** * Autocomplete handler for places */ class AutoCompletePlace extends AbstractAutocompleteHandler { private ModuleService $module_service; /** * @param SearchService $search_service */ public function __construct(SearchService $search_service, ModuleService $module_service) { parent::__construct($search_service); $this->module_service = $module_service; } /** * @param ServerRequestInterface $request * * @return Collection */ protected function search(ServerRequestInterface $request): Collection { $tree = $request->getAttribute('tree'); assert($tree instanceof Tree); $query = $request->getQueryParams()['query'] ?? ''; $data = $this->search_service ->searchPlaces($tree, $query, 0, static::LIMIT) ->map(static function (Place $place): string { return $place->gedcomName(); }); // No place found? Use external gazetteers. foreach ($this->module_service->findByInterface(ModuleMapAutocompleteInterface::class) as $module) { if ($data->isEmpty()) { $data = $data->concat($module->searchPlaceNames($query))->sort(); } } return $data; } }