summaryrefslogtreecommitdiffstats
path: root/apps/dav/lib/carddav/sharing/xml/sharerequest.php
blob: 175c5ffc306214abb544035b8a7eced2533b949a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php

namespace OCA\DAV\CardDAV\Sharing\Xml;

use Sabre\Xml\Reader;
use Sabre\Xml\XmlDeserializable;

class ShareRequest implements XmlDeserializable {

    public $set = [];

    public $remove = [];

    /**
     * Constructor
     *
     * @param array $set
     * @param array $remove
     */
    function __construct(array $set, array $remove) {

        $this->set = $set;
        $this->remove = $remove;

    }

    static function xmlDeserialize(Reader $reader) {

        $elems = $reader->parseInnerTree([
            '{' . \Sabre\CardDAV\Plugin::NS_CARDDAV. '}set'    => 'Sabre\\Xml\\Element\\KeyValue',
            '{' . \Sabre\CardDAV\Plugin::NS_CARDDAV . '}remove' => 'Sabre\\Xml\\Element\\KeyValue',
        ]);

        $set = [];
        $remove = [];

        foreach ($elems as $elem) {
            switch ($elem['name']) {

                case '{' . \Sabre\CardDAV\Plugin::NS_CARDDAV . '}set' :
                    $sharee = $elem['value'];

                    $sumElem = '{' . \Sabre\CardDAV\Plugin::NS_CARDDAV . '}summary';
                    $commonName = '{' . \Sabre\CardDAV\Plugin::NS_CARDDAV . '}common-name';

                    $set[] = [
                        'href'       => $sharee['{DAV:}href'],
                        'commonName' => isset($sharee[$commonName]) ? $sharee[$commonName] : null,
                        'summary'    => isset($sharee[$sumElem]) ? $sharee[$sumElem] : null,
                        'readOnly'   => !array_key_exists('{' . \Sabre\CardDAV\Plugin::NS_CARDDAV . '}read-write', $sharee),
                    ];
                    break;

                case '{' . \Sabre\CardDAV\Plugin::NS_CARDDAV . '}remove' :
                    $remove[] = $elem['value']['{DAV:}href'];
                    break;

            }
        }

        return new self($set, $remove);

    }

}