1<?php
2
3namespace Sabre\DAVACL\Xml\Property;
4
5use Sabre\DAV\Browser\HtmlOutput;
6use Sabre\DAV\Browser\HtmlOutputHelper;
7use Sabre\Xml\Element;
8use Sabre\Xml\Reader;
9use Sabre\Xml\Writer;
10
11/**
12 * CurrentUserPrivilegeSet
13 *
14 * This class represents the current-user-privilege-set property. When
15 * requested, it contain all the privileges a user has on a specific node.
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 CurrentUserPrivilegeSet implements Element, HtmlOutput {
22
23    /**
24     * List of privileges
25     *
26     * @var array
27     */
28    private $privileges;
29
30    /**
31     * Creates the object
32     *
33     * Pass the privileges in clark-notation
34     *
35     * @param array $privileges
36     */
37    function __construct(array $privileges) {
38
39        $this->privileges = $privileges;
40
41    }
42
43    /**
44     * The xmlSerialize method is called during xml writing.
45     *
46     * Use the $writer argument to write its own xml serialization.
47     *
48     * An important note: do _not_ create a parent element. Any element
49     * implementing XmlSerializable should only ever write what's considered
50     * its 'inner xml'.
51     *
52     * The parent of the current element is responsible for writing a
53     * containing element.
54     *
55     * This allows serializers to be re-used for different element names.
56     *
57     * If you are opening new elements, you must also close them again.
58     *
59     * @param Writer $writer
60     * @return void
61     */
62    function xmlSerialize(Writer $writer) {
63
64        foreach ($this->privileges as $privName) {
65
66            $writer->startElement('{DAV:}privilege');
67            $writer->writeElement($privName);
68            $writer->endElement();
69
70        }
71
72
73    }
74
75    /**
76     * Returns true or false, whether the specified principal appears in the
77     * list.
78     *
79     * @param string $privilegeName
80     * @return bool
81     */
82    function has($privilegeName) {
83
84        return in_array($privilegeName, $this->privileges);
85
86    }
87
88    /**
89     * Returns the list of privileges.
90     *
91     * @return array
92     */
93    function getValue() {
94
95        return $this->privileges;
96
97    }
98
99    /**
100     * The deserialize method is called during xml parsing.
101     *
102     * This method is called statically, this is because in theory this method
103     * may be used as a type of constructor, or factory method.
104     *
105     * Often you want to return an instance of the current class, but you are
106     * free to return other data as well.
107     *
108     * You are responsible for advancing the reader to the next element. Not
109     * doing anything will result in a never-ending loop.
110     *
111     * If you just want to skip parsing for this element altogether, you can
112     * just call $reader->next();
113     *
114     * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
115     * the next element.
116     *
117     * @param Reader $reader
118     * @return mixed
119     */
120    static function xmlDeserialize(Reader $reader) {
121
122        $result = [];
123
124        $tree = $reader->parseInnerTree(['{DAV:}privilege' => 'Sabre\\Xml\\Element\\Elements']);
125        foreach ($tree as $element) {
126            if ($element['name'] !== '{DAV:}privilege') {
127                continue;
128            }
129            $result[] = $element['value'][0];
130        }
131        return new self($result);
132
133    }
134
135    /**
136     * Generate html representation for this value.
137     *
138     * The html output is 100% trusted, and no effort is being made to sanitize
139     * it. It's up to the implementor to sanitize user provided values.
140     *
141     * The output must be in UTF-8.
142     *
143     * The baseUri parameter is a url to the root of the application, and can
144     * be used to construct local links.
145     *
146     * @param HtmlOutputHelper $html
147     * @return string
148     */
149    function toHtml(HtmlOutputHelper $html) {
150
151        return implode(
152            ', ',
153            array_map([$html, 'xmlName'], $this->getValue())
154        );
155
156    }
157
158
159}
160