Completed
Push — master ( a51ab2...247c5c )
by Antonio Carlos
03:22
created

Optional::offsetExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace IlluminateAgnostic\Str\Support;
4
5
use ArrayAccess;
6
7
class Optional implements ArrayAccess
8
{
9
    use Traits\Macroable {
10
        __call as macroCall;
11
    }
12
13
    /**
14
     * The underlying object.
15
     *
16
     * @var mixed
17
     */
18
    protected $value;
19
20
    /**
21
     * Create a new optional instance.
22
     *
23
     * @param  mixed  $value
24
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
25
     */
26
    public function __construct($value)
27
    {
28
        $this->value = $value;
29
    }
30
31
    /**
32
     * Dynamically access a property on the underlying object.
33
     *
34
     * @param  string  $key
35
     * @return mixed
36
     */
37
    public function __get($key)
38
    {
39
        if (is_object($this->value)) {
40
            return $this->value->{$key} ?? null;
41
        }
42
    }
43
44
    /**
45
     * Dynamically pass a method to the underlying object.
46
     *
47
     * @param  string  $method
48
     * @param  array  $parameters
49
     * @return mixed
50
     */
51
    public function __call($method, $parameters)
52
    {
53
        if (static::hasMacro($method)) {
54
            return $this->macroCall($method, $parameters);
55
        }
56
57
        if (is_object($this->value)) {
58
            return $this->value->{$method}(...$parameters);
59
        }
60
    }
61
62
    /**
63
     * Determine if an item exists at an offset.
64
     *
65
     * @param  mixed  $key
66
     * @return bool
67
     */
68
    public function offsetExists($key)
69
    {
70
        return Arr::accessible($this->value) && Arr::exists($this->value, $key);
71
    }
72
73
    /**
74
     * Get an item at a given offset.
75
     *
76
     * @param  mixed  $key
77
     * @return mixed
78
     */
79
    public function offsetGet($key)
80
    {
81
        return Arr::get($this->value, $key);
82
    }
83
84
    /**
85
     * Set the item at a given offset.
86
     *
87
     * @param  mixed  $key
88
     * @param  mixed  $value
89
     * @return void
90
     */
91
    public function offsetSet($key, $value)
92
    {
93
        if (Arr::accessible($this->value)) {
94
            $this->value[$key] = $value;
95
        }
96
    }
97
98
    /**
99
     * Unset the item at a given offset.
100
     *
101
     * @param  string  $key
102
     * @return void
103
     */
104
    public function offsetUnset($key)
105
    {
106
        if (Arr::accessible($this->value)) {
107
            unset($this->value[$key]);
108
        }
109
    }
110
}
111