1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MIT License |
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SprykerEco\Zed\Ratepay\Business\Api\Model; |
9
|
|
|
|
10
|
|
|
use SprykerEco\Zed\Ratepay\Business\Api\Builder\BuilderInterface; |
11
|
|
|
use SprykerEco\Zed\Ratepay\Business\Api\SimpleXMLElement; |
12
|
|
|
|
13
|
|
|
abstract class AbstractRequest implements RequestInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @return array |
17
|
|
|
*/ |
18
|
|
|
abstract protected function buildData(); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
abstract public function getRootTag(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param array $array |
27
|
|
|
* @param \SprykerEco\Zed\Ratepay\Business\Api\SimpleXMLElement &$xml |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
protected function arrayToXml(array $array, SimpleXMLElement $xml) |
32
|
|
|
{ |
33
|
|
|
foreach ($array as $key => $value) { |
34
|
|
|
$this->keyValueToXml($key, $value, $xml); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $key |
40
|
|
|
* @param mixed $value |
41
|
|
|
* @param \SprykerEco\Zed\Ratepay\Business\Api\SimpleXMLElement &$xml |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
protected function keyValueToXml($key, $value, SimpleXMLElement $xml) |
46
|
|
|
{ |
47
|
|
|
if ($value !== null) { |
48
|
|
|
if ($value instanceof BuilderInterface) { |
49
|
|
|
if (is_numeric($key)) { |
50
|
|
|
$key = $value->getRootTag(); |
51
|
|
|
} |
52
|
|
|
$value = $value->buildData(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (substr($key, 0, 1) === '@') { |
56
|
|
|
$attributeKey = substr($key, 1); |
57
|
|
|
$xml->addAttribute($attributeKey, $value); |
|
|
|
|
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($key === '#') { |
62
|
|
|
$dom = dom_import_simplexml($xml); |
63
|
|
|
$owner = $dom->ownerDocument; |
64
|
|
|
$dom->appendChild($owner->createCDATASection($value)); |
|
|
|
|
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (is_array($value)) { |
69
|
|
|
$subnode = $xml->addChild($key); |
70
|
|
|
$this->arrayToXml($value, $subnode); |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$xml->addCDataChild($key, $value); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function toXml() |
82
|
|
|
{ |
83
|
|
|
$data = $this->buildData(); |
84
|
|
|
$rootTag = $this->getRootTag(); |
85
|
|
|
$xml = new SimpleXMLElement('<' . $rootTag . '></' . $rootTag . '>'); |
86
|
|
|
$this->arrayToXml($data, $xml); |
87
|
|
|
|
88
|
|
|
return $xml->asXML(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function __toString() |
95
|
|
|
{ |
96
|
|
|
return $this->toXml(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|