PageRenderTime 102ms CodeModel.GetById 60ms app.highlight 4ms RepoModel.GetById 36ms app.codeStats 0ms

/lib/Sabre/DAV/Property/SupportedReportSet.php

https://github.com/KOLANICH/SabreDAV
PHP | 126 lines | 43 code | 31 blank | 52 comment | 5 complexity | e25b529f6c6c05dfdc928c6c29bd2d33 MD5 | raw file
  1<?php
  2
  3namespace Sabre\DAV\Property;
  4
  5use Sabre\DAV;
  6
  7/**
  8 * supported-report-set property.
  9 *
 10 * This property is defined in RFC3253, but since it's
 11 * so common in other webdav-related specs, it is part of the core server.
 12 *
 13 * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
 14 * @author Evert Pot (http://evertpot.com/)
 15 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
 16 */
 17class SupportedReportSet extends DAV\Property {
 18
 19    /**
 20     * List of reports
 21     *
 22     * @var array
 23     */
 24    protected $reports = array();
 25
 26    /**
 27     * Creates the property
 28     *
 29     * Any reports passed in the constructor
 30     * should be valid report-types in clark-notation.
 31     *
 32     * Either a string or an array of strings must be passed.
 33     *
 34     * @param mixed $reports
 35     */
 36    public function __construct($reports = null) {
 37
 38        if (!is_null($reports))
 39            $this->addReport($reports);
 40
 41    }
 42
 43    /**
 44     * Adds a report to this property
 45     *
 46     * The report must be a string in clark-notation.
 47     * Multiple reports can be specified as an array.
 48     *
 49     * @param mixed $report
 50     * @return void
 51     */
 52    public function addReport($report) {
 53
 54        if (!is_array($report)) $report = array($report);
 55
 56        foreach($report as $r) {
 57
 58            if (!preg_match('/^{([^}]*)}(.*)$/',$r))
 59                throw new DAV\Exception('Reportname must be in clark-notation');
 60
 61            $this->reports[] = $r;
 62
 63        }
 64
 65    }
 66
 67    /**
 68     * Returns the list of supported reports
 69     *
 70     * @return array
 71     */
 72    public function getValue() {
 73
 74        return $this->reports;
 75
 76    }
 77
 78    /**
 79     * Returns true or false if the property contains a specific report.
 80     *
 81     * @param string $reportName
 82     * @return bool
 83     */
 84    public function has($reportName) {
 85
 86        return in_array(
 87            $reportName,
 88            $this->reports
 89        );
 90
 91    }
 92
 93    /**
 94     * Serializes the node
 95     *
 96     * @param DAV\Server $server
 97     * @param \DOMElement $prop
 98     * @return void
 99     */
100    public function serialize(DAV\Server $server, \DOMElement $prop) {
101
102        foreach($this->reports as $reportName) {
103
104            $supportedReport = $prop->ownerDocument->createElement('d:supported-report');
105            $prop->appendChild($supportedReport);
106
107            $report = $prop->ownerDocument->createElement('d:report');
108            $supportedReport->appendChild($report);
109
110            preg_match('/^{([^}]*)}(.*)$/',$reportName,$matches);
111
112            list(, $namespace, $element) = $matches;
113
114            $prefix = isset($server->xmlNamespaces[$namespace])?$server->xmlNamespaces[$namespace]:null;
115
116            if ($prefix) {
117                $report->appendChild($prop->ownerDocument->createElement($prefix . ':' . $element));
118            } else {
119                $report->appendChild($prop->ownerDocument->createElementNS($namespace, 'x:' . $element));
120            }
121
122        }
123
124    }
125
126}