Completed
Pull Request — master (#4)
by ARCANEDEV
01:52
created

GeocodingResponse::isOk()   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\Google\Geocoding;
2
3
use Arcanedev\GeoLocation\Entities\Position;
4
use Arcanedev\GeoLocation\Entities\Viewport;
5
use Arcanedev\GeoLocation\Google\AbstractResponse;
6
use Illuminate\Support\Arr;
7
8
/**
9
 * Class     GeocodingResponse
10
 *
11
 * @package  Arcanedev\GeoLocation\Google\Geocoding
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class GeocodingResponse extends AbstractResponse
15
{
16
    /* -----------------------------------------------------------------
17
     |  Getters & Setters
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * Get the formatted address.
23
     *
24
     * @return string|null
25
     */
26
    public function getFormattedAddress()
27
    {
28
        return $this->get('results.0.formatted_address');
29
    }
30
31
    /**
32
     * Get the separate address components.
33
     *
34
     * @return array
35
     */
36
    public function getAddressComponents()
37
    {
38
        return $this->get('results.0.address_components', []);
39
    }
40 36
41
    /**
42 36
     * Get the location's position.
43 36
     *
44
     * @return \Arcanedev\GeoLocation\Contracts\Entities\Position
45
     */
46
    public function getLocationPosition()
47
    {
48
        return $this->createPosition(
49
            $this->get('results.0.geometry.location', [])
50
        );
51
    }
52
53
    /**
54
     * Get the location's type.
55 33
     *
56
     * @return string
57 33
     */
58
    public function getLocationType()
59
    {
60
        return $this->get('results.0.geometry.location_type');
61
    }
62
63
    /**
64
     * Get the viewport coordinates.
65
     *
66
     * @return \Arcanedev\GeoLocation\Entities\Viewport
67
     */
68 30
    public function getViewport()
69
    {
70 30
        return Viewport::create(
71
            $this->createPosition($this->get('results.0.geometry.viewport.northeast', [])),
72
            $this->createPosition($this->get('results.0.geometry.viewport.southwest', []))
73
        );
74
    }
75
76
    /**
77
     * Get the place id.
78 9
     *
79
     * @return string|null
80 9
     */
81
    public function getPlaceId()
82
    {
83
        return $this->get('results.0.place_id');
84
    }
85
86
    /* -----------------------------------------------------------------
87
     |  Other Methods
88 9
     | -----------------------------------------------------------------
89
     */
90 9
91
    /**
92
     * Convert the object to array.
93
     *
94
     * @return array
95
     */
96
    public function toArray()
97
    {
98 9
        return [
99
            'formatted_address'  => $this->getFormattedAddress(),
100 9
            'address_components' => $this->getAddressComponents(),
101 9
            'location_position'  => $this->getLocationPosition()->toArray(),
102 3
            'location_type'      => $this->getLocationType(),
103
            'viewport'           => $this->getViewport()->toArray(),
104
            'place_id'           => $this->getPlaceId(),
105
        ];
106
    }
107
108
    /**
109
     * Create the position instance.
110 9
     *
111
     * @param  array  $coordinates
112 9
     *
113
     * @return \Arcanedev\GeoLocation\Entities\Position
114
     */
115
    protected function createPosition(array $coordinates)
116
    {
117
        return Position::create(
118
            Arr::get($coordinates, 'lat', 0.0),
119
            Arr::get($coordinates, 'lng', 0.0)
120 9
        );
121
    }
122
}
123