Completed
Push — master ( d388c3...ceb9a8 )
by ARCANEDEV
05:06
created

AbstractCoordinate::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\GeoLocation\Entities\Coordinates;
2
3
use Arcanedev\GeoLocation\Contracts\Entities\Coordinates\Coordinate;
4
use Arcanedev\GeoLocation\Exceptions\InvalidCoordinateException;
5
6
/**
7
 * Class     AbstractCoordinate
8
 *
9
 * @package  Arcanedev\GeoLocation\Entities\Coordinates
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
abstract class AbstractCoordinate implements Coordinate
13
{
14
    /* -----------------------------------------------------------------
15
     |  Getters & Setters
16
     | -----------------------------------------------------------------
17
     */
18
19
    /**
20
     * The coordinate value.
21
     *
22
     * @var float
23
     */
24
    private $value;
25
26
    /* -----------------------------------------------------------------
27
     |  Constructor
28
     | -----------------------------------------------------------------
29
     */
30
31
    /**
32
     * AbstractCoordinate constructor.
33
     *
34
     * @param  float  $value
35
     */
36 111
    public function __construct($value)
37
    {
38 111
        $this->setValue($value);
39 99
    }
40
41
    /* -----------------------------------------------------------------
42
     |  Getters & Setters
43
     | -----------------------------------------------------------------
44
     */
45
46
    /**
47
     * Get the coordinate's value (alias).
48
     */
49 75
    public function value()
50
    {
51 75
        return $this->getValue();
52
    }
53
54
    /**
55
     * Get the coordinate's value.
56
     *
57
     * @return float
58
     */
59 75
    public function getValue()
60
    {
61 75
        return $this->value;
62
    }
63
64
    /**
65
     * Set the coordinate value.
66
     *
67
     * @param  float  $value
68
     *
69
     * @return self
70
     */
71 111
    public function setValue($value)
72
    {
73 111
        $this->checkValue($value);
74 99
        $this->value = (float) $value;
75
76 99
        return $this;
77
    }
78
79
    /* -----------------------------------------------------------------
80
     |  Other Methods
81
     | -----------------------------------------------------------------
82
     */
83
84
    /**
85
     * Check the given value.
86
     *
87
     * @param  float  $value
88
     */
89 111
    private function checkValue($value)
90
    {
91 111
        if ( ! is_numeric($value)) {
92 6
            throw new InvalidCoordinateException('The coordinate value must be numeric.');
93
        }
94
95 105
        if ($value < static::getMin() || $value > static::getMax()) {
96 6
            throw new InvalidCoordinateException(
97 6
                'The coordinate value must be between `'.static::getMin().'` & `'.static::getMax().'`, `'.$value.'` was given.'
98 2
            );
99
        }
100 99
    }
101
}
102