Debian Codegraph @

lib/3rdParty/composer/illuminate/support/Fluent.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support;
4
5 use ArrayAccess;
6 use ArrayIterator;
7 use Illuminate\Contracts\Support\Arrayable;
8 use Illuminate\Contracts\Support\Jsonable;
9 use Illuminate\Support\Traits\Conditionable;
10 use Illuminate\Support\Traits\InteractsWithData;
11 use Illuminate\Support\Traits\Macroable;
12 use IteratorAggregate;
13 use JsonSerializable;
14 use Traversable;
15
16 /**
17 * @template TKey of array-key
18 * @template TValue
19 *
20 * @implements \Illuminate\Contracts\Support\Arrayable<TKey, TValue>
21 * @implements \ArrayAccess<TKey, TValue>
22 */
23 class Fluent implements Arrayable, ArrayAccess, IteratorAggregate, Jsonable, JsonSerializable
24 {
25 use Conditionable, InteractsWithData, Macroable {
26 __call as macroCall;
27 }
28
29 /**
30 * All of the attributes set on the fluent instance.
31 *
32 * @var array<TKey, TValue>
33 */
34 protected $attributes = [];
35
36 /**
37 * Create a new fluent instance.
38 *
39 * @param iterable<TKey, TValue> $attributes
40 */
41 public function __construct($attributes = [])
42 {
43 $this->fill($attributes);
44 }
45
46 /**
47 * Create a new fluent instance.
48 *
49 * @param iterable<TKey, TValue> $attributes
50 * @return static
51 */
52 public static function make($attributes = [])
53 {
54 return new static($attributes);
55 }
56
57 /**
58 * Get an attribute from the fluent instance using "dot" notation.
59 *
60 * @template TGetDefault
61 *
62 * @param TKey $key
63 * @param TGetDefault|(\Closure(): TGetDefault) $default
64 * @return TValue|TGetDefault
65 */
66 public function get($key, $default = null)
67 {
68 return data_get($this->attributes, $key, $default);
69 }
70
71 /**
72 * Set an attribute on the fluent instance using "dot" notation.
73 *
74 * @param TKey $key
75 * @param TValue $value
76 * @return $this
77 */
78 public function set($key, $value)
79 {
80 data_set($this->attributes, $key, $value);
81
82 return $this;
83 }
84
85 /**
86 * Fill the fluent instance with an array of attributes.
87 *
88 * @param iterable<TKey, TValue> $attributes
89 * @return $this
90 */
91 public function fill($attributes)
92 {
93 foreach ($attributes as $key => $value) {
94 $this->attributes[$key] = $value;
95 }
96
97 return $this;
98 }
99
100 /**
101 * Get an attribute from the fluent instance.
102 *
103 * @param string $key
104 * @param mixed $default
105 * @return mixed
106 */
107 public function value($key, $default = null)
108 {
109 if (array_key_exists($key, $this->attributes)) {
110 return $this->attributes[$key];
111 }
112
113 return value($default);
114 }
115
116 /**
117 * Get the value of the given key as a new Fluent instance.
118 *
119 * @param string $key
120 * @param mixed $default
121 * @return static
122 */
123 public function scope($key, $default = null)
124 {
125 return new static(
126 (array) $this->get($key, $default)
127 );
128 }
129
130 /**
131 * Get all of the attributes from the fluent instance.
132 *
133 * @param mixed $keys
134 * @return array
135 */
136 public function all($keys = null)
137 {
138 $data = $this->data();
139
140 if (! $keys) {
141 return $data;
142 }
143
144 $results = [];
145
146 foreach (is_array($keys) ? $keys : func_get_args() as $key) {
147 Arr::set($results, $key, Arr::get($data, $key));
148 }
149
150 return $results;
151 }
152
153 /**
154 * Get data from the fluent instance.
155 *
156 * @param string|null $key
157 * @param mixed $default
158 * @return mixed
159 */
160 protected function data($key = null, $default = null)
161 {
162 return $this->get($key, $default);
163 }
164
165 /**
166 * Get the attributes from the fluent instance.
167 *
168 * @return array<TKey, TValue>
169 */
170 public function getAttributes()
171 {
172 return $this->attributes;
173 }
174
175 /**
176 * Convert the fluent instance to an array.
177 *
178 * @return array<TKey, TValue>
179 */
180 public function toArray()
181 {
182 return $this->attributes;
183 }
184
185 /**
186 * Convert the object into something JSON serializable.
187 *
188 * @return array<TKey, TValue>
189 */
190 public function jsonSerialize(): array
191 {
192 return $this->toArray();
193 }
194
195 /**
196 * Convert the fluent instance to JSON.
197 *
198 * @param int $options
199 * @return string
200 */
201 public function toJson($options = 0)
202 {
203 return json_encode($this->jsonSerialize(), $options);
204 }
205
206 /**
207 * Convert the fluent instance to pretty print formatted JSON.
208 *
209 * @param int $options
210 * @return string
211 */
212 public function toPrettyJson(int $options = 0)
213 {
214 return $this->toJson(JSON_PRETTY_PRINT | $options);
215 }
216
217 /**
218 * Determine if the fluent instance is empty.
219 *
220 * @return bool
221 */
222 public function isEmpty(): bool
223 {
224 return empty($this->attributes);
225 }
226
227 /**
228 * Determine if the fluent instance is not empty.
229 *
230 * @return bool
231 */
232 public function isNotEmpty(): bool
233 {
234 return ! $this->isEmpty();
235 }
236
237 /**
238 * Determine if the given offset exists.
239 *
240 * @param TKey $offset
241 * @return bool
242 */
243 public function offsetExists($offset): bool
244 {
245 return isset($this->attributes[$offset]);
246 }
247
248 /**
249 * Get the value for a given offset.
250 *
251 * @param TKey $offset
252 * @return TValue|null
253 */
254 public function offsetGet($offset): mixed
255 {
256 return $this->value($offset);
257 }
258
259 /**
260 * Set the value at the given offset.
261 *
262 * @param TKey $offset
263 * @param TValue $value
264 * @return void
265 */
266 public function offsetSet($offset, $value): void
267 {
268 $this->attributes[$offset] = $value;
269 }
270
271 /**
272 * Unset the value at the given offset.
273 *
274 * @param TKey $offset
275 * @return void
276 */
277 public function offsetUnset($offset): void
278 {
279 unset($this->attributes[$offset]);
280 }
281
282 /**
283 * Get an iterator for the attributes.
284 *
285 * @return ArrayIterator<TKey, TValue>
286 */
287 public function getIterator(): Traversable
288 {
289 return new ArrayIterator($this->attributes);
290 }
291
292 /**
293 * Handle dynamic calls to the fluent instance to set attributes.
294 *
295 * @param TKey $method
296 * @param array{0: ?TValue} $parameters
297 * @return $this
298 */
299 public function __call($method, $parameters)
300 {
301 if (static::hasMacro($method)) {
302 return $this->macroCall($method, $parameters);
303 }
304
305 $this->attributes[$method] = count($parameters) > 0 ? array_first($parameters) : true;
306
307 return $this;
308 }
309
310 /**
311 * Dynamically retrieve the value of an attribute.
312 *
313 * @param TKey $key
314 * @return TValue|null
315 */
316 public function __get($key)
317 {
318 return $this->value($key);
319 }
320
321 /**
322 * Dynamically set the value of an attribute.
323 *
324 * @param TKey $key
325 * @param TValue $value
326 * @return void
327 */
328 public function __set($key, $value)
329 {
330 $this->offsetSet($key, $value);
331 }
332
333 /**
334 * Dynamically check if an attribute is set.
335 *
336 * @param TKey $key
337 * @return bool
338 */
339 public function __isset($key)
340 {
341 return $this->offsetExists($key);
342 }
343
344 /**
345 * Dynamically unset an attribute.
346 *
347 * @param TKey $key
348 * @return void
349 */
350 public function __unset($key)
351 {
352 $this->offsetUnset($key);
353 }
354 }