Completed
Push — master ( da88de...163d4a )
by Joschi
03:49
created

GeoUri   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 100
ccs 22
cts 22
cp 1
rs 10
wmc 8
lcom 0
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 3
A getLatitude() 0 4 1
A getLongitude() 0 4 1
A getAltitude() 0 4 1
A __toString() 0 8 2
1
<?php
2
3
/**
4
 * apparat/object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Domain
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Domain\Model\Uri;
38
39
/**
40
 * Geo URI
41
 *
42
 * @package Apparat\Object
43
 * @subpackage Apparat\Object\Domain
44
 */
45
class GeoUri extends Uri
46
{
47
    /**
48
     * Geo schema
49
     *
50
     * @var string
51
     */
52
    const SCHEME_GEO = 'geo';
53
    /**
54
     * Regular expression for matching the Geo coordinates
55
     *
56
     * @var string
57
     */
58
    const GEO_REGEX = '(?P<latitude>-?\d+(?:\.\d+)?),(?P<longitude>-?\d+(?:\.\d+)?)(?:,(?P<altitude>-?\d+(?:\.\d+)?))?';
59
    /**
60
     * Latitude
61
     *
62
     * @var float
63
     */
64
    protected $latitude;
65
    /**
66
     * Longitude
67
     *
68
     * @var float
69
     */
70
    protected $longitude;
71
    /**
72
     * Altitude
73
     *
74
     * @var float
75
     */
76
    protected $altitude;
77
78
    /**
79
     * Constructor
80
     *
81
     * @param string $uri Geo URI
82
     * @throws InvalidArgumentException If the Geo URI doesn't suffice RFC 5870
83
     */
84 4
    public function __construct($uri)
85
    {
86 4
        parent::__construct($uri);
87
88
        // If the Geo URI doesn't suffice RFC 5870
89 4
        if (!preg_match('%^'.self::SCHEME_GEO.':'.self::GEO_REGEX.'$%', $this->uri, $geoParts)) {
90 2
            throw new InvalidArgumentException(
91 2
                sprintf('Invalid RFC 5870 GEO URL "%s"', $this->uri),
92
                InvalidArgumentException::INVALID_GEO_URL
93 2
            );
94
        }
95
96 3
        $this->latitude = floatval($geoParts['latitude']);
97 3
        $this->longitude = floatval($geoParts['longitude']);
98 3
        $this->altitude = empty($geoParts['altitude']) ? null : floatval($geoParts['altitude']);
99 3
    }
100
101
    /**
102
     * Return the latitude
103
     *
104
     * @return float latitude
105
     */
106 1
    public function getLatitude()
107
    {
108 1
        return $this->latitude;
109
    }
110
111
    /**
112
     * Return the longitude
113
     *
114
     * @return float Longitude
115
     */
116 1
    public function getLongitude()
117
    {
118 1
        return $this->longitude;
119
    }
120
121
    /**
122
     * Return the altitude
123
     *
124
     * @return float Altitude
125
     */
126 1
    public function getAltitude()
127
    {
128 1
        return $this->altitude;
129
    }
130
131
    /**
132
     * Serialize the Geo URI
133
     *
134
     * @return string
135
     */
136 1
    public function __toString()
137
    {
138 1
        $geoUri = self::SCHEME_GEO.':'.$this->latitude.','.$this->longitude;
139 1
        if ($this->altitude !== null) {
140 1
            $geoUri .= ','.$this->altitude;
141 1
        }
142 1
        return $geoUri;
143
    }
144
}
145