|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Codeception\Util; |
| 6 | + |
| 7 | +use DOMDocument; |
| 8 | +use DOMElement; |
| 9 | +use Exception; |
| 10 | + |
| 11 | +/** |
| 12 | + * That's a pretty simple yet powerful class to build XML structures in jQuery-like style. |
| 13 | + * With no XML line actually written! |
| 14 | + * Uses DOM extension to manipulate XML data. |
| 15 | + * |
| 16 | + * ```php |
| 17 | + * <?php |
| 18 | + * $xml = new \Codeception\Util\XmlBuilder(); |
| 19 | + * $xml->users |
| 20 | + * ->user |
| 21 | + * ->val(1) |
| 22 | + * ->email |
| 23 | + |
| 24 | + * ->attr('valid','true') |
| 25 | + * ->parent() |
| 26 | + * ->cart |
| 27 | + * ->attr('empty','false') |
| 28 | + * ->items |
| 29 | + * ->item |
| 30 | + * ->val('useful item'); |
| 31 | + * ->parents('user') |
| 32 | + * ->active |
| 33 | + * ->val(1); |
| 34 | + * echo $xml; |
| 35 | + * ``` |
| 36 | + * |
| 37 | + * This will produce this XML |
| 38 | + * |
| 39 | + * ```xml |
| 40 | + * <?xml version="1.0"?> |
| 41 | + * <users> |
| 42 | + * <user> |
| 43 | + * 1 |
| 44 | + * <email valid="true">[email protected]</email> |
| 45 | + * <cart empty="false"> |
| 46 | + * <items> |
| 47 | + * <item>useful item</item> |
| 48 | + * </items> |
| 49 | + * </cart> |
| 50 | + * <active>1</active> |
| 51 | + * </user> |
| 52 | + * </users> |
| 53 | + * ``` |
| 54 | + * |
| 55 | + * ### Usage |
| 56 | + * |
| 57 | + * Builder uses chained calls. So each call to builder returns a builder object. |
| 58 | + * Except for `getDom` and `__toString` methods. |
| 59 | + * |
| 60 | + * * `$xml->node` - create new xml node and go inside of it. |
| 61 | + * * `$xml->node->val('value')` - sets the inner value of node |
| 62 | + * * `$xml->attr('name','value')` - set the attribute of node |
| 63 | + * * `$xml->parent()` - go back to parent node. |
| 64 | + * * `$xml->parents('user')` - go back through all parents to `user` node. |
| 65 | + * |
| 66 | + * Export: |
| 67 | + * |
| 68 | + * * `$xml->getDom` - get a DOMDocument object |
| 69 | + * * `$xml->__toString` - get a string representation of XML. |
| 70 | + * |
| 71 | + * [Source code](https://github.com/Codeception/Codeception/blob/5.0/src/Codeception/Util/XmlBuilder.php) |
| 72 | + */ |
| 73 | +class XmlBuilder |
| 74 | +{ |
| 75 | + protected DOMDocument $dom; |
| 76 | + |
| 77 | + protected DOMElement|DOMDocument $currentNode; |
| 78 | + |
| 79 | + public function __construct() |
| 80 | + { |
| 81 | + $this->dom = new DOMDocument(); |
| 82 | + $this->currentNode = $this->dom; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Appends child node |
| 87 | + */ |
| 88 | + public function __get(string $tag): XmlBuilder |
| 89 | + { |
| 90 | + $domElement = $this->dom->createElement($tag); |
| 91 | + $this->currentNode->appendChild($domElement); |
| 92 | + $this->currentNode = $domElement; |
| 93 | + return $this; |
| 94 | + } |
| 95 | + |
| 96 | + public function val($val): self |
| 97 | + { |
| 98 | + $this->currentNode->nodeValue = $val; |
| 99 | + return $this; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Sets attribute for current node |
| 104 | + */ |
| 105 | + public function attr(string $attr, string $val): self |
| 106 | + { |
| 107 | + $this->currentNode->setAttribute($attr, $val); |
| 108 | + return $this; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Traverses to parent |
| 113 | + */ |
| 114 | + public function parent(): self |
| 115 | + { |
| 116 | + $this->currentNode = $this->currentNode->parentNode; |
| 117 | + return $this; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Traverses to parent with $name |
| 122 | + * |
| 123 | + * @throws Exception |
| 124 | + */ |
| 125 | + public function parents(string $tag): self |
| 126 | + { |
| 127 | + $traverseNode = $this->currentNode; |
| 128 | + $elFound = false; |
| 129 | + while ($traverseNode->parentNode) { |
| 130 | + $traverseNode = $traverseNode->parentNode; |
| 131 | + if ($traverseNode->tagName === $tag) { |
| 132 | + $this->currentNode = $traverseNode; |
| 133 | + $elFound = true; |
| 134 | + break; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if (!$elFound) { |
| 139 | + throw new Exception("Parent {$tag} not found in XML"); |
| 140 | + } |
| 141 | + |
| 142 | + return $this; |
| 143 | + } |
| 144 | + |
| 145 | + public function __toString(): string |
| 146 | + { |
| 147 | + return $this->dom->saveXML(); |
| 148 | + } |
| 149 | + |
| 150 | + public function getDom(): DOMDocument |
| 151 | + { |
| 152 | + return $this->dom; |
| 153 | + } |
| 154 | +} |
0 commit comments