Skip to content

Commit 6a5a54f

Browse files
committed
Fix search; Add alternate names to api response
1 parent 17f7d7a commit 6a5a54f

File tree

10 files changed

+230
-15
lines changed

10 files changed

+230
-15
lines changed

data/json/schema/command/coordinate/resource.json

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
"geoname-id": 6295497,
55
"name": "Zürich (Kreis 3) / Friesenberg",
66
"name-full": "Zürich (Kreis 3) / Friesenberg, Zürich, Zürich, Schweiz",
7+
"alternate-names": [
8+
"Friesenberg"
9+
],
710
"zip-code": "8055",
8-
"rivers": [],
911
"updated-at": "2023-08-21T23:08:34+00:00",
1012
"properties": {
1113
"population": {
@@ -58,10 +60,10 @@
5860
"timezone": "Europe/Zurich",
5961
"country": "CH",
6062
"current-time": {
61-
"timezone": "2024-03-20T23:53:17+01:00",
62-
"utc": "2024-03-20T22:53:17+00:00"
63+
"timezone": "2024-04-29T23:18:52+02:00",
64+
"utc": "2024-04-29T21:18:52+00:00"
6365
},
64-
"offset": "+01:00",
66+
"offset": "+02:00",
6567
"coordinate": {
6668
"latitude": {
6769
"decimal": 47.38333,
@@ -174,7 +176,7 @@
174176
]
175177
},
176178
"waters": {
177-
"distance": 100000,
179+
"distance": 20000,
178180
"limit": 10,
179181
"name": "lakes, rivers",
180182
"feature_codes": [
@@ -221,6 +223,9 @@
221223
"district-locality": {
222224
"geoname-id": 6295497,
223225
"name": "Zürich (Kreis 3) / Friesenberg",
226+
"alternate-names": [
227+
"Friesenberg"
228+
],
224229
"updated-at": "2023-08-21T23:08:34+00:00",
225230
"coordinate": {
226231
"latitude": {
@@ -346,7 +351,7 @@
346351
]
347352
},
348353
"waters": {
349-
"distance": 100000,
354+
"distance": 20000,
350355
"limit": 10,
351356
"name": "lakes, rivers",
352357
"feature_codes": [
@@ -393,6 +398,12 @@
393398
"city-municipality": {
394399
"geoname-id": 2657896,
395400
"name": "Zürich",
401+
"alternate-names": [
402+
"Zürich",
403+
"Zurich",
404+
"Zúrich",
405+
"Zuerich"
406+
],
396407
"updated-at": "2023-08-21T23:08:27+00:00",
397408
"coordinate": {
398409
"latitude": {
@@ -518,7 +529,7 @@
518529
]
519530
},
520531
"waters": {
521-
"distance": 100000,
532+
"distance": 20000,
522533
"limit": 10,
523534
"name": "lakes, rivers",
524535
"feature_codes": [
@@ -565,6 +576,15 @@
565576
"state": {
566577
"geoname-id": 2657895,
567578
"name": "Zürich",
579+
"alternate-names": [
580+
"Zürich",
581+
"Kanton Zürich",
582+
"Canton of Zurich",
583+
"Zurich",
584+
"Canton de Zurich",
585+
"Zurigo",
586+
"Region Zürich"
587+
],
568588
"updated-at": "2023-08-21T23:08:27+00:00",
569589
"coordinate": {
570590
"latitude": {
@@ -690,7 +710,7 @@
690710
]
691711
},
692712
"waters": {
693-
"distance": 100000,
713+
"distance": 20000,
694714
"limit": 10,
695715
"name": "lakes, rivers",
696716
"feature_codes": [
@@ -737,6 +757,17 @@
737757
"country": {
738758
"geoname-id": 2658434,
739759
"name": "Schweiz",
760+
"alternate-names": [
761+
"Schweizerische Eidgenossenschaft",
762+
"Schweiz",
763+
"Svizzera",
764+
"Helvetia",
765+
"Swiss Confederation",
766+
"Confederatio Helvetica",
767+
"Switzerland",
768+
"Schwiz",
769+
"Suisse"
770+
],
740771
"updated-at": "2023-08-21T23:08:27+00:00",
741772
"coordinate": {
742773
"latitude": {
@@ -862,7 +893,7 @@
862893
]
863894
},
864895
"waters": {
865-
"distance": 100000,
896+
"distance": 20000,
866897
"limit": 10,
867898
"name": "lakes, rivers",
868899
"feature_codes": [
@@ -929,9 +960,9 @@
929960
}
930961
}
931962
},
932-
"time-taken": "422 ms",
933-
"memory-taken": "58 MB",
934-
"version": "0.1.58",
963+
"time-taken": "1339 ms",
964+
"memory-taken": "59 MB",
965+
"version": "0.1.83",
935966
"data-licence": {
936967
"full": "Creative Commons Attribution 4.0 License",
937968
"short": "CC-BY 4.0",

data/json/schema/command/coordinate/resource.schema.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,23 @@
1515
"name": {
1616
"type": "string"
1717
},
18+
"alternate-names": {
19+
"type": "array",
20+
"items": {
21+
"type": "string"
22+
}
23+
},
1824
"name-full": {
1925
"type": "string"
2026
},
2127
"zip-code": {
2228
"type": "string"
2329
},
2430
"rivers": {
25-
"type": "array"
31+
"type": "array",
32+
"items": {
33+
"type": "string"
34+
}
2635
},
2736
"updated-at": {
2837
"$ref": "#/$defs/updatedAt"
@@ -590,6 +599,12 @@
590599
"name": {
591600
"type": "string"
592601
},
602+
"alternate-names": {
603+
"type": "array",
604+
"items": {
605+
"type": "string"
606+
}
607+
},
593608
"zip-code": {
594609
"type": "string"
595610
},

src/ApiPlatform/Resource/Base/LocationBase.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ abstract class LocationBase extends BasePublicResource
5858
#[SerializedName('name-full')]
5959
private string $nameFull;
6060

61+
/** @var string[] $alternateNames */
62+
#[SerializedName('alternate-names')]
63+
private array $alternateNames;
64+
6165
#[SerializedName('zip-code')]
6266
private string $zipCode;
6367

@@ -163,6 +167,25 @@ public function setNameFull(string $nameFull): self
163167
return $this;
164168
}
165169

170+
/**
171+
* @return string[]
172+
*/
173+
public function getAlternateNames(): array
174+
{
175+
return $this->alternateNames;
176+
}
177+
178+
/**
179+
* @param string[] $alternateNames
180+
* @return LocationBase
181+
*/
182+
public function setAlternateNames(array $alternateNames): LocationBase
183+
{
184+
$this->alternateNames = $alternateNames;
185+
186+
return $this;
187+
}
188+
166189
/**
167190
* @return string
168191
*/

src/ApiPlatform/State/LocationProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ private function doProvideGetWithCoordinateSchema(): BasePublicResource
573573
* @throws NonUniqueResultException
574574
* @throws ORMException
575575
* @throws ParserException
576+
* @throws QueryParserException
576577
* @throws RedirectionExceptionInterface
577578
* @throws ServerExceptionInterface
578579
* @throws TransportExceptionInterface

src/Constants/Key/KeyArray.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class KeyArray
2626

2727
final public const AIRPORT_CODES = 'airport_codes';
2828

29-
final public const ALTERNATE_NAMES = 'alternate_names';
29+
final public const ALTERNATE_NAMES = 'alternate-names';
3030

3131
final public const BOROUGH_LOCALITY = 'borough-locality';
3232

src/Repository/AlternateNameRepository.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,35 @@ public function findByIsoLanguage(Location $location, string $isoLanguage): arra
8383
);
8484
}
8585

86+
/**
87+
* Returns an array of AlternateName objects
88+
*
89+
* @param Location $location
90+
* @param array<int, string|null> $isoLanguages
91+
* @return AlternateName[]
92+
* @throws ClassInvalidException
93+
* @throws TypeInvalidException
94+
*/
95+
public function findByIsoLanguagesWithNull(Location $location, array $isoLanguages): array
96+
{
97+
$queryBuilder = $this->createQueryBuilder('a')
98+
->andWhere('a.location = :location')
99+
->setParameter('location', $location)
100+
101+
->andWhere('a.isoLanguage IN (:isoLanguages) OR a.isoLanguage IS NULL')
102+
->setParameter('isoLanguages', $isoLanguages)
103+
104+
->orderBy('a.id', 'ASC')
105+
->setMaxResults(10)
106+
;
107+
108+
/* Returns the result. */
109+
return array_values(
110+
(new CheckerArray($queryBuilder->getQuery()->getResult()))
111+
->checkClass(AlternateName::class)
112+
);
113+
}
114+
86115
/**
87116
* Returns the first AlternateName object.
88117
*

src/Repository/Base/BaseCoordinateRepository.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
use App\Repository\LocationRepository;
2020
use App\Repository\ZipCodeAreaRepository;
2121
use App\Repository\ZipCodeRepository;
22+
use App\Utils\Db\DebugNativeQuery;
2223
use App\Utils\Db\DebugQuery;
2324
use App\Utils\Doctrine\QueryBuilder as UtilQueryBuilder;
2425
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
26+
use Doctrine\ORM\NativeQuery;
2527
use Doctrine\ORM\QueryBuilder;
2628
use Doctrine\Persistence\ManagerRegistry;
2729
use Ixnode\PhpException\Case\CaseUnsupportedException;
@@ -140,4 +142,19 @@ protected function debugQuery(QueryBuilder $queryBuilder): void
140142
print $debugQuery->getSqlRaw();
141143
exit();
142144
}
145+
146+
/**
147+
* Prints the raw query from given native builder.
148+
*
149+
* @param NativeQuery $nativeQuery
150+
* @return void
151+
* @SuppressWarnings(PHPMD.ExitExpression)
152+
*/
153+
#[NoReturn]
154+
protected function debugNativeQuery(NativeQuery $nativeQuery): void
155+
{
156+
$debugNativeQuery = new DebugNativeQuery($nativeQuery);
157+
print $debugNativeQuery->getSqlRaw();
158+
exit();
159+
}
143160
}

src/Service/Base/BaseLocationService.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use App\DataTypes\Timezone;
3434
use App\DBAL\GeoLocation\ValueObject\Point;
3535
use App\Entity\AlternateName;
36+
use App\Entity\Location;
3637
use App\Entity\Location as LocationEntity;
3738
use App\Entity\ZipCode;
3839
use App\Entity\ZipCodeArea;
@@ -131,6 +132,7 @@ protected function getLocationResourceSimple(
131132
$key === KeyArray::GEONAME_ID => $location->setGeonameId(is_int($value) ? $value : 0),
132133
$key === KeyArray::NAME => $location->setName(is_string($value) ? $value : ''),
133134
$key === KeyArray::NAME_FULL => $location->setNameFull(is_string($value) ? $value : ''),
135+
$key === KeyArray::ALTERNATE_NAMES => $location->setAlternateNames(is_array($value) ? $value : []),
134136
$key === KeyArray::ZIP_CODE => $location->setZipCode(is_string($value) ? $value : null),
135137
$key === KeyArray::UPDATED_AT => $value instanceof DateTimeImmutable ? $location->setUpdatedAt($value) : null,
136138

@@ -1002,6 +1004,7 @@ private function getLocationBaseInformation(
10021004
/* Single fields. */
10031005
...(is_int($geonameId) ? [KeyArray::GEONAME_ID => $geonameId] : []),
10041006
...(is_string($name) ? [KeyArray::NAME => $name] : []),
1007+
...(is_string($name) ? [KeyArray::ALTERNATE_NAMES => $this->getAlternateNames($locationEntity)] : []),
10051008
...(is_string($nameFull) ? [KeyArray::NAME_FULL => $nameFull] : []),
10061009
...(!is_null($zipCodeString) ? [KeyArray::ZIP_CODE => $zipCodeString] : []),
10071010
...(!is_null($updateAt) ? [KeyArray::UPDATED_AT => $updateAt] : []),
@@ -1035,6 +1038,7 @@ private function getLocationBaseInformation(
10351038
* @return LocationEntity|null
10361039
* @throws CaseUnsupportedException
10371040
* @throws ClassInvalidException
1041+
* @throws ParserException
10381042
* @throws TypeInvalidException
10391043
*/
10401044
public function getLocationEntityByCoordinate(CoordinateIxnode $coordinate): LocationEntity|null
@@ -1254,4 +1258,31 @@ private function getLocationKey(string $locationType): string
12541258
default => throw new LogicException(sprintf('Invalid location type given: "%s"', $locationType)),
12551259
};
12561260
}
1261+
1262+
/**
1263+
* Return de, en, es alternate names.
1264+
*
1265+
* @param LocationEntity $locationEntity
1266+
* @return string[]
1267+
* @throws ClassInvalidException
1268+
* @throws TypeInvalidException
1269+
*/
1270+
private function getAlternateNames(Location $locationEntity): array
1271+
{
1272+
$alternateNames = [];
1273+
1274+
foreach ($this->alternateNameRepository->findByIsoLanguagesWithNull($locationEntity, ['de', 'en', 'es']) as $alternateName) {
1275+
$name = $alternateName->getAlternateName();
1276+
1277+
if (is_null($name)) {
1278+
continue;
1279+
}
1280+
1281+
$alternateNames[] = $name;
1282+
}
1283+
1284+
$alternateNames = array_unique($alternateNames);
1285+
1286+
return array_values($alternateNames);
1287+
}
12571288
}

0 commit comments

Comments
 (0)