1<?php
2
3namespace Sabre\CardDAV\Xml\Filter;
4
5use Sabre\Xml\Reader;
6use Sabre\Xml\XmlDeserializable;
7
8/**
9 * AddressData parser.
10 *
11 * This class parses the {urn:ietf:params:xml:ns:carddav}address-data XML
12 * element, as defined in:
13 *
14 * http://tools.ietf.org/html/rfc6352#section-10.4
15 *
16 * This element is used in two distinct places, but this one specifically
17 * encodes the address-data element as it appears in the addressbook-query
18 * adressbook-multiget REPORT requests.
19 *
20 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
21 * @author Evert Pot (http://www.rooftopsolutions.nl/)
22 * @license http://sabre.io/license/ Modified BSD License
23 */
24class AddressData implements XmlDeserializable {
25
26    /**
27     * The deserialize method is called during xml parsing.
28     *
29     * This method is called statically, this is because in theory this method
30     * may be used as a type of constructor, or factory method.
31     *
32     * Often you want to return an instance of the current class, but you are
33     * free to return other data as well.
34     *
35     * You are responsible for advancing the reader to the next element. Not
36     * doing anything will result in a never-ending loop.
37     *
38     * If you just want to skip parsing for this element altogether, you can
39     * just call $reader->next();
40     *
41     * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
42     * the next element.
43     *
44     * @param Reader $reader
45     * @return mixed
46     */
47    static function xmlDeserialize(Reader $reader) {
48
49        $result = [
50            'contentType' => $reader->getAttribute('content-type') ?: 'text/vcard',
51            'version'     => $reader->getAttribute('version') ?: '3.0',
52        ];
53
54        $elems = (array)$reader->parseInnerTree();
55        $elems = array_filter($elems, function($element) {
56            return $element['name'] === '{urn:ietf:params:xml:ns:carddav}prop' &&
57                isset($element['attributes']['name']);
58        });
59        $result['addressDataProperties'] = array_map(function($element) {
60            return $element['attributes']['name'];
61        }, $elems);
62
63        return $result;
64    }
65
66}
67