Response   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 21
c 2
b 0
f 0
dl 0
loc 90
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 3 1
A __construct() 0 10 1
A isEmpty() 0 7 1
1
<?php
2
3
namespace Psonrie\GeoLocation;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
7
class Response implements Arrayable
8
{
9
    /**
10
     * The IP address used to retrieve the geo-location.
11
     *
12
     * @var string
13
     */
14
    public $ip;
15
    /**
16
     * The country code.
17
     *
18
     * @var string|null
19
     */
20
    public $countryCode;
21
    /**
22
     * The country name.
23
     *
24
     * @var string|null
25
     */
26
    public $countryName;
27
    /**
28
     * The region name.
29
     *
30
     * @var string|null
31
     */
32
    public $regionName;
33
    /**
34
     * The city name.
35
     *
36
     * @var string|null
37
     */
38
    public $cityName;
39
    /**
40
     * The zip code.
41
     *
42
     * @var string|null
43
     */
44
    public $zipCode;
45
    /**
46
     * The latitude.
47
     *
48
     * @var string|null
49
     */
50
    public $latitude;
51
    /**
52
     * The longitude.
53
     *
54
     * @var string|null
55
     */
56
    public $longitude;
57
58
    /**
59
     * Response constructor.
60
     *
61
     * @param array $driverResponse
62
     */
63
    public function __construct($driverResponse)
64
    {
65
        $this->ip          = $driverResponse['ip'] ?? $driverResponse['IPv4'];
66
        $this->countryCode = $driverResponse['country_code'];
67
        $this->countryName = $driverResponse['country_name'];
68
        $this->regionName  = $driverResponse['state'];
69
        $this->cityName    = $driverResponse['city'];
70
        $this->zipCode     = $driverResponse['postal'];
71
        $this->latitude    = $driverResponse['latitude'];
72
        $this->longitude   = $driverResponse['longitude'];
73
    }
74
75
    /**
76
     * Determine if the response is empty.
77
     *
78
     * @return bool
79
     */
80
    public function isEmpty()
81
    {
82
        $data = $this->toArray();
83
84
        unset($data['ip']);
85
86
        return empty(array_filter($data));
87
    }
88
89
    /**
90
     * Get the instance as an array.
91
     *
92
     * @return array
93
     */
94
    public function toArray()
95
    {
96
        return get_object_vars($this);
97
    }
98
}
99