1<?php
2
3namespace Sabre\CalDAV\Xml\Request;
4
5use Sabre\Xml\Reader;
6use Sabre\Xml\XmlDeserializable;
7use Sabre\CalDAV\Plugin;
8
9/**
10 * Share POST request parser
11 *
12 * This class parses the share POST request, as defined in:
13 *
14 * http://svn.calendarserver.org/repository/calendarserver/CalendarServer/trunk/doc/Extensions/caldav-sharing.txt
15 *
16 * @copyright Copyright (C) 2007-2015 fruux GmbH. (https://fruux.com/)
17 * @author Evert Pot (http://evertpot.com/)
18 * @license http://sabre.io/license/ Modified BSD License
19 */
20class Share implements XmlDeserializable {
21
22    /**
23     * The list of new people added or updated.
24     *
25     * Every element has the following keys:
26     * 1. href - An email address
27     * 2. commonName - Some name
28     * 3. summary - An optional description of the share
29     * 4. readOnly - true or false
30     *
31     * @var array
32     */
33    public $set = [];
34
35    /**
36     * List of people removed from the share list.
37     *
38     * The list is a flat list of email addresses (including mailto:).
39     *
40     * @var array
41     */
42    public $remove = [];
43
44    /**
45     * Constructor
46     *
47     * @param array $set
48     * @param array $remove
49     */
50    function __construct(array $set, array $remove) {
51
52        $this->set = $set;
53        $this->remove = $remove;
54
55    }
56
57    /**
58     * The deserialize method is called during xml parsing.
59     *
60     * This method is called statictly, this is because in theory this method
61     * may be used as a type of constructor, or factory method.
62     *
63     * Often you want to return an instance of the current class, but you are
64     * free to return other data as well.
65     *
66     * You are responsible for advancing the reader to the next element. Not
67     * doing anything will result in a never-ending loop.
68     *
69     * If you just want to skip parsing for this element altogether, you can
70     * just call $reader->next();
71     *
72     * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
73     * the next element.
74     *
75     * @param Reader $reader
76     * @return mixed
77     */
78    static function xmlDeserialize(Reader $reader) {
79
80        $elems = $reader->parseInnerTree([
81            '{' . Plugin::NS_CALENDARSERVER . '}set'    => 'Sabre\\Xml\\Element\\KeyValue',
82            '{' . Plugin::NS_CALENDARSERVER . '}remove' => 'Sabre\\Xml\\Element\\KeyValue',
83        ]);
84
85        $set = [];
86        $remove = [];
87
88        foreach ($elems as $elem) {
89            switch ($elem['name']) {
90
91                case '{' . Plugin::NS_CALENDARSERVER . '}set' :
92                    $sharee = $elem['value'];
93
94                    $sumElem = '{' . Plugin::NS_CALENDARSERVER . '}summary';
95                    $commonName = '{' . Plugin::NS_CALENDARSERVER . '}common-name';
96
97                    $set[] = [
98                        'href'       => $sharee['{DAV:}href'],
99                        'commonName' => isset($sharee[$commonName]) ? $sharee[$commonName] : null,
100                        'summary'    => isset($sharee[$sumElem]) ? $sharee[$sumElem] : null,
101                        'readOnly'   => !array_key_exists('{' . Plugin::NS_CALENDARSERVER . '}read-write', $sharee),
102                    ];
103                    break;
104
105                case '{' . Plugin::NS_CALENDARSERVER . '}remove' :
106                    $remove[] = $elem['value']['{DAV:}href'];
107                    break;
108
109            }
110        }
111
112        return new self($set, $remove);
113
114    }
115
116}
117