Base.php
6 months ago
Cdata.php
6 months ago
Elements.php
6 months ago
KeyValue.php
6 months ago
Uri.php
6 months ago
XmlFragment.php
6 months ago
XmlFragment.php
145 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AmeliaVendor\Sabre\Xml\Element; |
| 6 | |
| 7 | use AmeliaVendor\Sabre\Xml\Element; |
| 8 | use AmeliaVendor\Sabre\Xml\Reader; |
| 9 | use AmeliaVendor\Sabre\Xml\Writer; |
| 10 | |
| 11 | /** |
| 12 | * The XmlFragment element allows you to extract a portion of your xml tree, |
| 13 | * and get a well-formed xml string. |
| 14 | * |
| 15 | * This goes a bit beyond `innerXml` and friends, as we'll also match all the |
| 16 | * correct namespaces. |
| 17 | * |
| 18 | * Please note that the XML fragment: |
| 19 | * |
| 20 | * 1. Will not have an <?xml declaration. |
| 21 | * 2. Or a DTD |
| 22 | * 3. It will have all the relevant xmlns attributes. |
| 23 | * 4. It may not have a root element. |
| 24 | */ |
| 25 | class XmlFragment implements Element |
| 26 | { |
| 27 | /** |
| 28 | * The inner XML value. |
| 29 | */ |
| 30 | protected string $xml; |
| 31 | |
| 32 | /** |
| 33 | * Constructor. |
| 34 | */ |
| 35 | public function __construct(string $xml) |
| 36 | { |
| 37 | $this->xml = $xml; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Returns the inner XML document. |
| 42 | */ |
| 43 | public function getXml(): string |
| 44 | { |
| 45 | return $this->xml; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * The xmlSerialize method is called during xml writing. |
| 50 | * |
| 51 | * Use the $writer argument to write its own xml serialization. |
| 52 | * |
| 53 | * An important note: do _not_ create a parent element. Any element |
| 54 | * implementing XmlSerializable should only ever write what's considered |
| 55 | * its 'inner xml'. |
| 56 | * |
| 57 | * The parent of the current element is responsible for writing a |
| 58 | * containing element. |
| 59 | * |
| 60 | * This allows serializers to be re-used for different element names. |
| 61 | * |
| 62 | * If you are opening new elements, you must also close them again. |
| 63 | */ |
| 64 | public function xmlSerialize(Writer $writer): void |
| 65 | { |
| 66 | $reader = new Reader(); |
| 67 | |
| 68 | // Wrapping the xml in a container, so root-less values can still be |
| 69 | // parsed. |
| 70 | $xml = <<<XML |
| 71 | <?xml version="1.0"?> |
| 72 | <xml-fragment xmlns="http://sabre.io/ns">{$this->getXml()}</xml-fragment> |
| 73 | XML; |
| 74 | |
| 75 | $reader->xml($xml); |
| 76 | |
| 77 | while ($reader->read()) { |
| 78 | if ($reader->depth < 1) { |
| 79 | // Skipping the root node. |
| 80 | continue; |
| 81 | } |
| 82 | |
| 83 | switch ($reader->nodeType) { |
| 84 | case Reader::ELEMENT: |
| 85 | $writer->startElement( |
| 86 | (string) $reader->getClark() |
| 87 | ); |
| 88 | $empty = $reader->isEmptyElement; |
| 89 | while ($reader->moveToNextAttribute()) { |
| 90 | switch ($reader->namespaceURI) { |
| 91 | case '': |
| 92 | $writer->writeAttribute($reader->localName, $reader->value); |
| 93 | break; |
| 94 | case 'http://www.w3.org/2000/xmlns/': |
| 95 | // Skip namespace declarations |
| 96 | break; |
| 97 | default: |
| 98 | $writer->writeAttribute((string) $reader->getClark(), $reader->value); |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | if ($empty) { |
| 103 | $writer->endElement(); |
| 104 | } |
| 105 | break; |
| 106 | case Reader::CDATA: |
| 107 | case Reader::TEXT: |
| 108 | $writer->text( |
| 109 | $reader->value |
| 110 | ); |
| 111 | break; |
| 112 | case Reader::END_ELEMENT: |
| 113 | $writer->endElement(); |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * The deserialize method is called during xml parsing. |
| 121 | * |
| 122 | * This method is called statically, this is because in theory this method |
| 123 | * may be used as a type of constructor, or factory method. |
| 124 | * |
| 125 | * Often you want to return an instance of the current class, but you are |
| 126 | * free to return other data as well. |
| 127 | * |
| 128 | * You are responsible for advancing the reader to the next element. Not |
| 129 | * doing anything will result in a never-ending loop. |
| 130 | * |
| 131 | * If you just want to skip parsing for this element altogether, you can |
| 132 | * just call $reader->next(); |
| 133 | * |
| 134 | * $reader->parseInnerTree() will parse the entire sub-tree, and advance to |
| 135 | * the next element. |
| 136 | */ |
| 137 | public static function xmlDeserialize(Reader $reader) |
| 138 | { |
| 139 | $result = new self($reader->readInnerXml()); |
| 140 | $reader->next(); |
| 141 | |
| 142 | return $result; |
| 143 | } |
| 144 | } |
| 145 |