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    public function getValueType()
35    {
36        return 'TIME';
37    }
38
39    /**
40     * Sets the JSON value, as it would appear in a jCard or jCal object.
41     *
42     * The value must always be an array.
43     */
44    public function setJsonValue(array $value)
45    {
46        // Removing colons from value.
47        $value = str_replace(
48            ':',
49            '',
50            $value
51        );
52
53        if (1 === count($value)) {
54            $this->setValue(reset($value));
55        } else {
56            $this->setValue($value);
57        }
58    }
59
60    /**
61     * Returns the value, in the format it should be encoded for json.
62     *
63     * This method must always return an array.
64     *
65     * @return array
66     */
67    public function getJsonValue()
68    {
69        $parts = DateTimeParser::parseVCardTime($this->getValue());
70        $timeStr = '';
71
72        // Hour
73        if (!is_null($parts['hour'])) {
74            $timeStr .= $parts['hour'];
75
76            if (!is_null($parts['minute'])) {
77                $timeStr .= ':';
78            }
79        } else {
80            // We know either minute or second _must_ be set, so we insert a
81            // dash for an empty value.
82            $timeStr .= '-';
83        }
84
85        // Minute
86        if (!is_null($parts['minute'])) {
87            $timeStr .= $parts['minute'];
88
89            if (!is_null($parts['second'])) {
90                $timeStr .= ':';
91            }
92        } else {
93            if (isset($parts['second'])) {
94                // Dash for empty minute
95                $timeStr .= '-';
96            }
97        }
98
99        // Second
100        if (!is_null($parts['second'])) {
101            $timeStr .= $parts['second'];
102        }
103
104        // Timezone
105        if (!is_null($parts['timezone'])) {
106            if ('Z' === $parts['timezone']) {
107                $timeStr .= 'Z';
108            } else {
109                $timeStr .=
110                    preg_replace('/([0-9]{2})([0-9]{2})$/', '$1:$2', $parts['timezone']);
111            }
112        }
113
114        return [$timeStr];
115    }
116
117    /**
118     * Hydrate data from a XML subtree, as it would appear in a xCard or xCal
119     * object.
120     */
121    public function setXmlValue(array $value)
122    {
123        $value = array_map(
124            function ($value) {
125                return str_replace(':', '', $value);
126            },
127            $value
128        );
129        parent::setXmlValue($value);
130    }
131}
132