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