Completed
Push — 0.11.x ( bc0e2a...a7b95f )
by Markus
101:21 queued 78:44
created

Geo   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 47.37%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 75
ccs 9
cts 19
cp 0.4737
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 7 1
A getGeoLocationAsString() 0 4 1
B __construct() 0 19 5
A getLatitude() 0 4 1
A getLongitude() 0 4 1
1
<?php
2
3
namespace Eluceo\iCal\Property\Event;
4
5
use Eluceo\iCal\Property;
6
7
/**
8
 * GEO property
9
 *
10
 * @see https://tools.ietf.org/html/rfc5545#section-3.8.1.6
11
 */
12
class Geo extends Property
13
{
14
    /**
15
     * @var float
16
     */
17
    private $latitude;
18
19
    /**
20
     * @var float
21
     */
22
    private $longitude;
23
24 1
    public function __construct($latitude, $longitude)
25
    {
26 1
        $this->latitude = $latitude;
27 1
        $this->longitude = $longitude;
28
29 1
        if ($this->latitude < -90 || $this->latitude > 90) {
30
            throw new \InvalidArgumentException(
31
                "The geographical latitude must be a value between -90 and 90 degrees. '{$this->latitude}' was given."
32
            );
33
        }
34
35 1
        if ($this->longitude < -180 || $this->longitude > 180) {
36
            throw new \InvalidArgumentException(
37
                "The geographical longitude must be a value between -180 and 180 degrees. '{$this->longitude}' was given."
38
            );
39
        }
40
41 1
        parent::__construct('GEO', new Property\RawStringValue($this->getGeoLocationAsString()));
42 1
    }
43
44
    /**
45
     * @deprecated This method is used to allow backwards compatibility for Event::setLocation
46
     *
47
     * @param string $geoLocationString
48
     * @return Geo
49
     */
50
    public static function fromString($geoLocationString)
51
    {
52
        $geoLocationString = str_replace(',', ';', $geoLocationString);
53
        $parts = explode(';', $geoLocationString);
54
55
        return new static((float) $parts[0], (float) $parts[1]);
56
    }
57
58
    /**
59
     * Returns the coordinates as a string.
60
     *
61
     * @example 37.386013;-122.082932
62
     *
63
     * @param string $separator
64
     * @return string
65
     */
66 1
    public function getGeoLocationAsString($separator = ';')
67
    {
68 1
        return number_format($this->latitude, 6) . $separator . number_format($this->longitude, 6);
69
    }
70
71
    /**
72
     * @return float
73
     */
74
    public function getLatitude()
75
    {
76
        return $this->latitude;
77
    }
78
79
    /**
80
     * @return float
81
     */
82
    public function getLongitude()
83
    {
84
        return $this->longitude;
85
    }
86
}
87