Completed
Pull Request — master (#4)
by ARCANEDEV
05:31
created

AbstractResponse::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
     |  Traits
19
     | -----------------------------------------------------------------
20
     */
21
22
    use CanBeJsonable;
23
24
    /* -----------------------------------------------------------------
25
     |  Properties
26
     | -----------------------------------------------------------------
27
     */
28
29
    /**
30
     * The response's data.
31
     *
32
     * @var  array
33
     */
34
    protected $data = [];
35
36
    /* -----------------------------------------------------------------
37
     |  Constructor
38
     | -----------------------------------------------------------------
39
     */
40
41
    /**
42
     * AbstractResponse constructor.
43
     *
44
     * @param  array  $data
45
     */
46 102
    public function __construct(array $data = [])
47
    {
48 102
        $this->data = $data;
49 102
    }
50
51
    /* -----------------------------------------------------------------
52
     |  Getters & Setters
53
     | -----------------------------------------------------------------
54
     */
55
56
    /**
57
     * Get the raw response.
58
     *
59
     * @return array
60
     */
61 96
    public function getRaw()
62
    {
63 96
        return $this->data;
64
    }
65
66
    /* -----------------------------------------------------------------
67
     |  Main Methods
68
     | -----------------------------------------------------------------
69
     */
70
71
    /**
72
     * Get a data with a given key.
73
     *
74
     * @param  string      $key
75
     * @param  mixed|null  $default
76
     *
77
     * @return mixed
78
     */
79 90
    public function get($key, $default = null)
80
    {
81 90
        return Arr::get($this->getRaw(), $key, $default);
82
    }
83
84
    /**
85
     * Check if the response's status is OK.
86
     *
87
     * @return bool
88
     */
89 33
    public function isOk()
90
    {
91 33
        return $this->get('status') === 'OK';
92
    }
93
}
94