Skip to content

Commit 1e5362e

Browse files
Fix server crash when users search included empty strings.
1 parent aa7b060 commit 1e5362e

File tree

2 files changed

+29
-33
lines changed

2 files changed

+29
-33
lines changed

src/Controller/SuggestLocationController.php

+16-11
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public function __construct(LoggerInterface $logger, TranslatorInterface $transl
2828
public function suggestExactPlaces(Request $request, SuggestLocationModel $model): JsonResponse
2929
{
3030
$response = new JsonResponse();
31-
$searchTerm = $request->query->get('term', '');
31+
$searchTerms = $this->splitElements($request->query->get('term', ''));
3232

33-
$this->logSearchInfo(__METHOD__, $searchTerm);
33+
$this->logSearchInfo(__METHOD__, $searchTerms);
3434

35-
$result = $model->getSuggestionsForPlacesExact($searchTerm);
35+
$result = $model->getSuggestionsForPlacesExact($searchTerms);
3636
$response->setData($result);
3737

3838
return $response;
@@ -44,11 +44,11 @@ public function suggestExactPlaces(Request $request, SuggestLocationModel $model
4444
public function suggestPlaces(Request $request, SuggestLocationModel $model): JsonResponse
4545
{
4646
$response = new JsonResponse();
47-
$searchTerm = $request->query->get('term', '');
47+
$searchTerms = $this->splitElements($request->query->get('term', ''));
4848

49-
$this->logSearchInfo(__METHOD__, $searchTerm);
49+
$this->logSearchInfo(__METHOD__, $searchTerms);
5050

51-
$result = $model->getSuggestionsForPlaces($searchTerm);
51+
$result = $model->getSuggestionsForPlaces($searchTerms);
5252
$response->setData($result);
5353

5454
return $response;
@@ -63,18 +63,23 @@ public function suggestPlaces(Request $request, SuggestLocationModel $model): Js
6363
public function suggestLocations(Request $request, SuggestLocationModel $model): JsonResponse
6464
{
6565
$response = new JsonResponse();
66-
$searchTerm = $request->query->get('term', '');
66+
$searchTerms = $this->splitElements($request->query->get('term', ''));
6767

68-
$this->logSearchInfo(__METHOD__, $searchTerm);
68+
$this->logSearchInfo(__METHOD__, $searchTerms);
6969

70-
$result = $model->getSuggestionsForLocations($searchTerm);
70+
$result = $model->getSuggestionsForLocations($searchTerms);
7171
$response->setData($result);
7272

7373
return $response;
7474
}
7575

76-
private function logSearchInfo(string $function, string $searchTerm)
76+
private function logSearchInfo(string $function, array $searchTerms)
7777
{
78-
$this->logger->alert($function, ['locale' => $this->translator->getLocale(), 'searchTerm' => $searchTerm]);
78+
$this->logger->alert($function, ['locale' => $this->translator->getLocale(), 'searchTerms' => $searchTerms]);
79+
}
80+
81+
private function splitElements(string $searchTerm): array
82+
{
83+
return array_filter(array_map('trim', explode(',', $searchTerm)), 'strlen');
7984
}
8085
}

src/Model/SuggestLocationModel.php

+13-22
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,24 @@ public function __construct(
4343
$this->manticorePort = $manticorePort;
4444
}
4545

46-
public function getSuggestionsForPlaces(string $term): array
46+
public function getSuggestionsForPlaces(array $terms): array
4747
{
48-
$places = $this->getPlaces($term, 25);
48+
$places = $this->getPlaces($terms, 25);
4949

5050
return ['locations' => $places];
5151
}
5252

53-
public function getSuggestionsForPlacesExact(string $term): array
53+
public function getSuggestionsForPlacesExact(array $terms): array
5454
{
55-
return ['locations' => $this->getPlacesExact($term, 100)];
55+
return ['locations' => $this->getPlacesExact($terms, 100)];
5656
}
5757

58-
public function getSuggestionsForLocations(string $term): array
58+
public function getSuggestionsForLocations(array $terms): array
5959
{
60-
$placesExact = $this->getPlacesExact($term, 10, self::EXACT_PLACE);
61-
$places = $this->getPlaces($term, 20);
62-
$adminUnits = $this->getAdminUnits($term, 10);
63-
$countries = $this->getCountries($term, 5);
60+
$placesExact = $this->getPlacesExact($terms, 10, self::EXACT_PLACE);
61+
$places = $this->getPlaces($terms, 20);
62+
$adminUnits = $this->getAdminUnits($terms, 10);
63+
$countries = $this->getCountries($terms, 5);
6464
$results = array_merge($placesExact, $places, $adminUnits, $countries);
6565

6666
return ['locations' => $this->removeDuplicates('id', $results)];
@@ -71,10 +71,8 @@ public function getSuggestionsForLocations(string $term): array
7171
*
7272
* place[[, admin unit], country]
7373
*/
74-
private function getPlacesExact(string $term, int $limit, ?string $translationId = null): array
74+
private function getPlacesExact(array $parts, int $limit, ?string $translationId = null): array
7575
{
76-
$parts = array_map('trim', explode(',', $term));
77-
7876
$countryId = '';
7977
$adminUnits = [];
8078
if (1 < count($parts)) {
@@ -128,10 +126,8 @@ private function getPlacesExact(string $term, int $limit, ?string $translationId
128126
return $this->getLocationDetails($results, $translationId);
129127
}
130128

131-
private function getPlaces(string $term, int $limit): array
129+
private function getPlaces(array $parts, int $limit): array
132130
{
133-
$parts = array_map('trim', explode(',', $term));
134-
135131
$countryId = '';
136132
$adminUnits = [];
137133
if (1 < count($parts)) {
@@ -181,10 +177,8 @@ private function getPlaces(string $term, int $limit): array
181177
return $this->getLocationDetails($results, self::PLACE);
182178
}
183179

184-
private function getAdminUnits(string $term, int $limit = 10): array
180+
private function getAdminUnits(array $parts, int $limit = 10): array
185181
{
186-
$parts = array_map('trim', explode(',', $term));
187-
188182
$countryId = '';
189183
$adminUnits = [];
190184
if (1 < count($parts)) {
@@ -232,10 +226,8 @@ private function getAdminUnits(string $term, int $limit = 10): array
232226
return $this->getLocationDetails($results, self::ADMIN_UNIT);
233227
}
234228

235-
private function getCountries(string $term, int $limit = 3): array
229+
private function getCountries(array $parts, int $limit = 3): array
236230
{
237-
$parts = array_map('trim', explode(',', $term));
238-
239231
if (1 !== count($parts)) {
240232
return [];
241233
}
@@ -445,7 +437,6 @@ private function getManticoreResults(Search $query, int $limit = 1000): array
445437

446438
private function getQueryForGeonamesRt(): Search
447439
{
448-
$locale = $this->translator->getLocale();
449440
$config = ['host' => $this->manticoreHost,'port' => $this->manticorePort];
450441
$client = new Client($config);
451442
$query = new Search($client);

0 commit comments

Comments
 (0)