Skip to content

Commit 258e1b7

Browse files
committed
Update Fluent class from Laravel 12
1 parent 056e75b commit 258e1b7

File tree

1 file changed

+83
-5
lines changed

1 file changed

+83
-5
lines changed

src/Html/Fluent.php

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
namespace Yajra\DataTables\Html;
44

55
use ArrayAccess;
6+
use ArrayIterator;
67
use Illuminate\Contracts\Support\Arrayable;
78
use Illuminate\Contracts\Support\Jsonable;
9+
use Illuminate\Support\Traits\Conditionable;
10+
use Illuminate\Support\Traits\Macroable;
11+
use IteratorAggregate;
812
use JsonSerializable;
13+
use Traversable;
914

1015
/**
1116
* @template TKey of array-key
@@ -14,8 +19,12 @@
1419
* @implements \Illuminate\Contracts\Support\Arrayable<TKey, TValue>
1520
* @implements \ArrayAccess<TKey, TValue>
1621
*/
17-
class Fluent implements Arrayable, ArrayAccess, Jsonable, JsonSerializable
22+
class Fluent implements Arrayable, ArrayAccess, IteratorAggregate, Jsonable, JsonSerializable
1823
{
24+
use Conditionable, Macroable {
25+
__call as macroCall;
26+
}
27+
1928
/**
2029
* All of the attributes set on the fluent instance.
2130
*
@@ -31,9 +40,7 @@ class Fluent implements Arrayable, ArrayAccess, Jsonable, JsonSerializable
3140
*/
3241
public function __construct($attributes = [])
3342
{
34-
foreach ($attributes as $key => $value) {
35-
$this->attributes[$key] = $value;
36-
}
43+
$this->fill($attributes);
3744
}
3845

3946
/**
@@ -50,6 +57,35 @@ public function get($key, $default = null)
5057
return data_get($this->attributes, $key, $default);
5158
}
5259

60+
/**
61+
* Set an attribute on the fluent instance using "dot" notation.
62+
*
63+
* @param TKey $key
64+
* @param TValue $value
65+
* @return $this
66+
*/
67+
public function set($key, $value)
68+
{
69+
data_set($this->attributes, $key, $value);
70+
71+
return $this;
72+
}
73+
74+
/**
75+
* Fill the fluent instance with an array of attributes.
76+
*
77+
* @param iterable<TKey, TValue> $attributes
78+
* @return $this
79+
*/
80+
public function fill($attributes)
81+
{
82+
foreach ($attributes as $key => $value) {
83+
$this->attributes[$key] = $value;
84+
}
85+
86+
return $this;
87+
}
88+
5389
/**
5490
* Get an attribute from the fluent instance.
5591
*
@@ -132,6 +168,34 @@ public function toJson($options = 0)
132168
return json_encode($this->jsonSerialize(), $options);
133169
}
134170

171+
/**
172+
* Convert the fluent instance to pretty print formatted JSON.
173+
*
174+
* @params int $options
175+
*
176+
* @return string
177+
*/
178+
public function toPrettyJson(int $options = 0)
179+
{
180+
return $this->toJson(JSON_PRETTY_PRINT | $options);
181+
}
182+
183+
/**
184+
* Determine if the fluent instance is empty.
185+
*/
186+
public function isEmpty(): bool
187+
{
188+
return empty($this->attributes);
189+
}
190+
191+
/**
192+
* Determine if the fluent instance is not empty.
193+
*/
194+
public function isNotEmpty(): bool
195+
{
196+
return ! $this->isEmpty();
197+
}
198+
135199
/**
136200
* Determine if the given offset exists.
137201
*
@@ -174,6 +238,16 @@ public function offsetUnset($offset): void
174238
unset($this->attributes[$offset]);
175239
}
176240

241+
/**
242+
* Get an iterator for the attributes.
243+
*
244+
* @return ArrayIterator<TKey, TValue>
245+
*/
246+
public function getIterator(): Traversable
247+
{
248+
return new ArrayIterator($this->attributes);
249+
}
250+
177251
/**
178252
* Handle dynamic calls to the fluent instance to set attributes.
179253
*
@@ -183,7 +257,11 @@ public function offsetUnset($offset): void
183257
*/
184258
public function __call($method, $parameters)
185259
{
186-
$this->attributes[$method] = count($parameters) > 0 ? reset($parameters) : true;
260+
if (static::hasMacro($method)) {
261+
return $this->macroCall($method, $parameters);
262+
}
263+
264+
$this->attributes[$method] = count($parameters) > 0 ? array_first($parameters) : true;
187265

188266
return $this;
189267
}

0 commit comments

Comments
 (0)