feat: API endpoint to load travel data by code

This commit is contained in:
Björn Fromme
2025-01-14 12:41:40 +01:00
parent b196825ab3
commit c96b525aa4
6 changed files with 82 additions and 21 deletions
+13 -13
View File
@@ -31,6 +31,19 @@ class HotelXmlLoader extends AbstractXmlLoader
}
}
public function mapCodeToId(string $hotelCode, ?string $filename = 'hotel.xml'): ?int
{
$hotels = $this->loadAll($filename);
foreach ($hotels as $hotel) {
if ($hotelCode === $hotel->code) {
return $hotel->id;
}
}
return null;
}
public function loadById(int $id, ?string $filename = 'hotel.xml'): ?Hotel
{
$hotels = $this->loadAll($filename);
@@ -38,19 +51,6 @@ class HotelXmlLoader extends AbstractXmlLoader
return $hotels[$id] ?? null;
}
public function loadByCode(string $code, ?string $filename = 'hotel.xml'): ?Hotel
{
$hotels = $this->loadAll($filename);
foreach ($hotels as $hotel) {
if ($code === $hotel->code) {
return $hotel;
}
}
return null;
}
private function loadXml(?string $filename = 'hotel.xml'): \SimpleXMLElement
{
$filepath = $this->xmlPath.'/'.$filename;
+18 -3
View File
@@ -25,7 +25,7 @@ class TravelXmlLoader extends AbstractXmlLoader
parent::__construct($cache, $xmlPath);
}
public function generateMapping(): array
public function generateFilesMap(): array
{
return $this->cache->get('bpn_travels_mapping', function (ItemInterface $item) {
$item->expiresAfter(3600);
@@ -46,6 +46,7 @@ class TravelXmlLoader extends AbstractXmlLoader
$mapping[$travelId] = [
'id' => (int) $attributes['idbuspro'],
'code' => (string) $attributes['code'],
'label' => (string) $travelXml->text,
'dateFrom' => $this->stringToDate((string) $attributes['termin']),
'dateTo' => $this->stringToDate((string) $attributes['bis']),
@@ -58,6 +59,7 @@ class TravelXmlLoader extends AbstractXmlLoader
$mapping[$travelId]['hotels'][$hotelId] = [
'id' => $hotelId,
'code' => (string) $hotelXml->attributes()['code'],
'label' => $hotels[$hotelId]->name,
];
}
@@ -68,13 +70,26 @@ class TravelXmlLoader extends AbstractXmlLoader
});
}
public function mapCodeToId(string $travelCode): ?int
{
$mapping = $this->generateFilesMap();
$travelCodes = array_column($mapping, 'code', 'id');
if (false === $travelId = array_search($travelCode, $travelCodes)) {
return null;
}
return $travelId;
}
public function loadById(int $travelId, ?int $hotelId = null, ?string $filename = null): ?Travel
{
if (null !== $filename) {
return $this->loadXml($travelId, $hotelId, $filename);
}
$mapping = $this->generateMapping();
$mapping = $this->generateFilesMap();
if (false === isset($mapping[$travelId])) {
return null;
@@ -107,7 +122,7 @@ class TravelXmlLoader extends AbstractXmlLoader
$travel = new Travel();
$travel->id = (int) $attributes['idbuspro'];
$travel->label = (string) $xml->text;
$travel->hotelId = $hotelId;
$travel->hotelId = (int) $hotelXml->attributes()['idbuspro'];
$travel->dateFrom = $this->stringToDate((string) $attributes['termin']);
$travel->dateTo = $this->stringToDate((string) $attributes['bis']);
$travel->code = (string) $attributes['code'];