50 lines
1.7 KiB
PHP
50 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\BusProNet\XmlParser;
|
|
|
|
use App\BusProNet\Model\VoucherSaleResponse;
|
|
use Symfony\Component\DomCrawler\Crawler;
|
|
|
|
/**
|
|
* Parses GUTSCHEINBUCHUNG responses that are not a HINWEIS.
|
|
*
|
|
* Dry run (buchungsart Anfrage, measured on the test system 2026-10-01):
|
|
* <ergebnis><satz typ="GUTSCHEINBUCHUNG"></satz><buchung>möglich</buchung></ergebnis>
|
|
*
|
|
* Purchase (buchungsart Buchung, as documented by the vendor):
|
|
* <ergebnis>
|
|
* <satz typ="GUTSCHEINBUCHUNG"/>
|
|
* <buchung>erfolgt</buchung>
|
|
* <idgutschein/><gutscheinnummer/><einloesecode/><preis/><rechnungsnummer/><email/>
|
|
* </ergebnis>
|
|
*/
|
|
final class VoucherSaleResponseParser extends AbstractParser
|
|
{
|
|
public function parse(Crawler $node): VoucherSaleResponse
|
|
{
|
|
$status = trim($this->getStringOrNullValue($node->filterXPath('//buchung')) ?? '');
|
|
if ('' === $status) {
|
|
throw new \InvalidArgumentException('Missing required status in voucher sale response.');
|
|
}
|
|
|
|
return new VoucherSaleResponse(
|
|
status: $status,
|
|
voucherId: $this->getIntOrNullValue($node->filterXPath('//idgutschein')),
|
|
voucherNumber: $this->trimmedOrNull($node->filterXPath('//gutscheinnummer')),
|
|
redemptionCode: $this->trimmedOrNull($node->filterXPath('//einloesecode')),
|
|
price: $this->getFloatOrNullValue($node->filterXPath('//preis')),
|
|
invoiceNumber: $this->trimmedOrNull($node->filterXPath('//rechnungsnummer')),
|
|
email: $this->trimmedOrNull($node->filterXPath('//email')),
|
|
);
|
|
}
|
|
|
|
private function trimmedOrNull(Crawler $node): ?string
|
|
{
|
|
$value = trim($this->getStringOrNullValue($node) ?? '');
|
|
|
|
return '' !== $value ? $value : null;
|
|
}
|
|
}
|