This commit is contained in:
Björn Fromme
2023-07-08 16:21:05 +02:00
parent 305699b1ac
commit dc5b9a9238
17 changed files with 297 additions and 176 deletions
+143
View File
@@ -0,0 +1,143 @@
<?php
namespace App\BusProNet;
use App\BusProNet\Model\Address;
use App\BusProNet\Model\Communication;
use App\BusProNet\Model\CrmAttribute;
use App\BusProNet\Model\CrmAttributesResponse;
use App\BusProNet\Model\CrmAttributeGroup;
use App\BusProNet\Model\ProfileResponse;
use App\BusProNet\Model\BaseResponse;
class ResponseParser
{
public const ATTR_ID_TEAMER = 1070;
/**
* @throws ResponseParserException
*/
public function parseXmlString(string $content): BaseResponse
{
$xml = simplexml_load_string($content);
$type = (string) $xml->xpath('satz/@typ')[0];
switch ($type) {
case 'HINWEIS':
return $this->createBaseResponse($xml);
case 'KUNDENKONTO':
$subType = (string) $xml->xpath('art')[0];
switch ($subType) {
case 'Adressdaten':
return $this->createProfileResponse($xml);
case 'SelektionCRM':
return $this->createCrmAttributesResponse($xml);
}
}
throw new ResponseParserException('Unable to parse XML response');
}
public function createBaseResponse(\SimpleXMLElement $xml): BaseResponse
{
$code = (int) $xml->xpath('satz/nr')[0];
$message = (string) $xml->xpath('satz/text')[0];
return new BaseResponse($code, $message);
}
public function createProfileResponse(\SimpleXMLElement $xml): ProfileResponse
{
$addressId = (int) $xml->xpath('idadresse')[0];
$personId = (int) $xml->xpath('idperson')[0];
$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];
$gender = (string) $xml->xpath('adressdaten/geschlecht')[0];
$gender = $gender === 'W' ? 'f' : strtolower($gender);
$date = $xml->xpath('adressdaten/geburtsdatum');
$dateOfBirth = $date ?\DateTimeImmutable::createFromFormat('d.m.Y', (string) $date[0]) : null;
$response = new ProfileResponse();
$response
->setAddressId($addressId)
->setPersonId($personId)
->setFirstName($firstName)
->setName($lastName)
->setTitle($title)
->setSalutation($salutation)
->setGender($gender)
->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');
$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)
;
$response->setAddress($address);
$phone = $xml->xpath('adressdaten/kommunikation/telefonprivat');
$mobile = $xml->xpath('adressdaten/kommunikation/telefonmobil');
$email = $xml->xpath('adressdaten/kommunikation/email');
$communication = new Communication();
$communication
->setPhone($phone ? (string) $phone[0] : null)
->setMobile($mobile ? (string) $mobile[0] : null)
->setEmail($email ? (string) $email[0] : null)
;
$response->setCommunication($communication);
return $response;
}
public function createCrmAttributesResponse(\SimpleXMLElement $xml): CrmAttributesResponse
{
$groups = [];
$isTeam = false;
foreach ($xml->xpath('selektionsmerkmale/selektionsgruppe') as $item) {
$group = new CrmAttributeGroup();
$group->setLabel($item->attributes()['bezeichnung']);
$attributes = [];
foreach ($item->xpath('selektion') as $subItem) {
$attribute = new CrmAttribute();
$attribute
->setId((int) $subItem->attributes()['id'])
->setLabel((string) $subItem->attributes()['bezeichnung'])
->setSelected('True' === (string) $subItem->attributes()['auswahl'])
;
$attributes[] = $attribute;
if (static::ATTR_ID_TEAMER === $attribute->getId() && true === $attribute->isSelected()) {
$isTeam = true;
}
}
$group->setAttributes($attributes);
$groups[] = $group;
}
$response = new CrmAttributesResponse();
$response
->setAttributeGroups($groups)
->setTeamer($isTeam)
;
return $response;
}
}