1<?php
2
3namespace Sabre\CalDAV\Xml\Request;
4
5use Sabre\Xml\Reader;
6use Sabre\Xml\XmlDeserializable;
7use Sabre\Xml\Element\KeyValue;
8use Sabre\DAV\Exception\BadRequest;
9use Sabre\CalDAV\Plugin;
10use Sabre\CalDAV\SharingPlugin;
11
12/**
13 * Invite-reply POST request parser
14 *
15 * This class parses the invite-reply POST request, as defined in:
16 *
17 * http://svn.calendarserver.org/repository/calendarserver/CalendarServer/trunk/doc/Extensions/caldav-sharing.txt
18 *
19 * @copyright Copyright (C) 2007-2015 fruux GmbH. (https://fruux.com/)
20 * @author Evert Pot (http://evertpot.com/)
21 * @license http://sabre.io/license/ Modified BSD License
22 */
23class InviteReply implements XmlDeserializable {
24
25    /**
26     * The sharee calendar user address.
27     *
28     * This is the address that the original invite was set to
29     *
30     * @var string
31     */
32    public $href;
33
34    /**
35     * The uri to the calendar that was being shared.
36     *
37     * @var string
38     */
39    public $calendarUri;
40
41    /**
42     * The id of the invite message that's being responded to
43     *
44     * @var string
45     */
46    public $inReplyTo;
47
48    /**
49     * An optional message
50     *
51     * @var string
52     */
53    public $summary;
54
55    /**
56     * Either SharingPlugin::STATUS_ACCEPTED or SharingPlugin::STATUS_DECLINED.
57     *
58     * @var int
59     */
60    public $status;
61
62    /**
63     * Constructor
64     *
65     * @param string $href
66     * @param string $calendarUri
67     * @param string $inReplyTo
68     * @param string $summary
69     * @param int $status
70     */
71    function __construct($href, $calendarUri, $inReplyTo, $summary, $status) {
72
73        $this->href = $href;
74        $this->calendarUri = $calendarUri;
75        $this->inReplyTo = $inReplyTo;
76        $this->summary = $summary;
77        $this->status = $status;
78
79    }
80
81    /**
82     * The deserialize method is called during xml parsing.
83     *
84     * This method is called statictly, this is because in theory this method
85     * may be used as a type of constructor, or factory method.
86     *
87     * Often you want to return an instance of the current class, but you are
88     * free to return other data as well.
89     *
90     * You are responsible for advancing the reader to the next element. Not
91     * doing anything will result in a never-ending loop.
92     *
93     * If you just want to skip parsing for this element altogether, you can
94     * just call $reader->next();
95     *
96     * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
97     * the next element.
98     *
99     * @param Reader $reader
100     * @return mixed
101     */
102    static function xmlDeserialize(Reader $reader) {
103
104        $elems = KeyValue::xmlDeserialize($reader);
105
106        $href = null;
107        $calendarUri = null;
108        $inReplyTo = null;
109        $summary = null;
110        $status = null;
111
112        foreach ($elems as $name => $value) {
113
114            switch ($name) {
115
116                case '{' . Plugin::NS_CALENDARSERVER . '}hosturl' :
117                    foreach ($value as $bla) {
118                        if ($bla['name'] === '{DAV:}href') {
119                            $calendarUri = $bla['value'];
120                        }
121                    }
122                    break;
123                case '{' . Plugin::NS_CALENDARSERVER . '}invite-accepted' :
124                    $status = SharingPlugin::STATUS_ACCEPTED;
125                    break;
126                case '{' . Plugin::NS_CALENDARSERVER . '}invite-declined' :
127                    $status = SharingPlugin::STATUS_DECLINED;
128                    break;
129                case '{' . Plugin::NS_CALENDARSERVER . '}in-reply-to' :
130                    $inReplyTo = $value;
131                    break;
132                case '{' . Plugin::NS_CALENDARSERVER . '}summary' :
133                    $summary = $value;
134                    break;
135                case '{DAV:}href' :
136                    $href = $value;
137                    break;
138            }
139
140        }
141        if (is_null($calendarUri)) {
142            throw new BadRequest('The {http://calendarserver.org/ns/}hosturl/{DAV:}href element must exist');
143        }
144
145        return new self($href, $calendarUri, $inReplyTo, $summary, $status);
146
147    }
148
149}
150