1<?php
2
3namespace Sabre\CalDAV\Xml\Request;
4
5use Sabre\CalDAV\Plugin;
6use Sabre\DAV\Exception\BadRequest;
7use Sabre\VObject\DateTimeParser;
8use Sabre\Xml\Reader;
9use Sabre\Xml\XmlDeserializable;
10
11/**
12 * FreeBusyQueryReport
13 *
14 * This class parses the {DAV:}free-busy-query REPORT, as defined in:
15 *
16 * http://tools.ietf.org/html/rfc3253#section-3.8
17 *
18 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
19 * @author Evert Pot (http://evertpot.com/)
20 * @license http://sabre.io/license/ Modified BSD License
21 */
22class FreeBusyQueryReport implements XmlDeserializable {
23
24    /**
25     * Starttime of report
26     *
27     * @var DateTime|null
28     */
29    public $start;
30
31    /**
32     * End time of report
33     *
34     * @var DateTime|null
35     */
36    public $end;
37
38    /**
39     * The deserialize method is called during xml parsing.
40     *
41     * This method is called statictly, this is because in theory this method
42     * may be used as a type of constructor, or factory method.
43     *
44     * Often you want to return an instance of the current class, but you are
45     * free to return other data as well.
46     *
47     * You are responsible for advancing the reader to the next element. Not
48     * doing anything will result in a never-ending loop.
49     *
50     * If you just want to skip parsing for this element altogether, you can
51     * just call $reader->next();
52     *
53     * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
54     * the next element.
55     *
56     * @param Reader $reader
57     * @return mixed
58     */
59    static function xmlDeserialize(Reader $reader) {
60
61        $timeRange = '{' . Plugin::NS_CALDAV . '}time-range';
62
63        $start = null;
64        $end = null;
65
66        foreach ((array)$reader->parseInnerTree([]) as $elem) {
67
68            if ($elem['name'] !== $timeRange) continue;
69
70            $start = empty($elem['attributes']['start']) ?: $elem['attributes']['start'];
71            $end = empty($elem['attributes']['end']) ?: $elem['attributes']['end'];
72
73        }
74        if (!$start && !$end) {
75            throw new BadRequest('The freebusy report must have a time-range element');
76        }
77        if ($start) {
78            $start = DateTimeParser::parseDateTime($start);
79        }
80        if ($end) {
81            $end = DateTimeParser::parseDateTime($end);
82        }
83        $result = new self();
84        $result->start = $start;
85        $result->end = $end;
86
87        return $result;
88
89    }
90
91}
92