wip: initial commit
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\BusProNet\DataLoader;
|
||||
|
||||
use App\BusProNet\Model\Pickup;
|
||||
use App\BusProNet\Model\Travel;
|
||||
use Psr\Cache\InvalidArgumentException;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
class PickupDataLoader extends AbstractDataLoader
|
||||
{
|
||||
public function loadAll(?string $filename = 'zustiege.xml'): array
|
||||
{
|
||||
try {
|
||||
return $this->cache->get('bpn_pickups', function (ItemInterface $item) use ($filename) {
|
||||
$item->expiresAfter(3600);
|
||||
|
||||
$xml = simplexml_load_file($this->xmlPath . '/' . $filename);
|
||||
|
||||
$pickups = [];
|
||||
|
||||
foreach ($xml->zustieg as $pickup) {
|
||||
$id = (int)$pickup->attributes()['idbuspro'];
|
||||
$pickups[$id] = $this->parseXml($pickup);
|
||||
}
|
||||
|
||||
return $pickups;
|
||||
});
|
||||
} catch (InvalidArgumentException $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
public function loadById(int $id, ?string $filename = 'zustiege.xml'): ?Pickup
|
||||
{
|
||||
$pickups = $this->loadAll($filename);
|
||||
|
||||
return $pickups[$id] ?? null;
|
||||
}
|
||||
|
||||
public function parseXml(\SimpleXMLElement $xml): Pickup
|
||||
{
|
||||
$attributes = $xml->attributes();
|
||||
|
||||
$pickup = new Pickup();
|
||||
$pickup->id = (int) $attributes['idbuspro'];
|
||||
$pickup->code = (string) $attributes['code'];
|
||||
$pickup->city = (string) $xml->ort;
|
||||
$pickup->postalCode = (string) $xml->plz;
|
||||
$pickup->street = (string) $xml->strasse;
|
||||
|
||||
return $pickup;
|
||||
}
|
||||
|
||||
public function enrichPickupsData(Travel $travel): void
|
||||
{
|
||||
foreach ($travel->pickups as $pickupId => $pickup) {
|
||||
$pickupData = $this->loadById($pickupId);
|
||||
|
||||
$pickup->code = $pickupData->code;
|
||||
$pickup->postalCode = $pickupData->postalCode;
|
||||
$pickup->city = $pickupData->city;
|
||||
$pickup->street = $pickupData->street;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user