1<?php
2
3namespace Sabre\DAV\Xml\Property;
4
5use DateTime;
6use DateTimeZone;
7use Sabre\HTTP;
8use Sabre\Xml\Element;
9use Sabre\Xml\Reader;
10use Sabre\Xml\Writer;
11
12/**
13 * This property represents the {DAV:}getlastmodified property.
14 *
15 * Defined in:
16 * http://tools.ietf.org/html/rfc4918#section-15.7
17 *
18 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
19 * @author Evert Pot (http://www.rooftopsolutions.nl/)
20 * @license http://sabre.io/license/ Modified BSD License
21 */
22class GetLastModified implements Element {
23
24    /**
25     * time
26     *
27     * @var DateTime
28     */
29    public $time;
30
31    /**
32     * Constructor
33     *
34     * @param int|DateTime $time
35     */
36    function __construct($time) {
37
38        if ($time instanceof DateTime) {
39            $this->time = clone $time;
40        } else {
41            $this->time = new DateTime('@' . $time);
42        }
43
44        // Setting timezone to UTC
45        $this->time->setTimezone(new DateTimeZone('UTC'));
46
47    }
48
49    /**
50     * getTime
51     *
52     * @return DateTime
53     */
54    function getTime() {
55
56        return $this->time;
57
58    }
59
60    /**
61     * The serialize method is called during xml writing.
62     *
63     * It should use the $writer argument to encode this object into XML.
64     *
65     * Important note: it is not needed to create the parent element. The
66     * parent element is already created, and we only have to worry about
67     * attributes, child elements and text (if any).
68     *
69     * Important note 2: If you are writing any new elements, you are also
70     * responsible for closing them.
71     *
72     * @param Writer $writer
73     * @return void
74     */
75    function xmlSerialize(Writer $writer) {
76
77        $writer->write(
78            HTTP\Util::toHTTPDate($this->time)
79        );
80
81    }
82
83    /**
84     * The deserialize method is called during xml parsing.
85     *
86     * This method is called statically, this is because in theory this method
87     * may be used as a type of constructor, or factory method.
88     *
89     * Often you want to return an instance of the current class, but you are
90     * free to return other data as well.
91     *
92     * Important note 2: You are responsible for advancing the reader to the
93     * next element. Not doing anything will result in a never-ending loop.
94     *
95     * If you just want to skip parsing for this element altogether, you can
96     * just call $reader->next();
97     *
98     * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
99     * the next element.
100     *
101     * @param Reader $reader
102     * @return mixed
103     */
104    static function xmlDeserialize(Reader $reader) {
105
106        return
107            new self(new DateTime($reader->parseInnerTree()));
108
109    }
110}
111