1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Load a DOM document from a json string or file |
4
|
|
|
* |
5
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
6
|
|
|
* @copyright Copyright (c) 2009-2017 FluentDOM Contributors |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace FluentDOM\Loader\Json { |
10
|
|
|
|
11
|
|
|
use FluentDOM\DOM\Document; |
12
|
|
|
use FluentDOM\DOM\Element; |
13
|
|
|
use FluentDOM\Loadable; |
14
|
|
|
use FluentDOM\Loader\Supports; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Load a DOM document from a json string or file |
18
|
|
|
*/ |
19
|
|
|
class Rayfish implements Loadable { |
20
|
|
|
|
21
|
|
|
use Supports\Json; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return string[] |
25
|
|
|
*/ |
26
|
6 |
|
public function getSupported(): array { |
27
|
6 |
|
return ['rayfish', 'application/rayfish', 'application/rayfish+json']; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param \DOMNode|Element $node |
32
|
|
|
* @param mixed $json |
33
|
|
|
*/ |
34
|
5 |
|
protected function transferTo(\DOMNode $node, $json) { |
35
|
5 |
|
if (is_object($json)) { |
36
|
|
|
/** @var Document $document */ |
37
|
5 |
|
$document = $node->ownerDocument ?: $node; |
38
|
5 |
|
$nodeName = $json->{'#name'}; |
39
|
5 |
|
list($attributes, $namespaces) = $this->getAttributes($json); |
40
|
5 |
|
$child = $document->createElementNS( |
41
|
5 |
|
$this->getNamespaceForNode($nodeName, $namespaces, $node), |
42
|
5 |
|
$nodeName |
43
|
|
|
); |
44
|
5 |
|
$node->appendChild($child); |
45
|
5 |
|
$this->transferText($document, $child, $json); |
46
|
5 |
|
$this->transferChildren($child, $json, $namespaces, $attributes); |
47
|
|
|
} |
48
|
5 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Document $document |
52
|
|
|
* @param Element $target |
53
|
|
|
* @param \stdClass $json |
54
|
|
|
*/ |
55
|
5 |
|
private function transferText(Document $document, Element $target, $json) { |
56
|
5 |
|
if (isset($json->{'#text'})) { |
57
|
2 |
|
$target->appendChild($document->createTextNode($json->{'#text'})); |
58
|
|
|
} |
59
|
5 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Element $target |
63
|
|
|
* @param \stdClass $json |
64
|
|
|
* @param \stdClass $namespaces |
65
|
|
|
* @param \stdClass $attributes |
66
|
|
|
*/ |
67
|
5 |
|
private function transferChildren( |
68
|
|
|
Element $target, \stdClass $json, \stdClass $namespaces, \stdClass $attributes |
69
|
|
|
) { |
70
|
5 |
|
if (isset($json->{'#children'})) { |
71
|
5 |
|
$this->transferAttributes($target, $namespaces, $attributes); |
72
|
5 |
|
foreach ($json->{'#children'} as $value) { |
73
|
4 |
|
$name = $value->{'#name'} ?? '@'; |
74
|
4 |
|
if (substr($name, 0, 1) !== '@') { |
75
|
1 |
|
$this->transferTo($target, $value); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
5 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Transfer attributes to the node. |
83
|
|
|
* |
84
|
|
|
* @param Element $node |
85
|
|
|
* @param \stdClass $namespaces |
86
|
|
|
* @param \stdClass $attributes |
87
|
|
|
*/ |
88
|
5 |
|
private function transferAttributes(Element $node, \stdClass $namespaces, \stdClass $attributes) { |
89
|
5 |
|
foreach ($namespaces as $name => $value) { |
90
|
2 |
|
$node->setAttribute($name, $value); |
91
|
|
|
} |
92
|
5 |
|
foreach ($attributes as $name => $value) { |
93
|
1 |
|
$node->setAttribute($name, $value); |
94
|
|
|
} |
95
|
5 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param \stdClass $json |
99
|
|
|
* @return \stdClass[] |
100
|
|
|
*/ |
101
|
5 |
|
private function getAttributes(\stdClass $json) { |
102
|
5 |
|
$attributes = new \stdClass(); |
103
|
5 |
|
$namespaces = new \stdClass(); |
104
|
5 |
|
if (isset($json->{'#children'})) { |
105
|
5 |
|
foreach ($json->{'#children'} as $child) { |
106
|
4 |
|
$name = $child->{'#name'} ?? ''; |
107
|
4 |
|
$value = $child->{'#text'} ?? ''; |
108
|
4 |
|
if ($name === '@xmlns' || substr($name, 0, 7) === '@xmlns:') { |
109
|
2 |
|
$namespaces->{substr($name, 1)} = $value; |
110
|
2 |
|
} elseif (substr($name, 0, 1) === '@') { |
111
|
1 |
|
$attributes->{substr($name, 1)} = $value; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
return [ |
116
|
5 |
|
$attributes, $namespaces |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |