1<?php
2
3namespace Sabre\VObject\Property;
4
5use Sabre\VObject\Property;
6use Sabre\Xml;
7
8/**
9 * Float property.
10 *
11 * This object represents FLOAT values. These can be 1 or more floating-point
12 * numbers.
13 *
14 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
15 * @author Evert Pot (http://evertpot.com/)
16 * @license http://sabre.io/license/ Modified BSD License
17 */
18class FloatValue extends Property {
19
20    /**
21     * In case this is a multi-value property. This string will be used as a
22     * delimiter.
23     *
24     * @var string|null
25     */
26    public $delimiter = ';';
27
28    /**
29     * Sets a raw value coming from a mimedir (iCalendar/vCard) file.
30     *
31     * This has been 'unfolded', so only 1 line will be passed. Unescaping is
32     * not yet done, but parameters are not included.
33     *
34     * @param string $val
35     *
36     * @return void
37     */
38    function setRawMimeDirValue($val) {
39
40        $val = explode($this->delimiter, $val);
41        foreach ($val as &$item) {
42            $item = (float)$item;
43        }
44        $this->setParts($val);
45
46    }
47
48    /**
49     * Returns a raw mime-dir representation of the value.
50     *
51     * @return string
52     */
53    function getRawMimeDirValue() {
54
55        return implode(
56            $this->delimiter,
57            $this->getParts()
58        );
59
60    }
61
62    /**
63     * Returns the type of value.
64     *
65     * This corresponds to the VALUE= parameter. Every property also has a
66     * 'default' valueType.
67     *
68     * @return string
69     */
70    function getValueType() {
71
72        return 'FLOAT';
73
74    }
75
76    /**
77     * Returns the value, in the format it should be encoded for JSON.
78     *
79     * This method must always return an array.
80     *
81     * @return array
82     */
83    function getJsonValue() {
84
85        $val = array_map('floatval', $this->getParts());
86
87        // Special-casing the GEO property.
88        //
89        // See:
90        // http://tools.ietf.org/html/draft-ietf-jcardcal-jcal-04#section-3.4.1.2
91        if ($this->name === 'GEO') {
92            return [$val];
93        }
94
95        return $val;
96
97    }
98
99    /**
100     * Hydrate data from a XML subtree, as it would appear in a xCard or xCal
101     * object.
102     *
103     * @param array $value
104     *
105     * @return void
106     */
107    function setXmlValue(array $value) {
108
109        $value = array_map('floatval', $value);
110        parent::setXmlValue($value);
111
112    }
113
114    /**
115     * This method serializes only the value of a property. This is used to
116     * create xCard or xCal documents.
117     *
118     * @param Xml\Writer $writer  XML writer.
119     *
120     * @return void
121     */
122    protected function xmlSerializeValue(Xml\Writer $writer) {
123
124        // Special-casing the GEO property.
125        //
126        // See:
127        // http://tools.ietf.org/html/rfc6321#section-3.4.1.2
128        if ($this->name === 'GEO') {
129
130            $value = array_map('floatval', $this->getParts());
131
132            $writer->writeElement('latitude', $value[0]);
133            $writer->writeElement('longitude', $value[1]);
134
135        }
136        else {
137            parent::xmlSerializeValue($writer);
138        }
139
140    }
141
142}
143