Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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