feat: harden xml parsing to avoid uncaught errors

This commit is contained in:
Björn Fromme committed 2026-03-20 12:14:02 +01:00
1 parent 3849ae306c
commit 3234c78f0d
9 files changed
+229 -147

No files matched your search

@@ -32,7 +32,11 @@ class BookingResponseParser extends AbstractParser
{
public function parse(Crawler $node): BookingResponse
{
$status = $node->filterXPath('//buchung')->text();
$status = $this->getStringOrNullValue($node->filterXPath('//buchung'));
if (null === $status || '' === $status) {
throw new \InvalidArgumentException('Missing required booking status in booking response.');
}
$bookingNumber = $this->getIntOrNullValue($node->filterXPath('//vorgang'));
$totalPrice = $this->getFloatOrNullValue($node->filterXPath('//gesamtpreis'));
$message = $this->getStringOrNullValue($node->filterXPath('//hinweis'));
@@ -60,18 +64,24 @@ class BookingResponseParser extends AbstractParser
$priceItems = [];
$node->filterXPath('//preise/preis')->each(function (Crawler $priceNode) use (&$priceItems): void {
$position = (int) $this->getRequiredAttrValue($priceNode, 'position', 'booking response price item');
$type = $this->getRequiredAttrValue($priceNode, 'art', 'booking response price item');
$label = $this->getRequiredAttrValue($priceNode, 'bezeichnung', 'booking response price item');
$unitPrice = $this->stringToFloat($this->getRequiredAttrValue($priceNode, 'preis', 'booking response price item'));
$totalPrice = $this->stringToFloat($this->getRequiredAttrValue($priceNode, 'gesamtpreis', 'booking response price item'));
$priceItems[] = new PriceItem(
position: (int) $priceNode->attr('position'),
type: $priceNode->attr('art'),
subType: $priceNode->attr('unterart'),
label: $priceNode->attr('bezeichnung'),
dateFrom: $priceNode->attr('terminvon'),
dateTo: $priceNode->attr('terminbis'),
quantity: (int) $priceNode->attr('anzahl'),
assignment: $priceNode->attr('zuordnung'),
unitPrice: $this->stringToFloat($priceNode->attr('preis')),
totalPrice: $this->stringToFloat($priceNode->attr('gesamtpreis')),
id: $priceNode->attr('id')
position: $position,
type: $type,
subType: $this->getAttrOrNullValue($priceNode, 'unterart'),
label: $label,
dateFrom: $this->getAttrOrNullValue($priceNode, 'terminvon'),
dateTo: $this->getAttrOrNullValue($priceNode, 'terminbis'),
quantity: (int) ($this->getAttrOrNullValue($priceNode, 'anzahl') ?? 0),
assignment: $this->getAttrOrNullValue($priceNode, 'zuordnung'),
unitPrice: $unitPrice,
totalPrice: $totalPrice,
id: $this->getAttrOrNullValue($priceNode, 'id')
);
});
@@ -94,25 +104,26 @@ class BookingResponseParser extends AbstractParser
$finalPaymentAmount = null;
$finalPaymentDate = null;
$depositNode = $paymentNode->filterXPath('//anzahlung');
$depositNode = $paymentNode->filterXPath('.//anzahlung');
if ($depositNode->count() > 0) {
$depositAmount = $this->stringToFloat($depositNode->attr('betrag'));
$depositDate = $depositNode->attr('termin');
$depositAmount = $this->stringToFloat($this->getAttrOrNullValue($depositNode, 'betrag'));
$depositDate = $this->getAttrOrNullValue($depositNode, 'termin');
}
$finalPaymentNode = $paymentNode->filterXPath('//restzahlung');
$finalPaymentNode = $paymentNode->filterXPath('.//restzahlung');
if ($finalPaymentNode->count() > 0) {
$finalPaymentAmount = $this->stringToFloat($finalPaymentNode->attr('betrag'));
$finalPaymentDate = $finalPaymentNode->attr('termin');
$finalPaymentAmount = $this->stringToFloat($this->getAttrOrNullValue($finalPaymentNode, 'betrag'));
$finalPaymentDate = $this->getAttrOrNullValue($finalPaymentNode, 'termin');
}
// Parse purchase vouchers (kaufgutschein elements)
$appliedPurchaseVouchers = [];
$voucherNodes = $paymentNode->filterXPath('//kaufgutschein');
$voucherNodes = $paymentNode->filterXPath('.//kaufgutschein');
$voucherNodes->each(function (Crawler $voucherNode) use (&$appliedPurchaseVouchers): void {
$amount = $this->getAttrOrNullValue($voucherNode, 'betrag');
$appliedPurchaseVouchers[] = [
'nummer' => $voucherNode->attr('nummer') ?? '',
'betrag' => $this->stringToFloat($voucherNode->attr('betrag')),
'nummer' => $this->getAttrOrNullValue($voucherNode, 'nummer') ?? '',
'betrag' => $this->stringToFloat($amount),
];
});