MultiStatus.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV\Xml\Response;
  4. use Sabre\Xml\Element;
  5. use Sabre\Xml\Reader;
  6. use Sabre\Xml\Writer;
  7. /**
  8. * WebDAV MultiStatus parser.
  9. *
  10. * This class parses the {DAV:}multistatus response, as defined in:
  11. * https://tools.ietf.org/html/rfc4918#section-14.16
  12. *
  13. * And it also adds the {DAV:}synctoken change from:
  14. * http://tools.ietf.org/html/rfc6578#section-6.4
  15. *
  16. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  17. * @author Evert Pot (http://evertpot.com/)
  18. * @license http://sabre.io/license/ Modified BSD License
  19. */
  20. class MultiStatus implements Element
  21. {
  22. /**
  23. * The responses.
  24. *
  25. * @var \Sabre\DAV\Xml\Element\Response[]
  26. */
  27. protected $responses;
  28. /**
  29. * A sync token (from RFC6578).
  30. *
  31. * @var string
  32. */
  33. protected $syncToken;
  34. /**
  35. * Constructor.
  36. *
  37. * @param \Sabre\DAV\Xml\Element\Response[] $responses
  38. * @param string $syncToken
  39. */
  40. public function __construct(array $responses, $syncToken = null)
  41. {
  42. $this->responses = $responses;
  43. $this->syncToken = $syncToken;
  44. }
  45. /**
  46. * Returns the response list.
  47. *
  48. * @return \Sabre\DAV\Xml\Element\Response[]
  49. */
  50. public function getResponses()
  51. {
  52. return $this->responses;
  53. }
  54. /**
  55. * Returns the sync-token, if available.
  56. *
  57. * @return string|null
  58. */
  59. public function getSyncToken()
  60. {
  61. return $this->syncToken;
  62. }
  63. /**
  64. * The serialize method is called during xml writing.
  65. *
  66. * It should use the $writer argument to encode this object into XML.
  67. *
  68. * Important note: it is not needed to create the parent element. The
  69. * parent element is already created, and we only have to worry about
  70. * attributes, child elements and text (if any).
  71. *
  72. * Important note 2: If you are writing any new elements, you are also
  73. * responsible for closing them.
  74. */
  75. public function xmlSerialize(Writer $writer)
  76. {
  77. foreach ($this->getResponses() as $response) {
  78. $writer->writeElement('{DAV:}response', $response);
  79. }
  80. if ($syncToken = $this->getSyncToken()) {
  81. $writer->writeElement('{DAV:}sync-token', $syncToken);
  82. }
  83. }
  84. /**
  85. * The deserialize method is called during xml parsing.
  86. *
  87. * This method is called statically, this is because in theory this method
  88. * may be used as a type of constructor, or factory method.
  89. *
  90. * Often you want to return an instance of the current class, but you are
  91. * free to return other data as well.
  92. *
  93. * You are responsible for advancing the reader to the next element. Not
  94. * doing anything will result in a never-ending loop.
  95. *
  96. * If you just want to skip parsing for this element altogether, you can
  97. * just call $reader->next();
  98. *
  99. * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
  100. * the next element.
  101. *
  102. * @return mixed
  103. */
  104. public static function xmlDeserialize(Reader $reader)
  105. {
  106. $elementMap = $reader->elementMap;
  107. $elementMap['{DAV:}prop'] = 'Sabre\\DAV\\Xml\\Element\\Prop';
  108. $elements = $reader->parseInnerTree($elementMap);
  109. $responses = [];
  110. $syncToken = null;
  111. if ($elements) {
  112. foreach ($elements as $elem) {
  113. if ('{DAV:}response' === $elem['name']) {
  114. $responses[] = $elem['value'];
  115. }
  116. if ('{DAV:}sync-token' === $elem['name']) {
  117. $syncToken = $elem['value'];
  118. }
  119. }
  120. }
  121. return new self($responses, $syncToken);
  122. }
  123. }