1<?php
2
3namespace Sabre\DAV\Xml\Property;
4
5use Sabre\DAV;
6use Sabre\DAV\Browser\HtmlOutput;
7use Sabre\DAV\Browser\HtmlOutputHelper;
8use Sabre\Xml\Writer;
9use Sabre\Xml\XmlSerializable;
10
11/**
12 * supported-report-set property.
13 *
14 * This property is defined in RFC3253, but since it's
15 * so common in other webdav-related specs, it is part of the core server.
16 *
17 * This property is defined here:
18 * http://tools.ietf.org/html/rfc3253#section-3.1.5
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 SupportedReportSet implements XmlSerializable, HtmlOutput {
25
26    /**
27     * List of reports
28     *
29     * @var array
30     */
31    protected $reports = [];
32
33    /**
34     * Creates the property
35     *
36     * Any reports passed in the constructor
37     * should be valid report-types in clark-notation.
38     *
39     * Either a string or an array of strings must be passed.
40     *
41     * @param string|string[] $reports
42     */
43    function __construct($reports = null) {
44
45        if (!is_null($reports))
46            $this->addReport($reports);
47
48    }
49
50    /**
51     * Adds a report to this property
52     *
53     * The report must be a string in clark-notation.
54     * Multiple reports can be specified as an array.
55     *
56     * @param mixed $report
57     * @return void
58     */
59    function addReport($report) {
60
61        $report = (array)$report;
62
63        foreach ($report as $r) {
64
65            if (!preg_match('/^{([^}]*)}(.*)$/', $r))
66                throw new DAV\Exception('Reportname must be in clark-notation');
67
68            $this->reports[] = $r;
69
70        }
71
72    }
73
74    /**
75     * Returns the list of supported reports
76     *
77     * @return string[]
78     */
79    function getValue() {
80
81        return $this->reports;
82
83    }
84
85    /**
86     * Returns true or false if the property contains a specific report.
87     *
88     * @param string $reportName
89     * @return bool
90     */
91    function has($reportName) {
92
93        return in_array(
94            $reportName,
95            $this->reports
96        );
97
98    }
99
100    /**
101     * The xmlSerialize method is called during xml writing.
102     *
103     * Use the $writer argument to write its own xml serialization.
104     *
105     * An important note: do _not_ create a parent element. Any element
106     * implementing XmlSerializable should only ever write what's considered
107     * its 'inner xml'.
108     *
109     * The parent of the current element is responsible for writing a
110     * containing element.
111     *
112     * This allows serializers to be re-used for different element names.
113     *
114     * If you are opening new elements, you must also close them again.
115     *
116     * @param Writer $writer
117     * @return void
118     */
119    function xmlSerialize(Writer $writer) {
120
121        foreach ($this->getValue() as $val) {
122            $writer->startElement('{DAV:}supported-report');
123            $writer->startElement('{DAV:}report');
124            $writer->writeElement($val);
125            $writer->endElement();
126            $writer->endElement();
127        }
128
129    }
130
131    /**
132     * Generate html representation for this value.
133     *
134     * The html output is 100% trusted, and no effort is being made to sanitize
135     * it. It's up to the implementor to sanitize user provided values.
136     *
137     * The output must be in UTF-8.
138     *
139     * The baseUri parameter is a url to the root of the application, and can
140     * be used to construct local links.
141     *
142     * @param HtmlOutputHelper $html
143     * @return string
144     */
145    function toHtml(HtmlOutputHelper $html) {
146
147        return implode(
148            ', ',
149            array_map([$html, 'xmlName'], $this->getValue())
150        );
151
152    }
153
154}
155