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