75 lines
2.0 KiB
PHP
75 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\BusProNet\DataLoader;
|
|
|
|
use App\BusProNet\Model\Hotel;
|
|
use Psr\Cache\InvalidArgumentException;
|
|
use Symfony\Contracts\Cache\ItemInterface;
|
|
|
|
class HotelDataLoader extends AbstractDataLoader
|
|
{
|
|
public function loadAll(?string $filename = 'hotel.xml'): array
|
|
{
|
|
try {
|
|
return $this->cache->get('bpn_hotels', function (ItemInterface $item) use ($filename) {
|
|
$item->expiresAfter(3600);
|
|
|
|
$xml = $this->loadXml($filename);
|
|
|
|
$hotels = [];
|
|
|
|
foreach ($xml->hotel as $hotel) {
|
|
$id = (int)$hotel->attributes()['idbuspro'];
|
|
$hotels[$id] = $this->parseXml($hotel);
|
|
}
|
|
|
|
return $hotels;
|
|
});
|
|
} catch (InvalidArgumentException $e) {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
public function loadById(int $id, ?string $filename = 'hotel.xml'): ?Hotel
|
|
{
|
|
$hotels = $this->loadAll($filename);
|
|
|
|
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;
|
|
|
|
return simplexml_load_file($filepath);
|
|
}
|
|
|
|
public function parseXml(\SimpleXMLElement $xml): Hotel
|
|
{
|
|
$attributes = $xml->attributes();
|
|
|
|
$hotel = new Hotel();
|
|
$hotel->id = (int) $attributes['idbuspro'];
|
|
$hotel->code = (string) $attributes['code'];
|
|
$hotel->name = (string) $xml->name;
|
|
$hotel->city = $xml->ort ? (string) $xml->ort : null;
|
|
$hotel->country = (string) $xml->land;
|
|
$hotel->street = (string) $xml->strasse;
|
|
$hotel->phone = $xml->telefon ? (string) $xml->telefon : null;
|
|
|
|
return $hotel;
|
|
}
|
|
} |