1<?php
2
3namespace Sabre\DAVACL\Xml\Request;
4
5use Sabre\Xml\Reader;
6use Sabre\Xml\XmlDeserializable;
7
8/**
9 * ExpandProperty request parser.
10 *
11 * This class parses the {DAV:}expand-property REPORT, as defined in:
12 *
13 * http://tools.ietf.org/html/rfc3253#section-3.8
14 *
15 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
16 * @author Evert Pot (http://evertpot.com/)
17 * @license http://sabre.io/license/ Modified BSD License
18 */
19class ExpandPropertyReport implements XmlDeserializable {
20
21    /**
22     * An array with requested properties.
23     *
24     * The requested properties will be used as keys in this array. The value
25     * is normally null.
26     *
27     * If the value is an array though, it means the property must be expanded.
28     * Within the array, the sub-properties, which themselves may be null or
29     * arrays.
30     *
31     * @var array
32     */
33    public $properties;
34
35    /**
36     * The deserialize method is called during xml parsing.
37     *
38     * This method is called statically, this is because in theory this method
39     * may be used as a type of constructor, or factory method.
40     *
41     * Often you want to return an instance of the current class, but you are
42     * free to return other data as well.
43     *
44     * You are responsible for advancing the reader to the next element. Not
45     * doing anything will result in a never-ending loop.
46     *
47     * If you just want to skip parsing for this element altogether, you can
48     * just call $reader->next();
49     *
50     * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
51     * the next element.
52     *
53     * @param Reader $reader
54     * @return mixed
55     */
56    static function xmlDeserialize(Reader $reader) {
57
58        $elems = $reader->parseInnerTree();
59
60        $obj = new self();
61        $obj->properties = self::traverse($elems);
62
63        return $obj;
64
65    }
66
67    /**
68     * This method is used by deserializeXml, to recursively parse the
69     * {DAV:}property elements.
70     *
71     * @param array $elems
72     * @return void
73     */
74    private static function traverse($elems) {
75
76        $result = [];
77
78        foreach ($elems as $elem) {
79
80            if ($elem['name'] !== '{DAV:}property') {
81                continue;
82            }
83
84            $namespace = isset($elem['attributes']['namespace']) ?
85                $elem['attributes']['namespace'] :
86                'DAV:';
87
88            $propName = '{' . $namespace . '}' . $elem['attributes']['name'];
89
90            $value = null;
91            if (is_array($elem['value'])) {
92                $value = self::traverse($elem['value']);
93            }
94
95            $result[$propName] = $value;
96
97        }
98
99        return $result;
100
101    }
102
103}
104