Completed
Push — master ( bc8bc1...b082b5 )
by Lynh
11:33
created

InteractsWithResponse::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Jenky\Hermes\Concerns;
4
5
use Illuminate\Support\Optional;
6
7
trait InteractsWithResponse
8
{
9
    /**
10
     * The response data.
11
     *
12
     * @var array
13
     */
14
    protected $data;
15
16
    /**
17
     * Get an attribute from the response data.
18
     *
19
     * @param  string $key
20
     * @param  mixed $default
21
     * @return mixed
22
     */
23
    public function get($key, $default = null)
24
    {
25
        return data_get($this->data, $key, $default);
26
    }
27
28
    /**
29
     * Set an attribute to the response data.
30
     *
31
     * @param  string $key
32
     * @param  mixed $value
33
     * @return array
34
     */
35
    public function set($key, $value)
36
    {
37
        return data_set($this->data, $key, $value);
38
    }
39
40
    /**
41
     * Determine if the given key exists in the response body.
42
     *
43
     * @param  string|array  $key
44
     * @return bool
45
     */
46
    public function exists($key)
47
    {
48
        $optional = new Optional($this->data);
49
50
        return isset($optional[$key]);
51
    }
52
53
    /**
54
     * Dynamically retrieve the value of an attribute.
55
     *
56
     * @param  string  $key
57
     * @return mixed
58
     */
59
    public function __get($key)
60
    {
61
        return $this->get($key);
62
    }
63
64
    /**
65
     * Dynamically set the value of an attribute.
66
     *
67
     * @param  string  $key
68
     * @param  mixed   $value
69
     * @return void
70
     */
71
    public function __set($key, $value)
72
    {
73
        $this->set($key, $value);
74
    }
75
76
    /**
77
     * Dynamically check if an attribute is set.
78
     *
79
     * @param  string  $key
80
     * @return bool
81
     */
82
    public function __isset($key)
83
    {
84
        return $this->exists($key);
85
    }
86
}
87