1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Stream\Data; |
6
|
|
|
|
7
|
|
|
class XMLConverter implements Converter |
8
|
|
|
{ |
9
|
|
|
public static function getFormat(): string |
10
|
|
|
{ |
11
|
|
|
return 'text/xml'; |
12
|
|
|
} |
13
|
|
|
public function convert($data, array $params = []): string |
14
|
|
|
{ |
15
|
|
|
return $this->xml_encode($data); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
private function xml_encode($data, \DOMNode $domElement = null, \DOMDocument $DOMDocument = null): string |
19
|
|
|
{ |
20
|
|
|
if ($DOMDocument === null) { |
21
|
|
|
$DOMDocument = new \DOMDocument(); |
22
|
|
|
$DOMDocument->formatOutput = true; |
23
|
|
|
$this->xml_encode(['data' => $data], $DOMDocument, $DOMDocument); |
24
|
|
|
return $DOMDocument->saveXML(); |
25
|
|
|
} |
26
|
|
|
// To cope with embedded objects |
27
|
|
|
if (is_object($data)) { |
28
|
|
|
$data = get_object_vars($data); |
29
|
|
|
} |
30
|
|
|
if (is_array($data)) { |
31
|
|
|
foreach ($data as $index => $mixedElement) { |
32
|
|
|
if (is_int($index)) { |
33
|
|
|
if ($index === 0) { |
34
|
|
|
$node = $domElement; |
35
|
|
|
} else { |
36
|
|
|
$node = $DOMDocument->createElement($domElement->tagName); |
37
|
|
|
$domElement->parentNode->appendChild($node); |
38
|
|
|
} |
39
|
|
|
} else { |
40
|
|
|
$plural = $DOMDocument->createElement($index); |
41
|
|
|
$domElement->appendChild($plural); |
|
|
|
|
42
|
|
|
$node = $plural; |
43
|
|
|
if (!(rtrim($index, 's') === $index)) { |
44
|
|
|
$singular = $DOMDocument->createElement(rtrim($index, 's')); |
45
|
|
|
$plural->appendChild($singular); |
46
|
|
|
$node = $singular; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->xml_encode($mixedElement, $node, $DOMDocument); |
51
|
|
|
} |
52
|
|
|
} else { |
53
|
|
|
$data = is_bool($data) ? ($data ? 'true' : 'false') : $data; |
54
|
|
|
$domElement->appendChild($DOMDocument->createTextNode($data)); |
55
|
|
|
} |
56
|
|
|
return ''; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.