Geo::fromString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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 8
    public function __construct(float $latitude, float $longitude)
34
    {
35 8
        $this->latitude = $latitude;
36 8
        $this->longitude = $longitude;
37
38 8
        if ($this->latitude < -90 || $this->latitude > 90) {
39 1
            throw new \InvalidArgumentException("The geographical latitude must be a value between -90 and 90 degrees. '{$this->latitude}' was given.");
40
        }
41
42 7
        if ($this->longitude < -180 || $this->longitude > 180) {
43 1
            throw new \InvalidArgumentException("The geographical longitude must be a value between -180 and 180 degrees. '{$this->longitude}' was given.");
44
        }
45
46 6
        parent::__construct('GEO', new Property\RawStringValue($this->getGeoLocationAsString()));
47 6
    }
48
49
    /**
50
     * @deprecated This method is used to allow backwards compatibility for Event::setLocation
51
     *
52
     * @return Geo
53
     */
54 2
    public static function fromString(string $geoLocationString): self
55
    {
56 2
        $geoLocationString = str_replace(',', ';', $geoLocationString);
57 2
        $geoLocationString = str_replace('GEO:', '', $geoLocationString);
58 2
        $parts = explode(';', $geoLocationString);
59
60 2
        return new static((float) $parts[0], (float) $parts[1]);
61
    }
62
63
    /**
64
     * Returns the coordinates as a string.
65
     *
66
     * @example 37.386013;-122.082932
67
     */
68 6
    public function getGeoLocationAsString(string $separator = ';'): string
69
    {
70 6
        return number_format($this->latitude, 6) . $separator . number_format($this->longitude, 6);
71
    }
72
73 2
    public function getLatitude(): float
74
    {
75 2
        return $this->latitude;
76
    }
77
78 2
    public function getLongitude(): float
79
    {
80 2
        return $this->longitude;
81
    }
82
}
83