1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Stream\Data; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\MessageInterface; |
8
|
|
|
|
9
|
|
|
class XMLConverter implements Converter |
10
|
|
|
{ |
11
|
|
|
public static function getFormat(): string |
12
|
|
|
{ |
13
|
|
|
return 'text/xml'; |
14
|
|
|
} |
15
|
|
|
public function setHeaders(MessageInterface $message): MessageInterface |
16
|
|
|
{ |
17
|
|
|
return $message->withHeader('Content-Type', static::getFormat()); |
18
|
|
|
} |
19
|
|
|
public function convert($data, array $params = []) : string |
20
|
|
|
{ |
21
|
|
|
return $this->xml_encode($data); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
private function xml_encode($data, \DOMNode $domElement = null, \DOMDocument $DOMDocument = null): string |
25
|
|
|
{ |
26
|
|
|
if ($DOMDocument === null) { |
27
|
|
|
$DOMDocument = new \DOMDocument(); |
28
|
|
|
$DOMDocument->formatOutput = true; |
29
|
|
|
$this->xml_encode(['data' => $data], $DOMDocument, $DOMDocument); |
30
|
|
|
return $DOMDocument->saveXML(); |
31
|
|
|
} |
32
|
|
|
// To cope with embedded objects |
33
|
|
|
if (is_object($data)) { |
34
|
|
|
$data = get_object_vars($data); |
35
|
|
|
} |
36
|
|
|
if (is_array($data)) { |
37
|
|
|
foreach ($data as $index => $mixedElement) { |
38
|
|
|
if (is_int($index)) { |
39
|
|
|
if ($index === 0) { |
40
|
|
|
$node = $domElement; |
41
|
|
|
} else { |
42
|
|
|
$node = $DOMDocument->createElement($domElement->tagName); |
43
|
|
|
$domElement->parentNode->appendChild($node); |
44
|
|
|
} |
45
|
|
|
} else { |
46
|
|
|
$plural = $DOMDocument->createElement($index); |
47
|
|
|
$domElement->appendChild($plural); |
|
|
|
|
48
|
|
|
$node = $plural; |
49
|
|
|
if (!(rtrim($index, 's') === $index)) { |
50
|
|
|
$singular = $DOMDocument->createElement(rtrim($index, 's')); |
51
|
|
|
$plural->appendChild($singular); |
52
|
|
|
$node = $singular; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->xml_encode($mixedElement, $node, $DOMDocument); |
57
|
|
|
} |
58
|
|
|
} else { |
59
|
|
|
$data = is_bool($data) ? ($data ? 'true' : 'false') : $data; |
60
|
|
|
$domElement->appendChild($DOMDocument->createTextNode($data)); |
61
|
|
|
} |
62
|
|
|
return ''; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
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.