InviteReply.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\CalDAV\Xml\Notification;
  4. use Sabre\CalDAV;
  5. use Sabre\CalDAV\SharingPlugin;
  6. use Sabre\DAV;
  7. use Sabre\Xml\Writer;
  8. /**
  9. * This class represents the cs:invite-reply notification element.
  10. *
  11. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  12. * @author Evert Pot (http://evertpot.com/)
  13. * @license http://sabre.io/license/ Modified BSD License
  14. */
  15. class InviteReply implements NotificationInterface
  16. {
  17. /**
  18. * A unique id for the message.
  19. *
  20. * @var string
  21. */
  22. protected $id;
  23. /**
  24. * Timestamp of the notification.
  25. *
  26. * @var \DateTime
  27. */
  28. protected $dtStamp;
  29. /**
  30. * The unique id of the notification this was a reply to.
  31. *
  32. * @var string
  33. */
  34. protected $inReplyTo;
  35. /**
  36. * A url to the recipient of the original (!) notification.
  37. *
  38. * @var string
  39. */
  40. protected $href;
  41. /**
  42. * The type of message, see the SharingPlugin::STATUS_ constants.
  43. *
  44. * @var int
  45. */
  46. protected $type;
  47. /**
  48. * A url to the shared calendar.
  49. *
  50. * @var string
  51. */
  52. protected $hostUrl;
  53. /**
  54. * A description of the share request.
  55. *
  56. * @var string
  57. */
  58. protected $summary;
  59. /**
  60. * Notification Etag.
  61. *
  62. * @var string
  63. */
  64. protected $etag;
  65. /**
  66. * Creates the Invite Reply Notification.
  67. *
  68. * This constructor receives an array with the following elements:
  69. *
  70. * * id - A unique id
  71. * * etag - The etag
  72. * * dtStamp - A DateTime object with a timestamp for the notification.
  73. * * inReplyTo - This should refer to the 'id' of the notification
  74. * this is a reply to.
  75. * * type - The type of notification, see SharingPlugin::STATUS_*
  76. * constants for details.
  77. * * hostUrl - A url to the shared calendar.
  78. * * summary - Description of the share, can be the same as the
  79. * calendar, but may also be modified (optional).
  80. */
  81. public function __construct(array $values)
  82. {
  83. $required = [
  84. 'id',
  85. 'etag',
  86. 'href',
  87. 'dtStamp',
  88. 'inReplyTo',
  89. 'type',
  90. 'hostUrl',
  91. ];
  92. foreach ($required as $item) {
  93. if (!isset($values[$item])) {
  94. throw new \InvalidArgumentException($item.' is a required constructor option');
  95. }
  96. }
  97. foreach ($values as $key => $value) {
  98. if (!property_exists($this, $key)) {
  99. throw new \InvalidArgumentException('Unknown option: '.$key);
  100. }
  101. $this->$key = $value;
  102. }
  103. }
  104. /**
  105. * The xmlSerialize method is called during xml writing.
  106. *
  107. * Use the $writer argument to write its own xml serialization.
  108. *
  109. * An important note: do _not_ create a parent element. Any element
  110. * implementing XmlSerializable should only ever write what's considered
  111. * its 'inner xml'.
  112. *
  113. * The parent of the current element is responsible for writing a
  114. * containing element.
  115. *
  116. * This allows serializers to be re-used for different element names.
  117. *
  118. * If you are opening new elements, you must also close them again.
  119. */
  120. public function xmlSerialize(Writer $writer)
  121. {
  122. $writer->writeElement('{'.CalDAV\Plugin::NS_CALENDARSERVER.'}invite-reply');
  123. }
  124. /**
  125. * This method serializes the entire notification, as it is used in the
  126. * response body.
  127. */
  128. public function xmlSerializeFull(Writer $writer)
  129. {
  130. $cs = '{'.CalDAV\Plugin::NS_CALENDARSERVER.'}';
  131. $this->dtStamp->setTimezone(new \DateTimeZone('GMT'));
  132. $writer->writeElement($cs.'dtstamp', $this->dtStamp->format('Ymd\\THis\\Z'));
  133. $writer->startElement($cs.'invite-reply');
  134. $writer->writeElement($cs.'uid', $this->id);
  135. $writer->writeElement($cs.'in-reply-to', $this->inReplyTo);
  136. $writer->writeElement('{DAV:}href', $this->href);
  137. switch ($this->type) {
  138. case DAV\Sharing\Plugin::INVITE_ACCEPTED:
  139. $writer->writeElement($cs.'invite-accepted');
  140. break;
  141. case DAV\Sharing\Plugin::INVITE_DECLINED:
  142. $writer->writeElement($cs.'invite-declined');
  143. break;
  144. }
  145. $writer->writeElement($cs.'hosturl', [
  146. '{DAV:}href' => $writer->contextUri.$this->hostUrl,
  147. ]);
  148. if ($this->summary) {
  149. $writer->writeElement($cs.'summary', $this->summary);
  150. }
  151. $writer->endElement(); // invite-reply
  152. }
  153. /**
  154. * Returns a unique id for this notification.
  155. *
  156. * This is just the base url. This should generally be some kind of unique
  157. * id.
  158. *
  159. * @return string
  160. */
  161. public function getId()
  162. {
  163. return $this->id;
  164. }
  165. /**
  166. * Returns the ETag for this notification.
  167. *
  168. * The ETag must be surrounded by literal double-quotes.
  169. *
  170. * @return string
  171. */
  172. public function getETag()
  173. {
  174. return $this->etag;
  175. }
  176. }