. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Module\ModuleMapAutocompleteInterface; use Fisharebest\Webtrees\Place; use Fisharebest\Webtrees\Services\ModuleService; use Fisharebest\Webtrees\Services\SearchService; use Fisharebest\Webtrees\Site; use Fisharebest\Webtrees\Tree; use GuzzleHttp\Client; use GuzzleHttp\Exception\RequestException; use Illuminate\Support\Collection; use Iodev\Whois\Modules\Module; use Psr\Http\Message\ServerRequestInterface; use function assert; use function is_array; use function json_decode; use function rawurlencode; use const JSON_THROW_ON_ERROR; /** * Autocomplete handler for places */ class AutoCompletePlace extends AbstractAutocompleteHandler { // Options for fetching files using GuzzleHTTP private const GUZZLE_OPTIONS = [ 'connect_timeout' => 3, 'read_timeout' => 3, 'timeout' => 3, ]; 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->getAttribute('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; } }