1
|
|
|
<?php namespace Arcanedev\GeoIP; |
2
|
|
|
|
3
|
|
|
use ArrayAccess; |
4
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
5
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Location |
11
|
|
|
* |
12
|
|
|
* @package Arcanedev\GeoIP |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
* |
15
|
|
|
* @property string ip |
16
|
|
|
* @property string iso_code |
17
|
|
|
* @property string country |
18
|
|
|
* @property string city |
19
|
|
|
* @property string state |
20
|
|
|
* @property string state_code |
21
|
|
|
* @property string postal_code |
22
|
|
|
* @property float latitude |
23
|
|
|
* @property float longitude |
24
|
|
|
* @property string timezone |
25
|
|
|
* @property string continent |
26
|
|
|
* |
27
|
|
|
* @property bool display_name |
28
|
|
|
* @property bool default |
29
|
|
|
* @property bool|null cached |
30
|
|
|
* @property string currency |
31
|
|
|
*/ |
32
|
|
|
class Location implements ArrayAccess, Arrayable, Jsonable |
33
|
|
|
{ |
34
|
|
|
/* ------------------------------------------------------------------------------------------------ |
35
|
|
|
| Properties |
36
|
|
|
| ------------------------------------------------------------------------------------------------ |
37
|
|
|
*/ |
38
|
|
|
/** |
39
|
|
|
* The location's attributes |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $attributes = []; |
44
|
|
|
|
45
|
|
|
/* ------------------------------------------------------------------------------------------------ |
46
|
|
|
| Constructor |
47
|
|
|
| ------------------------------------------------------------------------------------------------ |
48
|
|
|
*/ |
49
|
|
|
/** |
50
|
|
|
* Create a new location instance. |
51
|
|
|
* |
52
|
|
|
* @param array $attributes |
53
|
|
|
*/ |
54
|
30 |
|
public function __construct(array $attributes = []) |
55
|
|
|
{ |
56
|
30 |
|
$this->attributes = $attributes; |
57
|
30 |
|
} |
58
|
|
|
|
59
|
|
|
/* ------------------------------------------------------------------------------------------------ |
60
|
|
|
| Getters & Setters |
61
|
|
|
| ------------------------------------------------------------------------------------------------ |
62
|
|
|
*/ |
63
|
|
|
/** |
64
|
|
|
* Get all attributes. |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
30 |
|
public function attributes() |
69
|
|
|
{ |
70
|
30 |
|
return $this->attributes; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get an attribute from the $attributes array. |
75
|
|
|
* |
76
|
|
|
* @param string $key |
77
|
|
|
* |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
30 |
|
public function getAttribute($key) |
81
|
|
|
{ |
82
|
30 |
|
$value = Arr::get($this->attributes(), $key); |
83
|
|
|
|
84
|
30 |
|
return method_exists($this, $method = 'get'.Str::studly($key).'Attribute') |
85
|
30 |
|
? $this->{$method}($value) |
86
|
30 |
|
: $value; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set a given attribute on the location. |
91
|
|
|
* |
92
|
|
|
* @param string $key |
93
|
|
|
* @param mixed $value |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
6 |
|
public function setAttribute($key, $value) |
98
|
|
|
{ |
99
|
6 |
|
$this->attributes[$key] = $value; |
100
|
|
|
|
101
|
6 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Return the display name of the location. |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
30 |
|
public function getDisplayNameAttribute() |
110
|
|
|
{ |
111
|
30 |
|
return preg_replace('/^,\s/', '', "{$this->city}, {$this->state_code}"); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Is the location the default. |
116
|
|
|
* |
117
|
|
|
* @param mixed $value |
118
|
|
|
* |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
30 |
|
public function getDefaultAttribute($value) |
122
|
|
|
{ |
123
|
30 |
|
return is_null($value) ? false : $value; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the instance as an array. |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
30 |
|
public function toArray() |
132
|
|
|
{ |
133
|
30 |
|
return $this->attributes(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Convert the object to its JSON representation. |
138
|
|
|
* |
139
|
|
|
* @param int $options |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function toJson($options = 0) |
144
|
|
|
{ |
145
|
|
|
return json_encode($this->toArray(), $options); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/* ------------------------------------------------------------------------------------------------ |
149
|
|
|
| Check Functions |
150
|
|
|
| ------------------------------------------------------------------------------------------------ |
151
|
|
|
*/ |
152
|
|
|
/** |
153
|
|
|
* Determine if the location is for the same IP address. |
154
|
|
|
* |
155
|
|
|
* @param string $ip |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function same($ip) |
160
|
|
|
{ |
161
|
|
|
return $this->getAttribute('ip') === $ip; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/* ------------------------------------------------------------------------------------------------ |
165
|
|
|
| Other Functions |
166
|
|
|
| ------------------------------------------------------------------------------------------------ |
167
|
|
|
*/ |
168
|
|
|
/** |
169
|
|
|
* Determine if the given attribute exists. |
170
|
|
|
* |
171
|
|
|
* @param mixed $offset |
172
|
|
|
* |
173
|
|
|
* @return bool |
174
|
|
|
*/ |
175
|
|
|
public function offsetExists($offset) |
176
|
|
|
{ |
177
|
|
|
return isset($this->$offset); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get the value for a given offset. |
182
|
|
|
* |
183
|
|
|
* @param string $offset |
184
|
|
|
* |
185
|
|
|
* @return mixed |
186
|
|
|
*/ |
187
|
|
|
public function offsetGet($offset) |
188
|
|
|
{ |
189
|
|
|
return $this->__get($offset); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Set the value for a given offset. |
194
|
|
|
* |
195
|
|
|
* @param string $offset |
196
|
|
|
* @param mixed $value |
197
|
|
|
*/ |
198
|
|
|
public function offsetSet($offset, $value) |
199
|
|
|
{ |
200
|
|
|
$this->__set($offset, $value); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get the location's attribute |
205
|
|
|
* |
206
|
|
|
* @param string $key |
207
|
|
|
* |
208
|
|
|
* @return mixed |
209
|
|
|
*/ |
210
|
30 |
|
public function __get($key) |
211
|
|
|
{ |
212
|
30 |
|
return $this->getAttribute($key); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Set the location's attribute |
217
|
|
|
* |
218
|
|
|
* @param string $key |
219
|
|
|
* @param mixed $value |
220
|
|
|
*/ |
221
|
|
|
public function __set($key, $value) |
222
|
|
|
{ |
223
|
|
|
$this->setAttribute($key, $value); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Check if the location's attribute is set |
228
|
|
|
* |
229
|
|
|
* @param string $key |
230
|
|
|
* |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
|
|
public function __isset($key) |
234
|
|
|
{ |
235
|
|
|
return array_key_exists($key, $this->attributes); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Unset the value for a given offset. |
240
|
|
|
* |
241
|
|
|
* @param string $offset |
242
|
|
|
*/ |
243
|
|
|
public function offsetUnset($offset) |
244
|
|
|
{ |
245
|
|
|
$this->__unset($offset); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Unset an attribute on the location. |
250
|
|
|
* |
251
|
|
|
* @param string $key |
252
|
|
|
*/ |
253
|
|
|
public function __unset($key) |
254
|
|
|
{ |
255
|
|
|
unset($this->attributes[$key]); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|