Completed
Push — master ( 375238...44eb79 )
by ARCANEDEV
07:07
created

GeocodingResponse::toJson()   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 1
crap 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 36
    public function __construct(array $data = [])
41
    {
42 36
        $this->data = $data;
43 36
    }
44
45
    /* -----------------------------------------------------------------
46
     |  Getters & Setters
47
     | -----------------------------------------------------------------
48
     */
49
50
    /**
51
     * Get the raw response.
52
     *
53
     * @return array
54
     */
55 33
    public function getRaw()
56
    {
57 33
        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 30
    public function get($key, $default = null)
69
    {
70 30
        return Arr::get($this->getRaw(), $key, $default);
71
    }
72
73
    /**
74
     * Get the formatted address.
75
     *
76
     * @return string|null
77
     */
78 9
    public function getFormattedAddress()
79
    {
80 9
        return $this->get('results.0.formatted_address');
81
    }
82
83
    /**
84
     * Get the separate address components.
85
     *
86
     * @return array
87
     */
88 9
    public function getAddressComponents()
89
    {
90 9
        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 9
    public function getLocationPosition()
99
    {
100 9
        return $this->createPosition(
101 9
            $this->get('results.0.geometry.location', [])
102 3
        );
103
    }
104
105
    /**
106
     * Get the location's type.
107
     *
108
     * @return string
109
     */
110 9
    public function getLocationType()
111
    {
112 9
        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 9
    public function getViewport()
121
    {
122 9
        return Viewport::create(
123 9
            $this->createPosition($this->get('results.0.geometry.viewport.northeast', [])),
124 9
            $this->createPosition($this->get('results.0.geometry.viewport.southwest', []))
125 3
        );
126
    }
127
128
    /**
129
     * Get the place id.
130
     *
131
     * @return string|null
132
     */
133 9
    public function getPlaceId()
134
    {
135 9
        return $this->get('results.0.place_id');
136
    }
137
138
    /* -----------------------------------------------------------------
139
     |  Other Methods
140
     | -----------------------------------------------------------------
141
     */
142
143
    /**
144
     * Convert the object to its JSON representation.
145
     *
146
     * @param  int  $options
147
     *
148
     * @return string
149
     */
150 3
    public function toJson($options = 0)
151
    {
152 3
        return json_encode($this->jsonSerialize(), $options);
153
    }
154
155
    /**
156
     * Convert the object into something JSON serializable.
157
     *
158
     * @return array
159
     */
160 3
    public function jsonSerialize()
161
    {
162 3
        return $this->toArray();
163
    }
164
165
    /**
166
     * Convert the object to array.
167
     *
168
     * @return array
169
     */
170 6
    public function toArray()
171
    {
172
        return [
173 6
            'formatted_address'  => $this->getFormattedAddress(),
174 6
            'address_components' => $this->getAddressComponents(),
175 6
            'location_position'  => $this->getLocationPosition()->toArray(),
176 6
            'location_type'      => $this->getLocationType(),
177 6
            'viewport'           => $this->getViewport()->toArray(),
178 6
            'place_id'           => $this->getPlaceId(),
179 2
        ];
180
    }
181
182
    /**
183
     * Check if the response's status is OK.
184
     *
185
     * @return bool
186
     */
187 6
    public function isOk()
188
    {
189 6
        return $this->get('status') === 'OK';
190
    }
191
192
    /**
193
     * Create the position instance.
194
     *
195
     * @param  array  $coordinates
196
     *
197
     * @return \Arcanedev\GeoLocation\Entities\Position
198
     */
199 12
    protected function createPosition(array $coordinates)
200
    {
201 12
        return Position::create(
202 12
            Arr::get($coordinates, 'lat', 0.0),
203 12
            Arr::get($coordinates, 'lng', 0.0)
204 4
        );
205
    }
206
}
207