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