Complex.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV\Xml\Property;
  4. use Sabre\Xml\Element\XmlFragment;
  5. use Sabre\Xml\Reader;
  6. /**
  7. * This class represents a 'complex' property that didn't have a default
  8. * decoder.
  9. *
  10. * It's basically a container for an xml snippet.
  11. *
  12. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  13. * @author Evert Pot (http://evertpot.com/)
  14. * @license http://sabre.io/license/ Modified BSD License
  15. */
  16. class Complex extends XmlFragment
  17. {
  18. /**
  19. * The deserialize method is called during xml parsing.
  20. *
  21. * This method is called statically, this is because in theory this method
  22. * may be used as a type of constructor, or factory method.
  23. *
  24. * Often you want to return an instance of the current class, but you are
  25. * free to return other data as well.
  26. *
  27. * You are responsible for advancing the reader to the next element. Not
  28. * doing anything will result in a never-ending loop.
  29. *
  30. * If you just want to skip parsing for this element altogether, you can
  31. * just call $reader->next();
  32. *
  33. * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
  34. * the next element.
  35. *
  36. * @return mixed
  37. */
  38. public static function xmlDeserialize(Reader $reader)
  39. {
  40. $xml = $reader->readInnerXml();
  41. if (Reader::ELEMENT === $reader->nodeType && $reader->isEmptyElement) {
  42. // Easy!
  43. $reader->next();
  44. return null;
  45. }
  46. // Now we have a copy of the inner xml, we need to traverse it to get
  47. // all the strings. If there's no non-string data, we just return the
  48. // string, otherwise we return an instance of this class.
  49. $reader->read();
  50. $nonText = false;
  51. $text = '';
  52. while (true) {
  53. switch ($reader->nodeType) {
  54. case Reader::ELEMENT:
  55. $nonText = true;
  56. $reader->next();
  57. continue 2;
  58. case Reader::TEXT:
  59. case Reader::CDATA:
  60. $text .= $reader->value;
  61. break;
  62. case Reader::END_ELEMENT:
  63. break 2;
  64. }
  65. $reader->read();
  66. }
  67. // Make sure we advance the cursor one step further.
  68. $reader->read();
  69. if ($nonText) {
  70. $new = new self($xml);
  71. return $new;
  72. } else {
  73. return $text;
  74. }
  75. }
  76. }