PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.4
Booking for Appointments and Events Calendar – Amelia v2.4.4
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / vendor / apimatic / core / src / Utils / XmlSerializer.php
ameliabooking / vendor / apimatic / core / src / Utils Last commit date
CoreHelper.php 1 year ago DateHelper.php 1 year ago JsonHelper.php 1 year ago XmlDeserializer.php 1 year ago XmlSerializer.php 1 year ago
XmlSerializer.php
149 lines
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Core\Utils;
6
7 use DOMDocument;
8
9 class XmlSerializer
10 {
11 /**
12 * @var DOMDocument
13 */
14 private $dom;
15
16 public function __construct(array $options)
17 {
18 $this->dom = static::createDomDocumentFromContext($options);
19 }
20
21 public function serialize(string $rootName, $value): string
22 {
23 $this->addAsSubelement($this->dom, $rootName, $value);
24 return $this->dom->saveXML();
25 }
26
27 public function serializeArray(string $rootName, string $itemName, $value): string
28 {
29 $this->addArrayAsSubelement($this->dom, $itemName, $value, $rootName);
30 return $this->dom->saveXML();
31 }
32
33 public function serializeMap(string $rootName, $entries): string
34 {
35 $this->addMapAsSubelement($this->dom, $rootName, $entries);
36 return $this->dom->saveXML();
37 }
38
39 public function addAsAttribute(\DOMElement $element, $name, $value): void
40 {
41 if ($value === null) {
42 return;
43 }
44
45 $element->setAttribute($name, $this->convertSimple($value));
46 }
47
48 public function addMapAsSubelement(\DOMNode $root, string $name, $entries): void
49 {
50 if ($entries === null) {
51 return;
52 }
53
54 $parent = $this->createElement($name);
55 $root->appendChild($parent);
56
57 foreach ($entries as $key => $value) {
58 $element = $this->addAsSubelement($parent, 'entry', $value);
59
60 if ($element !== null) {
61 $element->setAttribute('key', $key);
62 }
63 }
64 }
65
66 public function addArrayAsSubelement(
67 \DOMNode $root,
68 string $itemName,
69 $items,
70 string $wrappingElementName = null
71 ): void {
72 if ($items === null) {
73 return;
74 }
75
76 if ($wrappingElementName === null) {
77 $parent = $root;
78 } else {
79 $parent = $this->createElement($wrappingElementName);
80 $root->appendChild($parent);
81 }
82
83 foreach ($items as $item) {
84 $this->addAsSubelement($parent, $itemName, $item);
85 }
86 }
87
88 public function addAsSubelement(\DOMNode $root, string $name, $value): ?\DOMElement
89 {
90 if ($value === null) {
91 return null;
92 }
93
94 if (\is_object($value) && \method_exists($value, 'toXmlElement')) {
95 $element = $this->createElement($name);
96 $value->toXmlElement($this, $element);
97 } else {
98 $element = $this->createElement($name, $this->convertSimple($value));
99 }
100
101 $root->appendChild($element);
102 return $element;
103 }
104
105 public function createElement(string $name, string $value = null): \DOMElement
106 {
107 return $value === null ? $this->dom->createElement($name) : $this->dom->createElement($name, $value);
108 }
109
110 private function convertSimple($value): string
111 {
112 if (\is_bool($value)) {
113 return $value ? 'true' : 'false';
114 } else {
115 return \strval($value);
116 }
117 }
118
119 /**
120 * Create a DOM document, taking serializer options into account.
121 *
122 * @param array $context Options that the encoder has access to
123 */
124 private static function createDomDocumentFromContext(array $context): DOMDocument
125 {
126 $document = new DOMDocument();
127
128 // Set an attribute on the DOM document specifying, as part of the XML declaration,
129 $xmlOptions = [
130 // nicely formats output with indentation and extra space
131 'formatOutput',
132 // the version number of the document
133 'xmlVersion',
134 // the encoding of the document
135 'encoding',
136 // whether the document is standalone
137 'xmlStandalone',
138 ];
139
140 foreach ($xmlOptions as $xmlOption) {
141 if (isset($context[$xmlOption])) {
142 $document->$xmlOption = $context[$xmlOption];
143 }
144 }
145
146 return $document;
147 }
148 }
149