1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace Codeception\Util;
use Flow\JSONPath\JSONPath;
use InvalidArgumentException;
use DOMDocument;
class JsonArray
{
/**
* @var array
*/
protected $jsonArray = [];
/**
* @var DOMDocument
*/
protected $jsonXml = null;
public function __construct($jsonString)
{
if (!is_string($jsonString)) {
throw new InvalidArgumentException('$jsonString param must be a string.');
}
$jsonDecode = json_decode($jsonString, true);
if (!is_array($jsonDecode)) {
$jsonDecode = [$jsonDecode];
}
$this->jsonArray = $jsonDecode;
if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidArgumentException(
sprintf(
"Invalid json: %s. System message: %s.",
$jsonString,
json_last_error_msg()
),
json_last_error()
);
}
}
public function toXml()
{
if ($this->jsonXml) {
return $this->jsonXml;
}
$root = 'root';
$jsonArray = $this->jsonArray;
if (count($jsonArray) == 1) {
$value = reset($jsonArray);
if (is_array($value)) {
$root = key($jsonArray);
$jsonArray = $value;
}
}
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = false;
$root = $dom->createElement($root);
$dom->appendChild($root);
$this->arrayToXml($dom, $root, $jsonArray);
$this->jsonXml = $dom;
return $dom;
}
/**
* @return array
*/
public function toArray()
{
return $this->jsonArray;
}
public function filterByXPath($xpath)
{
$path = new \DOMXPath($this->toXml());
return $path->query($xpath);
}
public function filterByJsonPath($jsonPath)
{
if (!class_exists('Flow\JSONPath\JSONPath')) {
throw new \Exception('JSONPath library not installed. Please add `flow/jsonpath` to composer.json');
}
return (new JSONPath($this->jsonArray))->find($jsonPath)->data();
}
public function getXmlString()
{
return $this->toXml()->saveXML();
}
public function containsArray(array $needle)
{
return (new ArrayContainsComparator($this->jsonArray))->containsArray($needle);
}
private function arrayToXml(\DOMDocument $doc, \DOMNode $node, $array)
{
foreach ($array as $key => $value) {
if (is_numeric($key)) {
$subNode = $doc->createElement($node->nodeName);
$node->parentNode->appendChild($subNode);
} else {
try {
$subNode = $doc->createElement($key);
} catch (\Exception $e) {
$key = $this->getValidTagNameForInvalidKey($key);
$subNode = $doc->createElement($key);
}
$node->appendChild($subNode);
}
if (is_array($value)) {
$this->arrayToXml($doc, $subNode, $value);
} else {
$subNode->nodeValue = htmlspecialchars((string)$value);
}
}
}
private function getValidTagNameForInvalidKey($key)
{
static $map = [];
if (!isset($map[$key])) {
$tagName = 'invalidTag' . (count($map) + 1);
$map[$key] = $tagName;
codecept_debug($tagName . ' is "' . $key . '"');
}
return $map[$key];
}
}