Completed
Push — master ( 61eea1...157236 )
by Antonio Carlos
03:35
created

Optional::__isset()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 0
loc 12
ccs 0
cts 6
cp 0
crap 20
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace IlluminateAgnostic\Str\Support;
4
5
use ArrayAccess;
6
use ArrayObject;
7
8
class Optional implements ArrayAccess
9
{
10
    use Traits\Macroable {
11
        __call as macroCall;
12
    }
13
14
    /**
15
     * The underlying object.
16
     *
17
     * @var mixed
18
     */
19
    protected $value;
20
21
    /**
22
     * Create a new optional instance.
23
     *
24
     * @param  mixed  $value
25
     * @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...
26
     */
27
    public function __construct($value)
28
    {
29
        $this->value = $value;
30
    }
31
32
    /**
33
     * Dynamically access a property on the underlying object.
34
     *
35
     * @param  string  $key
36
     * @return mixed
37
     */
38
    public function __get($key)
39
    {
40
        if (is_object($this->value)) {
41
            return $this->value->{$key} ?? null;
42
        }
43
    }
44
45
    /**
46
     * Dynamically check a property exists on the underlying object.
47
     *
48
     * @param $name
49
     * @return bool
50
     */
51
    public function __isset($name)
52
    {
53
        if (is_object($this->value)) {
54
            return isset($this->value->{$name});
55
        }
56
57
        if (is_array($this->value) || $this->value instanceof ArrayObject) {
58
            return isset($this->value[$name]);
59
        }
60
61
        return false;
62
    }
63
64
    /**
65
     * Determine if an item exists at an offset.
66
     *
67
     * @param  mixed  $key
68
     * @return bool
69
     */
70
    public function offsetExists($key)
71
    {
72
        return Arr::accessible($this->value) && Arr::exists($this->value, $key);
73
    }
74
75
    /**
76
     * Get an item at a given offset.
77
     *
78
     * @param  mixed  $key
79
     * @return mixed
80
     */
81
    public function offsetGet($key)
82
    {
83
        return Arr::get($this->value, $key);
84
    }
85
86
    /**
87
     * Set the item at a given offset.
88
     *
89
     * @param  mixed  $key
90
     * @param  mixed  $value
91
     * @return void
92
     */
93
    public function offsetSet($key, $value)
94
    {
95
        if (Arr::accessible($this->value)) {
96
            $this->value[$key] = $value;
97
        }
98
    }
99
100
    /**
101
     * Unset the item at a given offset.
102
     *
103
     * @param  string  $key
104
     * @return void
105
     */
106
    public function offsetUnset($key)
107
    {
108
        if (Arr::accessible($this->value)) {
109
            unset($this->value[$key]);
110
        }
111
    }
112
113
    /**
114
     * Dynamically pass a method to the underlying object.
115
     *
116
     * @param  string  $method
117
     * @param  array  $parameters
118
     * @return mixed
119
     */
120
    public function __call($method, $parameters)
121
    {
122
        if (static::hasMacro($method)) {
123
            return $this->macroCall($method, $parameters);
124
        }
125
126
        if (is_object($this->value)) {
127
            return $this->value->{$method}(...$parameters);
128
        }
129
    }
130
}
131