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 Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Contracts\Support\Jsonable;
8
use JsonSerializable;
9
 
10
/**
11
 * @template TKey of array-key
12
 * @template TValue
13
 *
14
 * @implements \Illuminate\Contracts\Support\Arrayable<TKey, TValue>
15
 * @implements \ArrayAccess<TKey, TValue>
16
 */
17
class Fluent implements Arrayable, ArrayAccess, Jsonable, JsonSerializable
18
{
19
    /**
20
     * All of the attributes set on the fluent instance.
21
     *
22
     * @var array<TKey, TValue>
23
     */
24
    protected $attributes = [];
25
 
26
    /**
27
     * Create a new fluent instance.
28
     *
29
     * @param  iterable<TKey, TValue>  $attributes
30
     * @return void
31
     */
32
    public function __construct($attributes = [])
33
    {
34
        foreach ($attributes as $key => $value) {
35
            $this->attributes[$key] = $value;
36
        }
37
    }
38
 
39
    /**
40
     * Get an attribute from the fluent instance.
41
     *
42
     * @template TGetDefault
43
     *
44
     * @param  TKey  $key
45
     * @param  TGetDefault|(\Closure(): TGetDefault)  $default
46
     * @return TValue|TGetDefault
47
     */
48
    public function get($key, $default = null)
49
    {
50
        if (array_key_exists($key, $this->attributes)) {
51
            return $this->attributes[$key];
52
        }
53
 
54
        return value($default);
55
    }
56
 
57
    /**
58
     * Get the attributes from the fluent instance.
59
     *
60
     * @return array<TKey, TValue>
61
     */
62
    public function getAttributes()
63
    {
64
        return $this->attributes;
65
    }
66
 
67
    /**
68
     * Convert the fluent instance to an array.
69
     *
70
     * @return array<TKey, TValue>
71
     */
72
    public function toArray()
73
    {
74
        return $this->attributes;
75
    }
76
 
77
    /**
78
     * Convert the object into something JSON serializable.
79
     *
80
     * @return array<TKey, TValue>
81
     */
82
    public function jsonSerialize(): array
83
    {
84
        return $this->toArray();
85
    }
86
 
87
    /**
88
     * Convert the fluent instance to JSON.
89
     *
90
     * @param  int  $options
91
     * @return string
92
     */
93
    public function toJson($options = 0)
94
    {
95
        return json_encode($this->jsonSerialize(), $options);
96
    }
97
 
98
    /**
99
     * Determine if the given offset exists.
100
     *
101
     * @param  TKey  $offset
102
     * @return bool
103
     */
104
    public function offsetExists($offset): bool
105
    {
106
        return isset($this->attributes[$offset]);
107
    }
108
 
109
    /**
110
     * Get the value for a given offset.
111
     *
112
     * @param  TKey  $offset
113
     * @return TValue|null
114
     */
115
    public function offsetGet($offset): mixed
116
    {
117
        return $this->get($offset);
118
    }
119
 
120
    /**
121
     * Set the value at the given offset.
122
     *
123
     * @param  TKey  $offset
124
     * @param  TValue  $value
125
     * @return void
126
     */
127
    public function offsetSet($offset, $value): void
128
    {
129
        $this->attributes[$offset] = $value;
130
    }
131
 
132
    /**
133
     * Unset the value at the given offset.
134
     *
135
     * @param  TKey  $offset
136
     * @return void
137
     */
138
    public function offsetUnset($offset): void
139
    {
140
        unset($this->attributes[$offset]);
141
    }
142
 
143
    /**
144
     * Handle dynamic calls to the fluent instance to set attributes.
145
     *
146
     * @param  TKey  $method
147
     * @param  array{0: ?TValue}  $parameters
148
     * @return $this
149
     */
150
    public function __call($method, $parameters)
151
    {
152
        $this->attributes[$method] = count($parameters) > 0 ? reset($parameters) : true;
153
 
154
        return $this;
155
    }
156
 
157
    /**
158
     * Dynamically retrieve the value of an attribute.
159
     *
160
     * @param  TKey  $key
161
     * @return TValue|null
162
     */
163
    public function __get($key)
164
    {
165
        return $this->get($key);
166
    }
167
 
168
    /**
169
     * Dynamically set the value of an attribute.
170
     *
171
     * @param  TKey  $key
172
     * @param  TValue  $value
173
     * @return void
174
     */
175
    public function __set($key, $value)
176
    {
177
        $this->offsetSet($key, $value);
178
    }
179
 
180
    /**
181
     * Dynamically check if an attribute is set.
182
     *
183
     * @param  TKey  $key
184
     * @return bool
185
     */
186
    public function __isset($key)
187
    {
188
        return $this->offsetExists($key);
189
    }
190
 
191
    /**
192
     * Dynamically unset an attribute.
193
     *
194
     * @param  TKey  $key
195
     * @return void
196
     */
197
    public function __unset($key)
198
    {
199
        $this->offsetUnset($key);
200
    }
201
}