Sharee.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV\Xml\Element;
  4. use Sabre\DAV\Exception\BadRequest;
  5. use Sabre\DAV\Sharing\Plugin;
  6. use Sabre\DAV\Xml\Property\Href;
  7. use Sabre\DAV\Xml\Property\ShareAccess;
  8. use Sabre\Xml\Deserializer;
  9. use Sabre\Xml\Element;
  10. use Sabre\Xml\Reader;
  11. use Sabre\Xml\Writer;
  12. /**
  13. * This class represents the {DAV:}sharee element.
  14. *
  15. * @copyright Copyright (C) fruux GmbH. (https://fruux.com/)
  16. * @author Evert Pot (http://evertpot.com/)
  17. * @license http://sabre.io/license/ Modified BSD License
  18. */
  19. class Sharee implements Element
  20. {
  21. /**
  22. * A URL. Usually a mailto: address, could also be a principal url.
  23. * This uniquely identifies the sharee.
  24. *
  25. * @var string
  26. */
  27. public $href;
  28. /**
  29. * A local principal path. The server will do its best to locate the
  30. * principal uri based on the given uri. If we could find a local matching
  31. * principal uri, this property will contain the value.
  32. *
  33. * @var string|null
  34. */
  35. public $principal;
  36. /**
  37. * A list of WebDAV properties that describe the sharee. This might for
  38. * example contain a {DAV:}displayname with the real name of the user.
  39. *
  40. * @var array
  41. */
  42. public $properties = [];
  43. /**
  44. * Share access level. One of the Sabre\DAV\Sharing\Plugin::ACCESS
  45. * constants.
  46. *
  47. * Can be one of:
  48. *
  49. * ACCESS_READ
  50. * ACCESS_READWRITE
  51. * ACCESS_SHAREDOWNER
  52. * ACCESS_NOACCESS
  53. *
  54. * depending on context.
  55. *
  56. * @var int
  57. */
  58. public $access;
  59. /**
  60. * When a sharee is originally invited to a share, the sharer may add
  61. * a comment. This will be placed in this property.
  62. *
  63. * @var string
  64. */
  65. public $comment;
  66. /**
  67. * The status of the invite, should be one of the
  68. * Sabre\DAV\Sharing\Plugin::INVITE constants.
  69. *
  70. * @var int
  71. */
  72. public $inviteStatus;
  73. /**
  74. * Creates the object.
  75. *
  76. * $properties will be used to populate all internal properties.
  77. */
  78. public function __construct(array $properties = [])
  79. {
  80. foreach ($properties as $k => $v) {
  81. if (property_exists($this, $k)) {
  82. $this->$k = $v;
  83. } else {
  84. throw new \InvalidArgumentException('Unknown property: '.$k);
  85. }
  86. }
  87. }
  88. /**
  89. * The xmlSerialize method is called during xml writing.
  90. *
  91. * Use the $writer argument to write its own xml serialization.
  92. *
  93. * An important note: do _not_ create a parent element. Any element
  94. * implementing XmlSerializable should only ever write what's considered
  95. * its 'inner xml'.
  96. *
  97. * The parent of the current element is responsible for writing a
  98. * containing element.
  99. *
  100. * This allows serializers to be re-used for different element names.
  101. *
  102. * If you are opening new elements, you must also close them again.
  103. */
  104. public function xmlSerialize(Writer $writer)
  105. {
  106. $writer->write([
  107. new Href($this->href),
  108. '{DAV:}prop' => $this->properties,
  109. '{DAV:}share-access' => new ShareAccess($this->access),
  110. ]);
  111. switch ($this->inviteStatus) {
  112. case Plugin::INVITE_NORESPONSE:
  113. $writer->writeElement('{DAV:}invite-noresponse');
  114. break;
  115. case Plugin::INVITE_ACCEPTED:
  116. $writer->writeElement('{DAV:}invite-accepted');
  117. break;
  118. case Plugin::INVITE_DECLINED:
  119. $writer->writeElement('{DAV:}invite-declined');
  120. break;
  121. case Plugin::INVITE_INVALID:
  122. $writer->writeElement('{DAV:}invite-invalid');
  123. break;
  124. }
  125. }
  126. /**
  127. * The deserialize method is called during xml parsing.
  128. *
  129. * This method is called statically, this is because in theory this method
  130. * may be used as a type of constructor, or factory method.
  131. *
  132. * Often you want to return an instance of the current class, but you are
  133. * free to return other data as well.
  134. *
  135. * You are responsible for advancing the reader to the next element. Not
  136. * doing anything will result in a never-ending loop.
  137. *
  138. * If you just want to skip parsing for this element altogether, you can
  139. * just call $reader->next();
  140. *
  141. * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
  142. * the next element.
  143. *
  144. * @return mixed
  145. */
  146. public static function xmlDeserialize(Reader $reader)
  147. {
  148. // Temporarily override configuration
  149. $reader->pushContext();
  150. $reader->elementMap['{DAV:}share-access'] = 'Sabre\DAV\Xml\Property\ShareAccess';
  151. $reader->elementMap['{DAV:}prop'] = 'Sabre\Xml\Deserializer\keyValue';
  152. $elems = Deserializer\keyValue($reader, 'DAV:');
  153. // Restore previous configuration
  154. $reader->popContext();
  155. $sharee = new self();
  156. if (!isset($elems['href'])) {
  157. throw new BadRequest('Every {DAV:}sharee must have a {DAV:}href child-element');
  158. }
  159. $sharee->href = $elems['href'];
  160. if (isset($elems['prop'])) {
  161. $sharee->properties = $elems['prop'];
  162. }
  163. if (isset($elems['comment'])) {
  164. $sharee->comment = $elems['comment'];
  165. }
  166. if (!isset($elems['share-access'])) {
  167. throw new BadRequest('Every {DAV:}sharee must have a {DAV:}share-access child element');
  168. }
  169. $sharee->access = $elems['share-access']->getValue();
  170. return $sharee;
  171. }
  172. }