Completed
Push — master ( e63e58...83f41a )
by Markus
08:43 queued 06:43
created

Geo::fromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the eluceo/iCal package.
5
 *
6
 * (c) Markus Poerschke <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Eluceo\iCal\Property\Event;
13
14
use Eluceo\iCal\Property;
15
16
/**
17
 * GEO property.
18
 *
19
 * @see https://tools.ietf.org/html/rfc5545#section-3.8.1.6
20
 */
21
class Geo extends Property
22
{
23
    /**
24
     * @var float
25
     */
26
    private $latitude;
27
28
    /**
29
     * @var float
30
     */
31
    private $longitude;
32
33
    public function __construct(float $latitude, float $longitude)
34
    {
35
        $this->latitude = $latitude;
36
        $this->longitude = $longitude;
37
38
        if ($this->latitude < -90 || $this->latitude > 90) {
39
            throw new \InvalidArgumentException(
40
                "The geographical latitude must be a value between -90 and 90 degrees. '{$this->latitude}' was given."
41
            );
42
        }
43
44
        if ($this->longitude < -180 || $this->longitude > 180) {
45
            throw new \InvalidArgumentException(
46
                "The geographical longitude must be a value between -180 and 180 degrees. '{$this->longitude}' was given."
47
            );
48
        }
49
50
        parent::__construct('GEO', new Property\RawStringValue($this->getGeoLocationAsString()));
51
    }
52
53
    /**
54
     * @deprecated This method is used to allow backwards compatibility for Event::setLocation
55
     *
56
     * @param string $geoLocationString
57
     *
58
     * @return Geo
59
     */
60
    public static function fromString(string $geoLocationString): self
61
    {
62
        $geoLocationString = str_replace(',', ';', $geoLocationString);
63
        $parts = explode(';', $geoLocationString);
64
65
        return new static((float) $parts[0], (float) $parts[1]);
66
    }
67
68
    /**
69
     * Returns the coordinates as a string.
70
     *
71
     * @example 37.386013;-122.082932
72
     *
73
     * @param string $separator
74
     *
75
     * @return string
76
     */
77
    public function getGeoLocationAsString(string $separator = ';'): string
78
    {
79
        return number_format($this->latitude, 6) . $separator . number_format($this->longitude, 6);
80
    }
81
82
    /**
83
     * @return float
84
     */
85
    public function getLatitude(): float
86
    {
87
        return $this->latitude;
88
    }
89
90
    /**
91
     * @return float
92
     */
93
    public function getLongitude(): float
94
    {
95
        return $this->longitude;
96
    }
97
}
98