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