fix: simplyfy xml parsing to avoid unexpected results

This commit is contained in:
Björn Fromme
2024-04-04 08:48:49 +02:00
parent 48d407f74c
commit a22c3c959a
+66 -54
View File
@@ -35,14 +35,14 @@ class ResponseParser
$responseType = $type; $responseType = $type;
$responseTypeXml = $xml->xpath('satz/@typ'); $responseTypeXml = $xml->xpath('satz/@typ');
if (0 < count($responseTypeXml)) { if (0 < count($responseTypeXml)) {
$responseType = (string)$xml->xpath('satz/@typ')[0]; $responseType = (string) $xml->xpath('satz/@typ')[0];
} }
switch ($responseType) { switch ($responseType) {
case ApiClient::TYPE_NOTIFICATION: case ApiClient::TYPE_NOTIFICATION:
return $this->createNotificationResponse($xml); return $this->createNotificationResponse($xml);
case ApiClient::TYPE_CUSTOMER_DATA: case ApiClient::TYPE_CUSTOMER_DATA:
$subType = (string) $xml->xpath('art')[0]; $subType = (string) $xml->art;
switch ($subType) { switch ($subType) {
case 'Adressdaten': case 'Adressdaten':
case 'Adressdaten_Ändern': case 'Adressdaten_Ändern':
@@ -65,27 +65,31 @@ class ResponseParser
public function createNotificationResponse(\SimpleXMLElement $xml): NotificationResponse public function createNotificationResponse(\SimpleXMLElement $xml): NotificationResponse
{ {
$code = (int) $xml->xpath('satz/nr')[0]; $recordXml = $xml->satz;
$message = (string) $xml->xpath('satz/text')[0];
$code = (int) $recordXml->nr;
$message = (string) $recordXml->text;
return new NotificationResponse($code, $message); return new NotificationResponse($code, $message);
} }
public function createProfileResponse(\SimpleXMLElement $xml): ProfileResponse public function createProfileResponse(\SimpleXMLElement $xml): ProfileResponse
{ {
$addressId = (int) $xml->xpath('idadresse')[0]; $addressId = (int) $xml->idadresse;
$personId = (int) $xml->xpath('idperson')[0]; $personId = (int) $xml->idperson;
$lastName = (string) $xml->xpath('adressdaten/name')[0]; $addressXml = $xml->adressdaten;
$firstName = (string) $xml->xpath('adressdaten/vorname')[0];
$salutation = (string) $xml->xpath('adressdaten/anrede')[0];
$title = (string) $xml->xpath('adressdaten/titel')[0];
$gender = (string) $xml->xpath('adressdaten/geschlecht')[0]; $lastName = (string) $addressXml->name;
$firstName = (string) $addressXml->vorname;
$salutation = (string) $addressXml->anrede;
$title = (string) $addressXml->titel;
$gender = (string) $addressXml->geschlecht;
$gender = strtoupper($gender) === 'W' ? 'F' : strtoupper($gender); $gender = strtoupper($gender) === 'W' ? 'F' : strtoupper($gender);
$date = $xml->xpath('adressdaten/geburtsdatum'); $dateString = (string) $addressXml->geburtsdatum;
$dateOfBirth = $date ?\DateTimeImmutable::createFromFormat('d.m.Y', (string) $date[0]) : null; $dateOfBirth = empty($dateString) ? null : \DateTimeImmutable::createFromFormat('d.m.Y', $dateString);
$response = new ProfileResponse(); $response = new ProfileResponse();
$response $response
@@ -99,30 +103,34 @@ class ResponseParser
->setDateOfBirth($dateOfBirth) ->setDateOfBirth($dateOfBirth)
; ;
$postalAddressStreet = $xml->xpath('adressdaten/anschrift/strasse'); $postalXml = $addressXml->anschrift;
$postalAddressPostcode = $xml->xpath('adressdaten/anschrift/plz');
$postalAddressCity = $xml->xpath('adressdaten/anschrift/ort'); $postalAddressStreet = (string) $postalXml->strasse;
$postalAddressCountry = $xml->xpath('adressdaten/anschrift/land'); $postalAddressPostcode = (string) $postalXml->plz;
$postalAddressCity = (string) $postalXml->ort;
$postalAddressCountry = (string) $postalXml->land;
$address = new Address(); $address = new Address();
$address $address
->setStreet($postalAddressStreet ? (string) $postalAddressStreet[0] : null) ->setStreet($postalAddressStreet)
->setPostCode($postalAddressPostcode ? (string) $postalAddressPostcode[0] : null) ->setPostCode($postalAddressPostcode)
->setCity($postalAddressCity ? (string) $postalAddressCity[0] : null) ->setCity($postalAddressCity)
->setCountry($postalAddressCountry ? (string) $postalAddressCountry[0] : null) ->setCountry($postalAddressCountry)
; ;
$response->setAddress($address); $response->setAddress($address);
$phone = $xml->xpath('adressdaten/kommunikation/telefonprivat'); $contactXml = $addressXml->kommunikation;
$mobile = $xml->xpath('adressdaten/kommunikation/telefonmobil');
$email = $xml->xpath('adressdaten/kommunikation/email'); $phone = (string) $contactXml->telefonprivat;
$mobile = (string) $contactXml->telefonmobil;
$email = (string) $contactXml->email;
$communication = new Communication(); $communication = new Communication();
$communication $communication
->setPhone($phone ? (string) $phone[0] : null) ->setPhone($phone)
->setMobile($mobile ? (string) $mobile[0] : null) ->setMobile($mobile)
->setEmail($email ? (string) $email[0] : null) ->setEmail($email)
; ;
$response->setCommunication($communication); $response->setCommunication($communication);
@@ -136,15 +144,16 @@ class ResponseParser
$isAdmin = $isManager = $isHouseManager = $isTeamer = false; $isAdmin = $isManager = $isHouseManager = $isTeamer = false;
$hotelCode = null; $hotelCode = null;
foreach ($xml->xpath('selektionsmerkmale/selektionsgruppe') as $item) { foreach ($xml->selektionsmerkmale->selektionsgruppe as $item) {
$group = new CrmAttributeGroup(); $group = new CrmAttributeGroup();
$group->setLabel($item->attributes()['bezeichnung']); $group->setLabel($item->attributes()['bezeichnung']);
$attributes = []; $attributes = [];
foreach ($item->xpath('selektion') as $subItem) { foreach ($item->selektion as $subItem) {
$attributeId = (int) $subItem->attributes()['id']; $subItemAttributes = $subItem->attributes();
$attributeLabel = (string) $subItem->attributes()['bezeichnung']; $attributeId = (int) $subItemAttributes['id'];
$attributeSelected = 'True' === (string) $subItem->attributes()['auswahl']; $attributeLabel = (string) $subItemAttributes['bezeichnung'];
$attributeSelected = 'True' === (string) $subItemAttributes['auswahl'];
$attribute = new CrmAttribute(); $attribute = new CrmAttribute();
$attribute $attribute
->setId($attributeId) ->setId($attributeId)
@@ -194,14 +203,15 @@ class ResponseParser
{ {
$countries = []; $countries = [];
foreach ($xml->xpath('laender/land') as $item) { foreach ($xml->laender->land as $item) {
$token = (string) $item->attributes()['kuerzel']; $itemAttributes = $item->attributes();
$token = (string) $itemAttributes['kuerzel'];
$country = new Country(); $country = new Country();
$country $country
->setId((int) $item->attributes()['id']) ->setId((int) $itemAttributes['id'])
->setName((string) $item->attributes()['bezeichnung']) ->setName((string) $itemAttributes['bezeichnung'])
->setToken((string) $item->attributes()['kuerzel']) ->setToken((string) $itemAttributes['kuerzel'])
->setNationality((string) $item->attributes()['nationalitaet']) ->setNationality((string) $itemAttributes['nationalitaet'])
; ;
$countries[$token] = $country; $countries[$token] = $country;
} }
@@ -213,16 +223,17 @@ class ResponseParser
{ {
$pickups = []; $pickups = [];
foreach ($xml->xpath('zustieg') as $item) { foreach ($xml->zustieg as $item) {
$id = (int) $item->attributes()['id']; $itemAttributes = $item->attributes();
$busProId = (int) $item->attributes()['idbuspro']; $id = (int) $itemAttributes['id'];
$busProId = (int) $itemAttributes['idbuspro'];
$pickup = new Pickup(); $pickup = new Pickup();
$pickup $pickup
->setId($id) ->setId($id)
->setBusProId($busProId) ->setBusProId($busProId)
->setCode((string) $item->attributes()['code']) ->setCode((string) $itemAttributes['code'])
->setCity($item->xpath('ort') ? (string) $item->xpath('ort')[0] : null) ->setCity((string) $item->ort)
->setStreet($item->xpath('strasse') ? (string) $item->xpath('strasse')[0] : '') ->setStreet((string) $item->strasse)
; ;
$pickups[$busProId] = $pickup; $pickups[$busProId] = $pickup;
} }
@@ -234,20 +245,21 @@ class ResponseParser
{ {
$hotels = []; $hotels = [];
foreach ($xml->xpath('hotel') as $item) { foreach ($xml->hotel as $item) {
$id = (int) $item->attributes()['id']; $itemAttributes = $item->attributes();
$busProId = (int) $item->attributes()['idbuspro']; $id = (int) $itemAttributes['id'];
$busProId = (int) $itemAttributes['idbuspro'];
$hotel = new Hotel(); $hotel = new Hotel();
$hotel $hotel
->setId($id) ->setId($id)
->setBusProId($busProId) ->setBusProId($busProId)
->setCode((string) $item->attributes()['code']) ->setCode((string) $itemAttributes['code'])
->setName($item->xpath('name') ? (string) $item->xpath('name')[0] : null) ->setName((string) $item->name)
->setCountry($item->xpath('land') ? strtolower((string) $item->xpath('land')[0]) : null) ->setCountry(strtolower((string) $item->land))
->setCity($item->xpath('ort') ? (string) $item->xpath('ort')[0] : null) ->setCity((string) $item->ort)
->setStreet($item->xpath('strasse') ? (string) $item->xpath('strasse')[0] : null) ->setStreet((string) $item->strasse)
->setPhone($item->xpath('telefon') ? (string) $item->xpath('telefon')[0] : null) ->setPhone((string) $item->telefon)
->setType($item->xpath('art') ? (string) $item->xpath('art')[0] : null) ->setType((string) $item->art)
; ;
$hotels[$busProId] = $hotel; $hotels[$busProId] = $hotel;
} }