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