-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXmlBuilder.php
More file actions
166 lines (150 loc) · 3.98 KB
/
XmlBuilder.php
File metadata and controls
166 lines (150 loc) · 3.98 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
declare(strict_types=1);
namespace Codeception\Util;
use DOMDocument;
use DOMElement;
use DOMNode;
use Exception;
/**
* That's a pretty simple yet powerful class to build XML structures in jQuery-like style.
* With no XML line actually written!
* Uses DOM extension to manipulate XML data.
*
* ```php
* <?php
* $xml = new \Codeception\Util\XmlBuilder();
* $xml->users
* ->user
* ->val(1)
* ->email
* ->val('[email protected]')
* ->attr('valid','true')
* ->parent()
* ->cart
* ->attr('empty','false')
* ->items
* ->item
* ->val('useful item');
* ->parents('user')
* ->active
* ->val(1);
* echo $xml;
* ```
*
* This will produce this XML
*
* ```xml
* <?xml version="1.0"?>
* <users>
* <user>
* 1
* <email valid="true">[email protected]</email>
* <cart empty="false">
* <items>
* <item>useful item</item>
* </items>
* </cart>
* <active>1</active>
* </user>
* </users>
* ```
*
* ### Usage
*
* Builder uses chained calls. So each call to builder returns a builder object.
* Except for `getDom` and `__toString` methods.
*
* * `$xml->node` - create new xml node and go inside of it.
* * `$xml->node->val('value')` - sets the inner value of node
* * `$xml->attr('name','value')` - set the attribute of node
* * `$xml->parent()` - go back to parent node.
* * `$xml->parents('user')` - go back through all parents to `user` node.
*
* Export:
*
* * `$xml->getDom` - get a DOMDocument object
* * `$xml->__toString` - get a string representation of XML.
*
* [Source code](https://github.com/Codeception/lib-xml/blob/main/src/Util/XmlBuilder.php)
*/
class XmlBuilder
{
protected DOMDocument $dom;
protected DOMNode $currentNode;
public function __construct()
{
$this->dom = new DOMDocument();
$this->currentNode = $this->dom;
}
/**
* Appends child node
*/
public function __get(string $tag): XmlBuilder
{
$domElement = $this->dom->createElement($tag);
$this->currentNode->appendChild($domElement);
$this->currentNode = $domElement;
return $this;
}
public function val(string $val): self
{
$this->currentNode->nodeValue = $val;
return $this;
}
/**
* Sets attribute for current node
*/
public function attr(string $attr, string $val): self
{
if (!$this->currentNode instanceof DOMElement) {
throw new Exception('Current node is not DOMElement');
}
$this->currentNode->setAttribute($attr, $val);
return $this;
}
/**
* Traverses to parent
*/
public function parent(): self
{
if ($this->currentNode->parentNode === null) {
throw new Exception('Element has no parent');
}
$this->currentNode = $this->currentNode->parentNode;
return $this;
}
/**
* Traverses to parent with $tagName
*
* @throws Exception
*/
public function parents(string $tagName): self
{
$traverseNode = $this->currentNode;
$elFound = false;
while ($traverseNode->parentNode) {
$traverseNode = $traverseNode->parentNode;
if ($traverseNode instanceof DOMElement && $traverseNode->tagName === $tagName) {
$this->currentNode = $traverseNode;
$elFound = true;
break;
}
}
if (!$elFound) {
throw new Exception("Parent {$tagName} not found in XML");
}
return $this;
}
public function __toString(): string
{
$string = $this->dom->saveXML();
if ($string === false) {
throw new Exception('Failed to convert DOM to string');
}
return $string;
}
public function getDom(): DOMDocument
{
return $this->dom;
}
}