diff --git a/src/Html/Fluent.php b/src/Html/Fluent.php
index 2ff5837..851f9d0 100644
--- a/src/Html/Fluent.php
+++ b/src/Html/Fluent.php
@@ -3,9 +3,14 @@
namespace Yajra\DataTables\Html;
use ArrayAccess;
+use ArrayIterator;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
+use Illuminate\Support\Traits\Conditionable;
+use Illuminate\Support\Traits\Macroable;
+use IteratorAggregate;
use JsonSerializable;
+use Traversable;
/**
* @template TKey of array-key
@@ -14,8 +19,12 @@
* @implements \Illuminate\Contracts\Support\Arrayable
* @implements \ArrayAccess
*/
-class Fluent implements Arrayable, ArrayAccess, Jsonable, JsonSerializable
+class Fluent implements Arrayable, ArrayAccess, IteratorAggregate, Jsonable, JsonSerializable
{
+ use Conditionable, Macroable {
+ __call as macroCall;
+ }
+
/**
* All of the attributes set on the fluent instance.
*
@@ -31,9 +40,7 @@ class Fluent implements Arrayable, ArrayAccess, Jsonable, JsonSerializable
*/
public function __construct($attributes = [])
{
- foreach ($attributes as $key => $value) {
- $this->attributes[$key] = $value;
- }
+ $this->fill($attributes);
}
/**
@@ -50,6 +57,35 @@ public function get($key, $default = null)
return data_get($this->attributes, $key, $default);
}
+ /**
+ * Set an attribute on the fluent instance using "dot" notation.
+ *
+ * @param TKey $key
+ * @param TValue $value
+ * @return $this
+ */
+ public function set($key, $value)
+ {
+ data_set($this->attributes, $key, $value);
+
+ return $this;
+ }
+
+ /**
+ * Fill the fluent instance with an array of attributes.
+ *
+ * @param iterable $attributes
+ * @return $this
+ */
+ public function fill($attributes)
+ {
+ foreach ($attributes as $key => $value) {
+ $this->attributes[$key] = $value;
+ }
+
+ return $this;
+ }
+
/**
* Get an attribute from the fluent instance.
*
@@ -132,6 +168,34 @@ public function toJson($options = 0)
return json_encode($this->jsonSerialize(), $options);
}
+ /**
+ * Convert the fluent instance to pretty print formatted JSON.
+ *
+ * @params int $options
+ *
+ * @return string
+ */
+ public function toPrettyJson(int $options = 0)
+ {
+ return $this->toJson(JSON_PRETTY_PRINT | $options);
+ }
+
+ /**
+ * Determine if the fluent instance is empty.
+ */
+ public function isEmpty(): bool
+ {
+ return empty($this->attributes);
+ }
+
+ /**
+ * Determine if the fluent instance is not empty.
+ */
+ public function isNotEmpty(): bool
+ {
+ return ! $this->isEmpty();
+ }
+
/**
* Determine if the given offset exists.
*
@@ -174,6 +238,16 @@ public function offsetUnset($offset): void
unset($this->attributes[$offset]);
}
+ /**
+ * Get an iterator for the attributes.
+ *
+ * @return ArrayIterator
+ */
+ public function getIterator(): Traversable
+ {
+ return new ArrayIterator($this->attributes);
+ }
+
/**
* Handle dynamic calls to the fluent instance to set attributes.
*
@@ -183,7 +257,11 @@ public function offsetUnset($offset): void
*/
public function __call($method, $parameters)
{
- $this->attributes[$method] = count($parameters) > 0 ? reset($parameters) : true;
+ if (static::hasMacro($method)) {
+ return $this->macroCall($method, $parameters);
+ }
+
+ $this->attributes[$method] = count($parameters) > 0 ? array_first($parameters) : true;
return $this;
}