Completed
Push — master ( c8ea86...04bb20 )
by ARCANEDEV
05:10 queued 05:02
created

Location   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 46.67%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 0
loc 241
ccs 21
cts 45
cp 0.4667
rs 10
c 0
b 0
f 0

18 Methods

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