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    public function setRawMimeDirValue($val)
37    {
38        $val = explode($this->delimiter, $val);
39        foreach ($val as &$item) {
40            $item = (float) $item;
41        }
42        $this->setParts($val);
43    }
44
45    /**
46     * Returns a raw mime-dir representation of the value.
47     *
48     * @return string
49     */
50    public function getRawMimeDirValue()
51    {
52        return implode(
53            $this->delimiter,
54            $this->getParts()
55        );
56    }
57
58    /**
59     * Returns the type of value.
60     *
61     * This corresponds to the VALUE= parameter. Every property also has a
62     * 'default' valueType.
63     *
64     * @return string
65     */
66    public function getValueType()
67    {
68        return 'FLOAT';
69    }
70
71    /**
72     * Returns the value, in the format it should be encoded for JSON.
73     *
74     * This method must always return an array.
75     *
76     * @return array
77     */
78    public function getJsonValue()
79    {
80        $val = array_map('floatval', $this->getParts());
81
82        // Special-casing the GEO property.
83        //
84        // See:
85        // http://tools.ietf.org/html/draft-ietf-jcardcal-jcal-04#section-3.4.1.2
86        if ('GEO' === $this->name) {
87            return [$val];
88        }
89
90        return $val;
91    }
92
93    /**
94     * Hydrate data from a XML subtree, as it would appear in a xCard or xCal
95     * object.
96     *
97     * @param array $value
98     */
99    public function setXmlValue(array $value)
100    {
101        $value = array_map('floatval', $value);
102        parent::setXmlValue($value);
103    }
104
105    /**
106     * This method serializes only the value of a property. This is used to
107     * create xCard or xCal documents.
108     *
109     * @param Xml\Writer $writer XML writer
110     */
111    protected function xmlSerializeValue(Xml\Writer $writer)
112    {
113        // Special-casing the GEO property.
114        //
115        // See:
116        // http://tools.ietf.org/html/rfc6321#section-3.4.1.2
117        if ('GEO' === $this->name) {
118            $value = array_map('floatval', $this->getParts());
119
120            $writer->writeElement('latitude', $value[0]);
121            $writer->writeElement('longitude', $value[1]);
122        } else {
123            parent::xmlSerializeValue($writer);
124        }
125    }
126}
127