From eae987951967a7731986129a09245db5aa93692f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Thu, 30 Jan 2025 10:40:38 +0100 Subject: [PATCH] feat: simplify xml parsing, add 'mutable' property for personal data --- src/BusProNet/Model/PersonalData.php | 1 + src/BusProNet/XmlParser/AbstractParser.php | 29 +++++++++++++++++++ src/BusProNet/XmlParser/BookingParser.php | 10 +++---- .../XmlParser/BookingUpdateParser.php | 2 +- src/BusProNet/XmlParser/BookingsParser.php | 17 ++++------- .../XmlParser/PersonalDataParser.php | 8 ++--- src/BusProNet/XmlParser/RoomsParser.php | 6 ++-- src/BusProNet/XmlParser/ServicesParser.php | 6 ++-- 8 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/BusProNet/Model/PersonalData.php b/src/BusProNet/Model/PersonalData.php index bf1e178..7a80a56 100644 --- a/src/BusProNet/Model/PersonalData.php +++ b/src/BusProNet/Model/PersonalData.php @@ -8,6 +8,7 @@ class PersonalData { public ?int $addressId = null; public ?int $personId = null; + public bool $mutable = false; #[Assert\NotBlank(message: 'Bitte angeben')] public ?string $name = null; diff --git a/src/BusProNet/XmlParser/AbstractParser.php b/src/BusProNet/XmlParser/AbstractParser.php index 9280151..49604f4 100644 --- a/src/BusProNet/XmlParser/AbstractParser.php +++ b/src/BusProNet/XmlParser/AbstractParser.php @@ -18,4 +18,33 @@ abstract class AbstractParser { return 0 < $node->count() ? (int) $node->text() : null; } + + protected function getFloatOrNullValue(Crawler $node): ?float + { + return 0 < $node->count() ? $this->stringToFloat($node->text()) : null; + } + + protected function getDateOrNullValue(Crawler $node): ?\DateTimeImmutable + { + return 0 < $node->count() ? $this->stringToDate($node->text()) : null; + } + + protected function getDateTimeOrNullValue(Crawler $node): ?\DateTimeImmutable + { + return 0 < $node->count() ? $this->stringToDateTime($node->text()) : null; + } + + protected function getBoolValue(Crawler $node): bool + { + return 0 < $node->count() && $this->stringToBool($node->text()); + } + + protected function getArrayValue(Crawler $node, string $separator = ','): array + { + if (0 === $node->count()) { + return []; + } + + return $this->stringToArray($node->text(), $separator); + } } \ No newline at end of file diff --git a/src/BusProNet/XmlParser/BookingParser.php b/src/BusProNet/XmlParser/BookingParser.php index 497fda3..67fffda 100644 --- a/src/BusProNet/XmlParser/BookingParser.php +++ b/src/BusProNet/XmlParser/BookingParser.php @@ -32,8 +32,7 @@ class BookingParser extends AbstractParser $booking->agencyId = $this->getIntOrNullValue($node->filterXPath('//idagentur')); $booking->bookingNumber = $this->getIntOrNullValue($node->filterXPath('//vorgang')); $booking->invoiceNumber = $this->getIntOrNullValue($node->filterXPath('//zahlungsdaten/rechnung')); - $booking->totalPrice = $this - ->stringToFloat($this->getStringOrNullValue($node->filterXPath('//zahlungsdaten/gesamtbetrag'))); + $booking->totalPrice = $this->getFloatOrNullValue($node->filterXPath('//zahlungsdaten/gesamtbetrag')); $booking->status = $this->getStringOrNullValue($node->filterXPath('//status')); $travelData = $node->filterXPath('//reise'); @@ -48,8 +47,7 @@ class BookingParser extends AbstractParser $booking->applicant = $this->parsePersonalData($node->filterXPath('//anmelder')); - $participantsStatus = $this - ->stringToArray($this->getStringOrNullValue($node->filterXPath('//status_teilnehmer')), '/'); + $participantsStatus = $this->getArrayValue($node->filterXPath('//status_teilnehmer'), '/'); $booking->participantsStatus = array_combine(range(1, count($participantsStatus)), $participantsStatus); $booking->participants = $this->parseParticipants($node->filterXPath('//teilnehmerliste/teilnehmer')); @@ -109,6 +107,7 @@ class BookingParser extends AbstractParser $personalData->addressId = $this->getIntOrNullValue($node->filterXPath('//idadresse')); $personalData->personId = $this->getIntOrNullValue($node->filterXPath('//idadresseperson')); + $personalData->mutable = $this->getBoolValue($node->filterXPath('//aenderungmoeglich')); $dateString = $this->getStringOrNullValue($node->filterXPath('//geburtsdatum')); $personalData->dateOfBirth = null !== $dateString @@ -145,8 +144,7 @@ class BookingParser extends AbstractParser $communication->phone = $this->getStringOrNullValue($contactNode->filterXPath('//telefonprivat')); $communication->mobile = $this->getStringOrNullValue($contactNode->filterXPath('//telefonmobil')); $communication->email = $this->getStringOrNullValue($contactNode->filterXPath('//email')); - $communication->newsletter = $this - ->stringToBool($contactNode->filterXPath('//newsletter')->text('False')); + $communication->newsletter = $this->getBoolValue($contactNode->filterXPath('//newsletter')); $personalData->communication = $communication; } diff --git a/src/BusProNet/XmlParser/BookingUpdateParser.php b/src/BusProNet/XmlParser/BookingUpdateParser.php index 20b4350..5c9e679 100644 --- a/src/BusProNet/XmlParser/BookingUpdateParser.php +++ b/src/BusProNet/XmlParser/BookingUpdateParser.php @@ -11,7 +11,7 @@ class BookingUpdateParser extends AbstractParser { $bookingUpdate = new BookingUpdate(); $bookingUpdate->valid = 'möglich' === $node->filterXPath('//aenderung')->text(); - $bookingUpdate->totalPrice = $this->stringToFloat($node->filterXPath('//gesamtpreis')->text()); + $bookingUpdate->totalPrice = $this->getFloatOrNullValue($node->filterXPath('//gesamtpreis')); return $bookingUpdate; } diff --git a/src/BusProNet/XmlParser/BookingsParser.php b/src/BusProNet/XmlParser/BookingsParser.php index cc56a58..cc0c75d 100644 --- a/src/BusProNet/XmlParser/BookingsParser.php +++ b/src/BusProNet/XmlParser/BookingsParser.php @@ -24,19 +24,14 @@ class BookingsParser extends AbstractParser $booking->bookingNumber = $this->getIntOrNullValue($node->filterXPath('//vorgangsnummer')); $booking->status = $this->getStringOrNullValue($node->filterXPath('//status')); $booking->participantCount = $this->getIntOrNullValue($node->filterXPath('//personen')); - $booking->price = $this->stringToFloat($this->getStringOrNullValue($node->filterXPath('//preis'))); - $booking->bookingDate = $this - ->stringToDateTime($this->getStringOrNullValue($node->filterXPath('//buchungsdatum'))); + $booking->price = $this->getFloatOrNullValue($node->filterXPath('//preis')); + $booking->bookingDate = $this->getDateTimeOrNullValue($node->filterXPath('//buchungsdatum')); $booking->travel = $this->getStringOrNullValue($node->filterXPath('//reise')); $booking->travelId = $this->getIntOrNullValue($node->filterXPath('//idreise')); - $booking->travelDate = $this - ->stringToDate($this->getStringOrNullValue($node->filterXPath('//reisedatum'))); - $booking->hasDocuments = $this - ->stringToBool($this->getStringOrNullValue($node->filterXPath('//reisedokument'))); - $booking->payment = $this - ->stringToFloat($this->getStringOrNullValue($node->filterXPath('//zahlung'))); - $booking->hasDocuments = $this - ->stringToBool($this->getStringOrNullValue($node->filterXPath('//reisedokument'))); + $booking->travelDate = $this->getDateOrNullValue($node->filterXPath('//reisedatum')); + $booking->hasDocuments = $this->getBoolValue($node->filterXPath('//reisedokument')); + $booking->payment = $this->getFloatOrNullValue($node->filterXPath('//zahlung')); + $booking->hasDocuments = $this->getBoolValue($node->filterXPath('//reisedokument')); $bookings[] = $booking; }); diff --git a/src/BusProNet/XmlParser/PersonalDataParser.php b/src/BusProNet/XmlParser/PersonalDataParser.php index 98ed9f0..1428dea 100644 --- a/src/BusProNet/XmlParser/PersonalDataParser.php +++ b/src/BusProNet/XmlParser/PersonalDataParser.php @@ -19,10 +19,7 @@ class PersonalDataParser extends AbstractParser $addressDataNode = $node->filterXPath('//adressdaten'); - $dateString = $this->getStringOrNullValue($addressDataNode->filterXPath('//geburtsdatum')); - $personalData->dateOfBirth = null !== $dateString ? - \DateTimeImmutable::createFromFormat('d.m.Y', $dateString) : null; - + $personalData->dateOfBirth = $this->getDateOrNullValue($addressDataNode->filterXPath('//geburtsdatum')); $personalData->firstName = $this->getStringOrNullValue($addressDataNode->filterXPath('//vorname')); $personalData->name = $this->getStringOrNullValue($addressDataNode->filterXPath('//name')); $personalData->salutation = $this->getStringOrNullValue($addressDataNode->filterXPath('//anrede')); @@ -52,8 +49,7 @@ class PersonalDataParser extends AbstractParser $communication->phone = $this->getStringOrNullValue($contactDataNode->filterXPath('//telefonprivat')); $communication->mobile = $this->getStringOrNullValue($contactDataNode->filterXPath('//telefonmobil')); $communication->email = $this->getStringOrNullValue($contactDataNode->filterXPath('//email')); - $communication->newsletter = $this - ->stringToBool($this->getStringOrNullValue($contactDataNode->filterXPath('//newsletter'))); + $communication->newsletter = $this->getBoolValue($contactDataNode->filterXPath('//newsletter')); $personalData->communication = $communication; } diff --git a/src/BusProNet/XmlParser/RoomsParser.php b/src/BusProNet/XmlParser/RoomsParser.php index bc89ce8..477be63 100644 --- a/src/BusProNet/XmlParser/RoomsParser.php +++ b/src/BusProNet/XmlParser/RoomsParser.php @@ -17,10 +17,8 @@ class RoomsParser extends AbstractParser $room->label = $node->attr('zimmer'); $room->category = $node->attr('kategorie'); $room->boardId = (int) $node->attr('idverpflegung'); - $room->dateFrom = $node->attr('anreise') ? - $this->stringToDate($node->attr('anreise')) : null; - $room->dateTo = $node->attr('abreise') ? - $this->stringToDate($node->attr('abreise')) : null; + $room->dateFrom = $this->stringToDate($node->attr('anreise')); + $room->dateTo = $this->stringToDate($node->attr('abreise')); $room->totalCount = (int) $node->attr('anzahl'); $room->minPax = (int) $node->attr('minpax'); $room->maxPax = (int) $node->attr('maxpax'); diff --git a/src/BusProNet/XmlParser/ServicesParser.php b/src/BusProNet/XmlParser/ServicesParser.php index b077312..f2cd87e 100644 --- a/src/BusProNet/XmlParser/ServicesParser.php +++ b/src/BusProNet/XmlParser/ServicesParser.php @@ -17,10 +17,8 @@ class ServicesParser extends AbstractParser $service->category = $category; $service->source = $source; $service->label = $node->attr('leistung'); - $service->dateFrom = $node->attr('termin') ? - $this->stringToDate($node->attr('termin')) : null; - $service->dateTo = $node->attr('terminbis') ? - $this->stringToDate($node->attr('terminbis')) : null; + $service->dateFrom = $this->stringToDate($node->attr('termin')); + $service->dateTo = $this->stringToDate($node->attr('terminbis')); $service->subType = $node->attr('unterart'); $service->totalCount = $node->attr('anzahl') ? (int) $node->attr('anzahl') : null; $mapping = $this->stringToArray($node->attr('zuordnung'));