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; } }