|
1
|
|
|
<?php namespace Arcanedev\GeoLocation\Google; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\GeoLocation\Traits\CanBeJsonable; |
|
4
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
5
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use JsonSerializable; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class AbstractResponse |
|
11
|
|
|
* |
|
12
|
|
|
* @package Arcanedev\GeoLocation\Google |
|
13
|
|
|
* @author ARCANEDEV <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class AbstractResponse implements Arrayable, Jsonable, JsonSerializable |
|
16
|
|
|
{ |
|
17
|
|
|
/* ----------------------------------------------------------------- |
|
18
|
|
|
| Constants |
|
19
|
|
|
| ----------------------------------------------------------------- |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
const STATUS_OK = 'OK'; |
|
23
|
|
|
const STATUS_ZERO_RESULTS = 'ZERO_RESULTS'; |
|
24
|
|
|
|
|
25
|
|
|
/* ----------------------------------------------------------------- |
|
26
|
|
|
| Traits |
|
27
|
|
|
| ----------------------------------------------------------------- |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
use CanBeJsonable; |
|
31
|
|
|
|
|
32
|
|
|
/* ----------------------------------------------------------------- |
|
33
|
|
|
| Properties |
|
34
|
|
|
| ----------------------------------------------------------------- |
|
35
|
|
|
*/ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The response's data. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $data = []; |
|
43
|
|
|
|
|
44
|
|
|
/* ----------------------------------------------------------------- |
|
45
|
|
|
| Constructor |
|
46
|
|
|
| ----------------------------------------------------------------- |
|
47
|
|
|
*/ |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* AbstractResponse constructor. |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $data |
|
53
|
|
|
*/ |
|
54
|
111 |
|
public function __construct(array $data = []) |
|
55
|
|
|
{ |
|
56
|
111 |
|
$this->data = $data; |
|
57
|
111 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/* ----------------------------------------------------------------- |
|
60
|
|
|
| Getters & Setters |
|
61
|
|
|
| ----------------------------------------------------------------- |
|
62
|
|
|
*/ |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get the raw response. |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
105 |
|
public function getRaw() |
|
70
|
|
|
{ |
|
71
|
105 |
|
return $this->data; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get the status. |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
42 |
|
public function getStatus() |
|
80
|
|
|
{ |
|
81
|
42 |
|
return $this->get('status'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/* ----------------------------------------------------------------- |
|
85
|
|
|
| Main Methods |
|
86
|
|
|
| ----------------------------------------------------------------- |
|
87
|
|
|
*/ |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get a data with a given key. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $key |
|
93
|
|
|
* @param mixed|null $default |
|
94
|
|
|
* |
|
95
|
|
|
* @return mixed |
|
96
|
|
|
*/ |
|
97
|
99 |
|
public function get($key, $default = null) |
|
98
|
|
|
{ |
|
99
|
99 |
|
return Arr::get($this->getRaw(), $key, $default); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Check if the response's status is OK. |
|
104
|
|
|
* |
|
105
|
|
|
* @return bool |
|
106
|
|
|
*/ |
|
107
|
42 |
|
public function isOk() |
|
108
|
|
|
{ |
|
109
|
42 |
|
return in_array($this->getStatus(), [ |
|
110
|
42 |
|
static::STATUS_OK, |
|
111
|
42 |
|
static::STATUS_ZERO_RESULTS, |
|
112
|
|
|
]); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|