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