Completed
Pull Request — master (#4)
by ARCANEDEV
03:32
created

Position::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php namespace Arcanedev\GeoLocation\Entities;
2
3
use Arcanedev\GeoLocation\Contracts\Entities\Coordinate;
4
use Arcanedev\GeoLocation\Contracts\Entities\Position as PositionContract;
5
use Arcanedev\GeoLocation\Traits\CanBeJsonable;
6
7
/**
8
 * Class     Position
9
 *
10
 * @package  Arcanedev\GeoLocation\Entities
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class Position implements PositionContract
14
{
15
    /* -----------------------------------------------------------------
16
     |  Traits
17
     | -----------------------------------------------------------------
18
     */
19
20
    use CanBeJsonable;
21
22
    /* -----------------------------------------------------------------
23
     |  Properties
24
     | -----------------------------------------------------------------
25
     */
26
27
    /**
28
     * The latitude coordinate.
29
     *
30
     * @var  \Arcanedev\GeoLocation\Contracts\Entities\Coordinate
31
     */
32
    protected $latitude;
33
34
    /**
35
     * The longitude coordinate.
36
     *
37
     * @var  \Arcanedev\GeoLocation\Contracts\Entities\Coordinate
38
     */
39
    protected $longitude;
40
41
    /* -----------------------------------------------------------------
42
     |  Constructor
43
     | -----------------------------------------------------------------
44
     */
45
46
    /**
47
     * Position constructor.
48
     *
49
     * @param  \Arcanedev\GeoLocation\Contracts\Entities\Coordinate  $latitude
50
     * @param  \Arcanedev\GeoLocation\Contracts\Entities\Coordinate  $longitude
51
     */
52 87
    public function __construct(Coordinate $latitude, Coordinate $longitude)
53
    {
54 87
        $this->setLatitude($latitude);
55 87
        $this->setLongitude($longitude);
56 87
    }
57
58
    /* -----------------------------------------------------------------
59
     |  Getters & Setters
60
     | -----------------------------------------------------------------
61
     */
62
63
    /**
64
     * Get the latitude coordinate.
65
     *
66
     * @return \Arcanedev\GeoLocation\Contracts\Entities\Coordinate
67
     */
68 66
    public function getLatitude()
69
    {
70 66
        return $this->latitude;
71
    }
72
73
    /**
74
     * Get the latitude coordinate (alias).
75
     *
76
     * @return \Arcanedev\GeoLocation\Contracts\Entities\Coordinate
77
     */
78 42
    public function lat()
79
    {
80 42
        return $this->getLatitude();
81
    }
82
83
    /**
84
     * Set the latitude coordinate.
85
     *
86
     * @param  \Arcanedev\GeoLocation\Contracts\Entities\Coordinate  $latitude
87
     *
88
     * @return self
89
     */
90 87
    public function setLatitude(Coordinate $latitude)
91
    {
92 87
        $this->latitude = $latitude;
93
94 87
        return $this;
95
    }
96
97
    /**
98
     * Get the longitude coordinate.
99
     *
100
     * @return \Arcanedev\GeoLocation\Contracts\Entities\Coordinate
101
     */
102 66
    public function getLongitude()
103
    {
104 66
        return $this->longitude;
105
    }
106
107
    /**
108
     * Get the longitude coordinate (alias).
109
     *
110
     * @return \Arcanedev\GeoLocation\Contracts\Entities\Coordinate
111
     */
112 42
    public function lng()
113
    {
114 42
        return $this->getLongitude();
115
    }
116
117
    /**
118
     * Set the longitude coordinate.
119
     *
120
     * @param  \Arcanedev\GeoLocation\Contracts\Entities\Coordinate  $longitude
121
     *
122
     * @return self
123
     */
124 87
    public function setLongitude(Coordinate $longitude)
125
    {
126 87
        $this->longitude = $longitude;
127
128 87
        return $this;
129
    }
130
131
    /* -----------------------------------------------------------------
132
     |  Main Methods
133
     | -----------------------------------------------------------------
134
     */
135
136
    /**
137
     * Create the position.
138
     *
139
     * @param  float  $latitude
140
     * @param  float  $longitude
141
     *
142
     * @return self
143
     */
144 60
    public static function create($latitude, $longitude)
145
    {
146 60
        return new static(
147 60
            new Latitude($latitude),
148 60
            new Longitude($longitude)
149 20
        );
150
    }
151
152
    /**
153
     * Create position instance from array.
154
     *
155
     * @param  array  $location
156
     *
157
     * @return self
158
     */
159 21
    public static function createFromArray(array $location)
160
    {
161 21
        return static::create($location['lat'], $location['lng']);
162
    }
163
164
    /**
165
     * Get the instance as an array.
166
     *
167
     * @return array
168
     */
169 27
    public function toArray()
170
    {
171
        return [
172 27
            'lat' => $this->getLatitude()->value(),
173 27
            'lng' => $this->getLongitude()->value(),
174 9
        ];
175
    }
176
}
177