PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.0
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.0
6.2.1 6.2.0 6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / libraries / framework / Supports / Fluent.php
kirki / libraries / framework / Supports Last commit date
Facades 1 week ago Traits 1 month ago Arr.php 1 week ago DataCaster.php 1 month ago Fluent.php 1 week ago HigherOrderTapProxy.php 1 month ago MediaAttachment.php 1 month ago MessagesBag.php 1 week ago Somoy.php 1 week ago Str.php 1 week ago Url.php 1 month ago Utils.php 1 month ago
Fluent.php
412 lines
1 <?php
2
3 /**
4 * Lightweight dynamic object bag implementing ArrayAccess, JsonSerializable, and iteration.
5 * Stores arbitrary key-value attributes with fill, get, and fluent access patterns.
6 * Alternative to stdClass for structured but schema-free data.
7 *
8 * @package Framework
9 * @subpackage Supports
10 * @since 1.0.0
11 */
12 namespace Kirki\Framework\Supports;
13
14 \defined('ABSPATH') || exit;
15 use ArrayAccess;
16 use ArrayIterator;
17 use Kirki\Framework\Contracts\Support\Arrayable;
18 use Kirki\Framework\Contracts\Support\Jsonable;
19 use Kirki\Framework\Supports\Traits\Macroable;
20 use IteratorAggregate;
21 use JsonSerializable;
22 use Traversable;
23 use function Kirki\Framework\deep_get;
24 use function Kirki\Framework\deep_set;
25 use function Kirki\Framework\Polyfill\array_first;
26 use function Kirki\Framework\value;
27 class Fluent implements ArrayAccess, IteratorAggregate, Arrayable, Jsonable, JsonSerializable
28 {
29 use Macroable {
30 __call as macro_call;
31 }
32 /**
33 * The attributes.
34 *
35 * @var array
36 *
37 * @since 1.0.0
38 */
39 protected $attributes = [];
40 /**
41 * Create a new instance.
42 *
43 * @param array $attributes The attributes array.
44 *
45 * @return void
46 *
47 * @since 1.0.0
48 */
49 public function __construct(array $attributes = [])
50 {
51 $this->fill($attributes);
52 }
53 /**
54 * Create a new instance.
55 *
56 * @param array $attributes The attributes array.
57 *
58 * @return static
59 *
60 * @since 1.0.0
61 */
62 public static function make(array $attributes = [])
63 {
64 return new static($attributes);
65 }
66 /**
67 * Fill the attributes with the given array.
68 *
69 * @param array $attributes The attributes array.
70 *
71 * @return void
72 *
73 * @since 1.0.0
74 */
75 protected function fill(array $attributes)
76 {
77 foreach ($attributes as $key => $value) {
78 $this->attributes[$key] = $value;
79 }
80 return $this;
81 }
82 /**
83 * Get the value of a given attribute.
84 *
85 * @param string $key The attribute key.
86 * @param mixed $default The default value if the attribute does not exist.
87 *
88 * @return mixed
89 *
90 * @since 1.0.0
91 */
92 public function value($key, $default = null)
93 {
94 if ($this->exists($key)) {
95 return $this->get($key);
96 }
97 return value($default);
98 }
99 /**
100 * Get the keys of the attributes.
101 *
102 * @return array
103 *
104 * @since 1.0.0
105 */
106 public function keys()
107 {
108 return \array_keys($this->attributes);
109 }
110 /**
111 * Get the value of a given attribute.
112 *
113 * @param string $key The attribute key.
114 * @param mixed $default The default value if the attribute does not exist.
115 *
116 * @return mixed
117 *
118 * @since 1.0.0
119 */
120 public function get($key, $default = null)
121 {
122 return deep_get($this->attributes, $key, $default);
123 }
124 /**
125 * Check if a given attribute exists.
126 *
127 * @param string $key The attribute key.
128 *
129 * @return bool
130 *
131 * @since 1.0.0
132 */
133 public function exists($key)
134 {
135 return \array_key_exists($key, $this->attributes);
136 }
137 /**
138 * Set the value of a given attribute.
139 *
140 * @param string $key The attribute key.
141 * @param mixed $value The value to set.
142 *
143 * @return $this
144 *
145 * @since 1.0.0
146 */
147 public function set($key, $value)
148 {
149 deep_set($this->attributes, $key, $value);
150 return $this;
151 }
152 /**
153 * Remove a given attribute.
154 *
155 * @param string $key The attribute key.
156 *
157 * @return $this
158 *
159 * @since 1.0.0
160 */
161 public function remove($key)
162 {
163 if ($this->exists($key)) {
164 unset($this->attributes[$key]);
165 }
166 return $this;
167 }
168 /**
169 * Get the value of a given attribute.
170 *
171 * @param string $keys The attribute keys.
172 *
173 * @return mixed
174 *
175 * @since 1.0.0
176 */
177 public function all($keys = null)
178 {
179 $data = $this->data();
180 if (!$keys) {
181 return $data;
182 }
183 $results = [];
184 $keys = \is_array($keys) ? $keys : \func_get_args();
185 foreach ($keys as $key) {
186 Arr::set($results, $key, Arr::get($data, $key));
187 }
188 return $results;
189 }
190 /**
191 * Get the value of a given attribute.
192 *
193 * @param string $key The attribute key.
194 * @param mixed $default The default value if the attribute does not exist.
195 *
196 * @return mixed
197 *
198 * @since 1.0.0
199 */
200 public function data($key = null, $default = null)
201 {
202 return $this->get($key, $default);
203 }
204 /**
205 * Get the attributes.
206 *
207 * @return array
208 *
209 * @since 1.0.0
210 */
211 public function get_attributes()
212 {
213 return $this->attributes;
214 }
215 /**
216 * Check if the attributes are empty.
217 *
218 * @return bool
219 *
220 * @since 1.0.0
221 */
222 public function empty() : bool
223 {
224 return empty($this->attributes);
225 }
226 /**
227 * Check if the attributes are not empty.
228 *
229 * @return bool
230 *
231 * @since 1.0.0
232 */
233 public function not_empty() : bool
234 {
235 return !$this->empty();
236 }
237 /**
238 * Get all attributes as an array.
239 *
240 * @return array
241 *
242 * @since 1.0.0
243 */
244 public function to_array()
245 {
246 return $this->attributes;
247 }
248 /**
249 * Magic getter for attributes.
250 *
251 * @param string $name The attribute name.
252 *
253 * @return mixed
254 *
255 * @since 1.0.0
256 */
257 public function __get($name)
258 {
259 return $this->get($name);
260 }
261 /**
262 * Magic setter for attributes.
263 *
264 * @param string $name The attribute name.
265 * @param mixed $value The value to set.
266 *
267 * @return void
268 *
269 * @since 1.0.0
270 */
271 public function __set($name, $value)
272 {
273 $this->set($name, $value);
274 }
275 /**
276 * Magic isset to check if an attribute exists.
277 *
278 * @param string $name The attribute name.
279 *
280 * @return bool
281 *
282 * @since 1.0.0
283 */
284 public function __isset($name)
285 {
286 return $this->exists($name);
287 }
288 /**
289 * Magic unset to remove an attribute.
290 *
291 * @param string $name The attribute name.
292 *
293 * @return void
294 *
295 * @since 1.0.0
296 */
297 public function __unset($name)
298 {
299 unset($this->attributes[$name]);
300 }
301 /**
302 * Determine if the given offset exists.
303 *
304 * @param mixed $offset The offset.
305 *
306 * @return bool
307 *
308 * @since 1.0.0
309 */
310 public function offsetExists($offset) : bool
311 {
312 return isset($this->attributes[$offset]);
313 }
314 /**
315 * OffsetGet.
316 *
317 * @param mixed $offset The offset.
318 *
319 * @return void
320 *
321 * @since 1.0.0
322 */
323 #[\ReturnTypeWillChange]
324 public function offsetGet($offset)
325 {
326 return $this->value($offset);
327 }
328 /**
329 * Set the value at the given offset.
330 *
331 * @param mixed $offset The offset.
332 * @param mixed $value The value.
333 *
334 * @return void
335 *
336 * @since 1.0.0
337 */
338 public function offsetSet($offset, $value) : void
339 {
340 $this->attributes[$offset] = $value;
341 }
342 /**
343 * Unset the value at the given offset.
344 *
345 * @param mixed $offset The offset.
346 *
347 * @return void
348 *
349 * @since 1.0.0
350 */
351 public function offsetUnset($offset) : void
352 {
353 unset($this->attributes[$offset]);
354 }
355 /**
356 * Get an iterator for the attributes.
357 *
358 * @template TKey
359 * @template TValue
360 *
361 * @return ArrayIterator<TKey, TValue>
362 *
363 * @since 1.0.0
364 */
365 public function getIterator() : Traversable
366 {
367 return new ArrayIterator($this->attributes);
368 }
369 /**
370 * Specify data which should be serialized to JSON.
371 *
372 * @return array
373 *
374 * @since 1.0.0
375 */
376 public function jsonSerialize() : array
377 {
378 return $this->to_array();
379 }
380 /**
381 * Convert the object to a JSON string.
382 *
383 * @param mixed $options The options array.
384 *
385 * @return string
386 *
387 * @since 1.0.0
388 */
389 public function to_json($options = 0)
390 {
391 return Arr::json_encode($this->jsonSerialize(), $options);
392 }
393 /**
394 * Magic call to set an attribute by method name.
395 *
396 * @param string $method The method name (attribute key).
397 * @param array $arguments The arguments to set as value.
398 *
399 * @return $this
400 *
401 * @since 1.0.0
402 */
403 public function __call($method, $arguments)
404 {
405 if (static::has_macro($method)) {
406 return $this->macro_call($method, $arguments);
407 }
408 $this->attributes[$method] = \count($arguments) > 0 ? array_first($arguments) : \true;
409 return $this;
410 }
411 }
412