1<?php
2
3namespace Sabre\CalDAV\Xml\Filter;
4
5use Sabre\Xml\Reader;
6use Sabre\Xml\XmlDeserializable;
7use Sabre\DAV\Exception\BadRequest;
8use Sabre\CalDAV\Plugin;
9use Sabre\VObject\DateTimeParser;
10
11/**
12 * CompFilter parser.
13 *
14 * This class parses the {urn:ietf:params:xml:ns:caldav}comp-filter XML
15 * element, as defined in:
16 *
17 * https://tools.ietf.org/html/rfc4791#section-9.6
18 *
19 * The result will be spit out as an array.
20 *
21 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
22 * @author Evert Pot (http://www.rooftopsolutions.nl/)
23 * @license http://sabre.io/license/ Modified BSD License
24 */
25class CompFilter implements XmlDeserializable {
26
27    /**
28     * The deserialize method is called during xml parsing.
29     *
30     * This method is called statictly, this is because in theory this method
31     * may be used as a type of constructor, or factory method.
32     *
33     * Often you want to return an instance of the current class, but you are
34     * free to return other data as well.
35     *
36     * You are responsible for advancing the reader to the next element. Not
37     * doing anything will result in a never-ending loop.
38     *
39     * If you just want to skip parsing for this element altogether, you can
40     * just call $reader->next();
41     *
42     * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
43     * the next element.
44     *
45     * @param Reader $reader
46     * @return mixed
47     */
48    static function xmlDeserialize(Reader $reader) {
49
50        $result = [
51            'name'           => null,
52            'is-not-defined' => false,
53            'comp-filters'   => [],
54            'prop-filters'   => [],
55            'time-range'     => false,
56        ];
57
58        $att = $reader->parseAttributes();
59        $result['name'] = $att['name'];
60
61        $elems = $reader->parseInnerTree();
62
63        if (is_array($elems)) foreach ($elems as $elem) {
64
65            switch ($elem['name']) {
66
67                case '{' . Plugin::NS_CALDAV . '}comp-filter' :
68                    $result['comp-filters'][] = $elem['value'];
69                    break;
70                case '{' . Plugin::NS_CALDAV . '}prop-filter' :
71                    $result['prop-filters'][] = $elem['value'];
72                    break;
73                case '{' . Plugin::NS_CALDAV . '}is-not-defined' :
74                    $result['is-not-defined'] = true;
75                    break;
76                case '{' . Plugin::NS_CALDAV . '}time-range' :
77                    if ($result['name'] === 'VCALENDAR') {
78                        throw new BadRequest('You cannot add time-range filters on the VCALENDAR component');
79                    }
80                    $result['time-range'] = [
81                        'start' => isset($elem['attributes']['start']) ? DateTimeParser::parseDateTime($elem['attributes']['start']) : null,
82                        'end'   => isset($elem['attributes']['end']) ? DateTimeParser::parseDateTime($elem['attributes']['end']) : null,
83                    ];
84                    if ($result['time-range']['start'] && $result['time-range']['end'] && $result['time-range']['end'] <= $result['time-range']['start']) {
85                        throw new BadRequest('The end-date must be larger than the start-date');
86                    }
87                    break;
88
89            }
90
91        }
92
93        return $result;
94
95    }
96
97}
98