Completed
Pull Request — master (#2)
by ARCANEDEV
03:37
created

GeocodingResponse   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 176
c 0
b 0
f 0
wmc 13
lcom 1
cbo 3
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRaw() 0 4 1
A get() 0 4 1
A getFormattedAddress() 0 4 1
A getAddressComponents() 0 4 1
A getLocationPosition() 0 6 1
A getLocationType() 0 4 1
A getViewport() 0 7 1
A toJson() 0 4 1
A jsonSerialize() 0 4 1
A toArray() 0 6 1
A isOk() 0 4 1
A createPosition() 0 7 1
1
<?php namespace Arcanedev\GeoLocation\Google\Geocoding;
2
3
use Arcanedev\GeoLocation\Entities\Position;
4
use Arcanedev\GeoLocation\Entities\Viewport;
5
use Illuminate\Contracts\Support\Arrayable;
6
use Illuminate\Contracts\Support\Jsonable;
7
use Illuminate\Support\Arr;
8
use JsonSerializable;
9
10
/**
11
 * Class     GeocodingResponse
12
 *
13
 * @package  Arcanedev\GeoLocation\Google\Geocoding
14
 * @author   ARCANEDEV <[email protected]>
15
 */
16
class GeocodingResponse implements Arrayable, Jsonable, JsonSerializable
17
{
18
    /* -----------------------------------------------------------------
19
     |  Properties
20
     | -----------------------------------------------------------------
21
     */
22
23
    /**
24
     * The response's data.
25
     *
26
     * @var  array
27
     */
28
    protected $data = [];
29
30
    /* -----------------------------------------------------------------
31
     |  Constructor
32
     | -----------------------------------------------------------------
33
     */
34
35
    /**
36
     * DistanceMatrixResponse constructor.
37
     *
38
     * @param  array  $data
39
     */
40
    public function __construct(array $data = [])
41
    {
42
        $this->data = $data;
43
    }
44
45
    /* -----------------------------------------------------------------
46
     |  Getters & Setters
47
     | -----------------------------------------------------------------
48
     */
49
50
    /**
51
     * Get the raw response.
52
     *
53
     * @return array
54
     */
55
    public function getRaw()
56
    {
57
        return $this->data;
58
    }
59
60
    /**
61
     * Get a data with a given key.
62
     *
63
     * @param  string      $key
64
     * @param  mixed|null  $default
65
     *
66
     * @return mixed
67
     */
68
    public function get($key, $default = null)
69
    {
70
        return Arr::get($this->getRaw(), $key, $default);
71
    }
72
73
    /**
74
     * Get the formatted address.
75
     *
76
     * @return string|null
77
     */
78
    public function getFormattedAddress()
79
    {
80
        return $this->get('results.0.formatted_address');
81
    }
82
83
    /**
84
     * Get the separate address components.
85
     *
86
     * @return array
87
     */
88
    public function getAddressComponents()
89
    {
90
        return $this->get('results.0.address_components', []);
91
    }
92
93
    /**
94
     * Get the location's position.
95
     *
96
     * @return \Arcanedev\GeoLocation\Contracts\Entities\Position
97
     */
98
    public function getLocationPosition()
99
    {
100
        return $this->createPosition(
101
            $this->get('results.0.geometry.location', [])
102
        );
103
    }
104
105
    /**
106
     * Get the location's type.
107
     *
108
     * @return string
109
     */
110
    public function getLocationType()
111
    {
112
        return $this->get('results.0.geometry.location_type');
113
    }
114
115
    /**
116
     * Get the viewport coordinates.
117
     *
118
     * @return \Arcanedev\GeoLocation\Entities\Viewport
119
     */
120
    public function getViewport()
121
    {
122
        return Viewport::create(
123
            $this->createPosition($this->get('results.0.geometry.viewport.northeast', [])),
124
            $this->createPosition($this->get('results.0.geometry.viewport.southwest', []))
125
        );
126
    }
127
128
    /* -----------------------------------------------------------------
129
     |  Other Methods
130
     | -----------------------------------------------------------------
131
     */
132
133
    /**
134
     * Convert the object to its JSON representation.
135
     *
136
     * @param  int  $options
137
     *
138
     * @return string
139
     */
140
    public function toJson($options = 0)
141
    {
142
        return json_encode($this->jsonSerialize(), $options);
143
    }
144
145
    /**
146
     * Convert the object into something JSON serializable.
147
     *
148
     * @return array
149
     */
150
    public function jsonSerialize()
151
    {
152
        return $this->toArray();
153
    }
154
155
    /**
156
     * Convert the object to array.
157
     *
158
     * @return array
159
     */
160
    public function toArray()
161
    {
162
        return [
163
            //
164
        ];
165
    }
166
167
    /**
168
     * Check if the response's status is OK.
169
     *
170
     * @return bool
171
     */
172
    public function isOk()
173
    {
174
        return $this->get('status') === 'OK';
175
    }
176
177
    /**
178
     * Create the position instance.
179
     *
180
     * @param  array  $coordinates
181
     *
182
     * @return \Arcanedev\GeoLocation\Entities\Position
183
     */
184
    protected function createPosition(array $coordinates)
185
    {
186
        return Position::create(
187
            Arr::get($coordinates, 'lat', 0.0),
188
            Arr::get($coordinates, 'lng', 0.0)
189
        );
190
    }
191
}
192