Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 83 additions & 5 deletions src/Html/Fluent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,8 +19,12 @@
* @implements \Illuminate\Contracts\Support\Arrayable<TKey, TValue>
* @implements \ArrayAccess<TKey, TValue>
*/
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.
*
Expand All @@ -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);
}

/**
Expand All @@ -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<TKey, TValue> $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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -174,6 +238,16 @@ public function offsetUnset($offset): void
unset($this->attributes[$offset]);
}

/**
* Get an iterator for the attributes.
*
* @return ArrayIterator<TKey, TValue>
*/
public function getIterator(): Traversable
{
return new ArrayIterator($this->attributes);
}

/**
* Handle dynamic calls to the fluent instance to set attributes.
*
Expand All @@ -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;
}
Expand Down
Loading