<?php
 namespace App\ImportedOrder\Convertor; class ToXml { private \App\ImportedOrder\Order $importedOrderObj; private \SimpleXMLElement $xml; public function __construct(\App\ImportedOrder\Order $io) { $this->importedOrderObj = $io; } public function generateXml(): void { $this->xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><order/>'); $this->xml->system = ''; $this->xml->system['code'] = $this->importedOrderObj->externalSystemCode; if ($this->importedOrderObj->respOffice or $this->importedOrderObj->respUser) { $this->xml->responsible = ''; if ($this->importedOrderObj->respOffice) { $this->xml->responsible->office = ''; $this->injectImportedOrderOffice($this->importedOrderObj->respOffice, $this->xml->responsible->office); } if ($this->importedOrderObj->respUser) { $this->xml->responsible->user = ''; $this->injectImportedOrderUser($this->importedOrderObj->respUser, $this->xml->responsible->user); } } } public function getXml(): \SimpleXMLElement { if (!isset($this->xml)) { throw new \Exception('You must call the `generateXml()` method before calling this method.'); } return $this->xml; } private function injectImportedOrderOffice(\App\ImportedOrder\Office $officeObj, \SimpleXMLElement $xml): void { if ($officeObj->idSystem) { $xml['idSystem'] = $officeObj->idSystem; } if ($officeObj->idTina) { $xml['idTina'] = $officeObj->idTina; } if ($officeObj->name) { $xml->name = $officeObj->name; } } private function injectImportedOrderUser(\App\ImportedOrder\User $userObj, \SimpleXMLElement $xml): void { if ($userObj->idSystem) { $xml['idSystem'] = $userObj->idSystem; } if ($userObj->idTina) { $xml['idTina'] = $userObj->idTina; } if ($userObj->firstName) { $xml->firstName = $userObj->firstName; } if ($userObj->lastName) { $xml->lastName = $userObj->lastName; } } }