1<?php
2
3namespace Sabre\VObject\Property;
4
5use Sabre\VObject\DateTimeParser;
6
7/**
8 * Time property.
9 *
10 * This object encodes TIME values.
11 *
12 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
13 * @author Evert Pot (http://evertpot.com/)
14 * @license http://sabre.io/license/ Modified BSD License
15 */
16class Time extends Text {
17
18    /**
19     * In case this is a multi-value property. This string will be used as a
20     * delimiter.
21     *
22     * @var string|null
23     */
24    public $delimiter = null;
25
26    /**
27     * Returns the type of value.
28     *
29     * This corresponds to the VALUE= parameter. Every property also has a
30     * 'default' valueType.
31     *
32     * @return string
33     */
34    function getValueType() {
35
36        return 'TIME';
37
38    }
39
40    /**
41     * Sets the JSON value, as it would appear in a jCard or jCal object.
42     *
43     * The value must always be an array.
44     *
45     * @param array $value
46     *
47     * @return void
48     */
49    function setJsonValue(array $value) {
50
51        // Removing colons from value.
52        $value = str_replace(
53            ':',
54            '',
55            $value
56        );
57
58        if (count($value) === 1) {
59            $this->setValue(reset($value));
60        } else {
61            $this->setValue($value);
62        }
63
64    }
65
66    /**
67     * Returns the value, in the format it should be encoded for json.
68     *
69     * This method must always return an array.
70     *
71     * @return array
72     */
73    function getJsonValue() {
74
75        $parts = DateTimeParser::parseVCardTime($this->getValue());
76        $timeStr = '';
77
78        // Hour
79        if (!is_null($parts['hour'])) {
80            $timeStr .= $parts['hour'];
81
82            if (!is_null($parts['minute'])) {
83                $timeStr .= ':';
84            }
85        } else {
86            // We know either minute or second _must_ be set, so we insert a
87            // dash for an empty value.
88            $timeStr .= '-';
89        }
90
91        // Minute
92        if (!is_null($parts['minute'])) {
93            $timeStr .= $parts['minute'];
94
95            if (!is_null($parts['second'])) {
96                $timeStr .= ':';
97            }
98        } else {
99            if (isset($parts['second'])) {
100                // Dash for empty minute
101                $timeStr .= '-';
102            }
103        }
104
105        // Second
106        if (!is_null($parts['second'])) {
107            $timeStr .= $parts['second'];
108        }
109
110        // Timezone
111        if (!is_null($parts['timezone'])) {
112            if ($parts['timezone'] === 'Z') {
113                $timeStr .= 'Z';
114            } else {
115                $timeStr .=
116                    preg_replace('/([0-9]{2})([0-9]{2})$/', '$1:$2', $parts['timezone']);
117            }
118        }
119
120        return [$timeStr];
121
122    }
123
124    /**
125     * Hydrate data from a XML subtree, as it would appear in a xCard or xCal
126     * object.
127     *
128     * @param array $value
129     *
130     * @return void
131     */
132    function setXmlValue(array $value) {
133
134        $value = array_map(
135            function($value) {
136                return str_replace(':', '', $value);
137            },
138            $value
139        );
140        parent::setXmlValue($value);
141
142    }
143
144}
145