IsGeolocation::isValid()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 4
nop 1
dl 0
loc 22
rs 8.9457
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright Andreas Heigl <[email protected]>
4
 * 
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 * 
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIBILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @author    Andreas Heigl<[email protected]>
24
 * @copyright Andreas Heigl
25
 * @license   http://www.opesource.org/licenses/mit-license.php MIT-License
26
 * @version   0.0
27
 * @since     04.07.13
28
 * @link      https://github.com/heiglandreas/OrgHeiglGeolocation
29
 */
30
31
namespace Org_Heigl\Geolocation\Validator;
32
33
use Zend\Validator\AbstractValidator;
34
/**
35
 * Validates a string whether it's a valid Geolocation or not
36
 *
37
 * Validation checks whether the format is ok and whether the resulting latitude
38
 * and longitude are within the range of -90 to 90 degrees.
39
 *
40
 * @author    Andreas Heigl<[email protected]>
41
 * @copyright Andreas Heigl
42
 * @license   http://www.opesource.org/licenses/mit-license.php MIT-License
43
 * @version   0.0
44
 * @since     03.07.13
45
 * @link      https://github.com/heiglandreas/OrgHeiglGeolocation
46
 */
47
class IsGeolocation extends AbstractValidator
48
{
49
    const INVALID_FORMAT         = 'invalidFormat';
50
    const LATITUDE_OUT_OF_RANGE  = 'latitudeOutOfRange';
51
    const LONGITUDE_OUT_OF_RANGE = 'longitudeOutOfRange';
52
53
    /**
54
     * The message templates
55
     *
56
     * @var $messageTemplates
57
     */
58
    protected $messageTemplates = array(
59
        self::INVALID_FORMAT => 'The given value is of an unrecognised format',
60
        self::LATITUDE_OUT_OF_RANGE => 'The given latitude exceeds the range of -90 through 90 degrees',
61
        self::LONGITUDE_OUT_OF_RANGE => 'The given longitude exceeds the range of  -180 through 180 degrees',
62
    );
63
64
    /**
65
     * Check the given value
66
     *
67
     * The value has to be a string of the form '[LAT],[LON]' where [LAT] and
68
     * [LON] are floating point values with a dot (.) as decimal separator.
69
     * Both values are separated by a colon (,).
70
     * [LAT] has to be in the rage of -90 through 90, whereas [LON] has to be in the
71
     * range of -180 through 180
72
     *
73
     * @param string $value The value-string
74
     */
75
    public function isValid($value) : bool
76
    {
77
        if (! preg_match('/(?P<lat>[-]?\d{1,2}(\.\d+)?)\D+(?P<lon>[-]?\d{1,3}(\.\d+)?)/', $value, $result)) {
78
            $this->error(self::INVALID_FORMAT);
79
            return false;
80
        }
81
82
        $latitude  = (float) $result['lat'];
83
        $longitude = (float) $result['lon'];
84
85
        if (-90 > $latitude || 90 < $latitude) {
86
            $this->error(self::LATITUDE_OUT_OF_RANGE);
87
            return false;
88
        }
89
90
        if (-180 > $longitude || 180 < $longitude) {
91
            $this->error(self::LONGITUDE_OUT_OF_RANGE);
92
            return false;
93
        }
94
95
        return true;
96
    }
97
98
}