XmlFragment.php 4.2 KB

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