1<?php
2
3namespace Sabre\CardDAV\Xml\Request;
4
5use Sabre\CardDAV\Plugin;
6use Sabre\Uri;
7use Sabre\Xml\Reader;
8use Sabre\Xml\XmlDeserializable;
9
10/**
11 * AddressBookMultiGetReport request parser.
12 *
13 * This class parses the {urn:ietf:params:xml:ns:carddav}addressbook-multiget
14 * REPORT, as defined in:
15 *
16 * http://tools.ietf.org/html/rfc6352#section-8.7
17 *
18 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
19 * @author Evert Pot (http://www.rooftopsolutions.nl/)
20 * @license http://sabre.io/license/ Modified BSD License
21 */
22class AddressBookMultiGetReport implements XmlDeserializable {
23
24    /**
25     * An array with requested properties.
26     *
27     * @var array
28     */
29    public $properties;
30
31    /**
32     * This is an array with the urls that are being requested.
33     *
34     * @var array
35     */
36    public $hrefs;
37
38    /**
39     * The mimetype of the content that should be returend. Usually
40     * text/vcard.
41     *
42     * @var string
43     */
44    public $contentType = null;
45
46    /**
47     * The version of vcard data that should be returned. Usually 3.0,
48     * referring to vCard 3.0.
49     *
50     * @var string
51     */
52    public $version = null;
53
54    /**
55     * The deserialize method is called during xml parsing.
56     *
57     * This method is called statically, this is because in theory this method
58     * may be used as a type of constructor, or factory method.
59     *
60     * Often you want to return an instance of the current class, but you are
61     * free to return other data as well.
62     *
63     * You are responsible for advancing the reader to the next element. Not
64     * doing anything will result in a never-ending loop.
65     *
66     * If you just want to skip parsing for this element altogether, you can
67     * just call $reader->next();
68     *
69     * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
70     * the next element.
71     *
72     * @param Reader $reader
73     * @return mixed
74     */
75    static function xmlDeserialize(Reader $reader) {
76
77        $elems = $reader->parseInnerTree([
78            '{urn:ietf:params:xml:ns:carddav}address-data' => 'Sabre\\CardDAV\\Xml\\Filter\\AddressData',
79            '{DAV:}prop'                                   => 'Sabre\\Xml\\Element\\KeyValue',
80        ]);
81
82        $newProps = [
83            'hrefs'      => [],
84            'properties' => []
85        ];
86
87        foreach ($elems as $elem) {
88
89            switch ($elem['name']) {
90
91                case '{DAV:}prop' :
92                    $newProps['properties'] = array_keys($elem['value']);
93                    if (isset($elem['value']['{' . Plugin::NS_CARDDAV . '}address-data'])) {
94                        $newProps += $elem['value']['{' . Plugin::NS_CARDDAV . '}address-data'];
95                    }
96                    break;
97                case '{DAV:}href' :
98                    $newProps['hrefs'][] = Uri\resolve($reader->contextUri, $elem['value']);
99                    break;
100
101            }
102
103        }
104
105        $obj = new self();
106        foreach ($newProps as $key => $value) {
107            $obj->$key = $value;
108        }
109        return $obj;
110
111    }
112
113}
114