I am currently trying to rebuilt some java code in PHP. The code builds XML from a given object and sends XML requests to a backend. The backend delivers the response. Now I want to convert the XML response in a php object of a certain complex class. I used SimpleXml to convert the string to the class XmlResponse but i can only access the getter and setter from the XmlResponse class and not the other class inside the XmlResponse Class. In Java I simply use JAXB but I am not sure if something like this is possible. The the problem is, that the data tag can change from request to request, delivering other objects, depending on the request!
I am using the following setup :
PHP 5.6 or 7
Zend 2 or 3
Greetings!
At the end I want something like this :
<?php
$responseObject = convertStringToObject($responseString, XMLResponse::class);
$result = $responseObject->getResult();
$data = $responseObject->getResult()->getData();
$status = $responseObject->getResult()->getStatus();
$user = $responseObject->getResult()->getData()->getUser()
?>
The Response looks like this :
<response>
<transaction_id>1234</transaction_id>
<result>
<status>
<code>1</code>
<msg>Success</msg>
</status>
<data>
<user>
<fname>Jon</lname>
<lname>Doe</lname>
<birthday>1986-08-01 00:00:00</birthday>
</user>
</data>
</result>
</response>
Now I have the following classes in PHP.
XmlResponse
class XmlResponse {
public $result; #XmlResult Class
public $transaction_id; #string
public function getArrayCopy() {
return get_object_vars($this);
}
//getter + setter
}
XmlResult
class XmlResult {
public $data; #XmlData Class
public $status; #XmlStatus Class
public function getArrayCopy() {
return get_object_vars($this);
}
//getter + setter
}
XmlStatus
class XmlStatus {
public $code; #string
public $msg; #string
public function getArrayCopy() {
return get_object_vars($this);
}
//getter + setter
}
XmlData
class XmlData {
public $user; #User Class
public $car; #Car Class
public $member; #Member Class
public $foo; #Foo Class
public function getArrayCopy() {
return get_object_vars($this);
}
//getter + setter
}
User
class User {
public $fname; #string
public $lname; #string
public $birthday; #string
public function getArrayCopy() {
return get_object_vars($this);
}
//getter + setter
}