Skip to content

Commit 8070171

Browse files
authored
Merge pull request #230 from nuernbergerA/fix-fluent
feat: clone the "working" `Fluent` class from laravel
2 parents 7bdb2c7 + 425070d commit 8070171

File tree

9 files changed

+241
-9
lines changed

9 files changed

+241
-9
lines changed

src/Html/Button.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Closure;
66
use Illuminate\Contracts\Support\Arrayable;
7-
use Illuminate\Support\Fluent;
87
use Illuminate\Support\Traits\Macroable;
98

109
class Button extends Fluent implements Arrayable

src/Html/Column.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Yajra\DataTables\Html;
44

55
use Illuminate\Support\Arr;
6-
use Illuminate\Support\Fluent;
76
use Illuminate\Support\Str;
87
use Yajra\DataTables\Html\Options\Plugins\SearchPanes;
98

src/Html/Editor/Editor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
namespace Yajra\DataTables\Html\Editor;
44

55
use Illuminate\Support\Arr;
6-
use Illuminate\Support\Fluent;
76
use Illuminate\Support\Str;
87
use Illuminate\Support\Traits\Macroable;
98
use Yajra\DataTables\Html\Editor\Fields\Field;
9+
use Yajra\DataTables\Html\Fluent;
1010
use Yajra\DataTables\Html\HasAuthorizations;
1111
use Yajra\DataTables\Utilities\Helper;
1212

src/Html/Editor/Fields/Field.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use Illuminate\Contracts\Support\Arrayable;
77
use Illuminate\Database\Eloquent\Builder;
88
use Illuminate\Database\Query\Builder as QueryBuilder;
9-
use Illuminate\Support\Fluent;
109
use Illuminate\Support\Str;
1110
use Illuminate\Support\Traits\Macroable;
11+
use Yajra\DataTables\Html\Fluent;
1212
use Yajra\DataTables\Html\HasAuthorizations;
1313

1414
/**

src/Html/Editor/FormOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Yajra\DataTables\Html\Editor;
44

5-
use Illuminate\Support\Fluent;
5+
use Yajra\DataTables\Html\Fluent;
66

77
/**
88
* @see https://editor.datatables.net/reference/type/form-options

src/Html/Fluent.php

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Html;
4+
5+
use ArrayAccess;
6+
use Illuminate\Contracts\Support\Arrayable;
7+
use Illuminate\Contracts\Support\Jsonable;
8+
use JsonSerializable;
9+
10+
/**
11+
* @template TKey of array-key
12+
* @template TValue
13+
*
14+
* @implements \Illuminate\Contracts\Support\Arrayable<TKey, TValue>
15+
* @implements \ArrayAccess<TKey, TValue>
16+
*/
17+
class Fluent implements Arrayable, ArrayAccess, Jsonable, JsonSerializable
18+
{
19+
/**
20+
* All of the attributes set on the fluent instance.
21+
*
22+
* @var array<TKey, TValue>
23+
*/
24+
protected $attributes = [];
25+
26+
/**
27+
* Create a new fluent instance.
28+
*
29+
* @param iterable<TKey, TValue> $attributes
30+
* @return void
31+
*/
32+
public function __construct($attributes = [])
33+
{
34+
foreach ($attributes as $key => $value) {
35+
$this->attributes[$key] = $value;
36+
}
37+
}
38+
39+
/**
40+
* Get an attribute from the fluent instance using "dot" notation.
41+
*
42+
* @template TGetDefault
43+
*
44+
* @param TKey $key
45+
* @param TGetDefault|(\Closure(): TGetDefault) $default
46+
* @return TValue|TGetDefault
47+
*/
48+
public function get($key, $default = null)
49+
{
50+
return data_get($this->attributes, $key, $default);
51+
}
52+
53+
/**
54+
* Get an attribute from the fluent instance.
55+
*
56+
* @param string $key
57+
* @param mixed $default
58+
* @return mixed
59+
*/
60+
public function value($key, $default = null)
61+
{
62+
if (array_key_exists($key, $this->attributes)) {
63+
return $this->attributes[$key];
64+
}
65+
66+
return value($default);
67+
}
68+
69+
/**
70+
* Get the value of the given key as a new Fluent instance.
71+
*
72+
* @param string $key
73+
* @param mixed $default
74+
* @return static
75+
*/
76+
public function scope($key, $default = null)
77+
{
78+
return new static(
79+
(array) $this->get($key, $default)
80+
);
81+
}
82+
83+
/**
84+
* Get the attributes from the fluent instance.
85+
*
86+
* @return array<TKey, TValue>
87+
*/
88+
public function getAttributes()
89+
{
90+
return $this->attributes;
91+
}
92+
93+
/**
94+
* Convert the fluent instance to an array.
95+
*
96+
* @return array<TKey, TValue>
97+
*/
98+
public function toArray()
99+
{
100+
return $this->attributes;
101+
}
102+
103+
/**
104+
* Convert the fluent instance to a Collection.
105+
*
106+
* @param string|null $key
107+
* @return \Illuminate\Support\Collection
108+
*/
109+
public function collect($key = null)
110+
{
111+
return new Collection($this->get($key));
112+
}
113+
114+
/**
115+
* Convert the object into something JSON serializable.
116+
*
117+
* @return array<TKey, TValue>
118+
*/
119+
public function jsonSerialize(): array
120+
{
121+
return $this->toArray();
122+
}
123+
124+
/**
125+
* Convert the fluent instance to JSON.
126+
*
127+
* @param int $options
128+
* @return string
129+
*/
130+
public function toJson($options = 0)
131+
{
132+
return json_encode($this->jsonSerialize(), $options);
133+
}
134+
135+
/**
136+
* Determine if the given offset exists.
137+
*
138+
* @param TKey $offset
139+
* @return bool
140+
*/
141+
public function offsetExists($offset): bool
142+
{
143+
return isset($this->attributes[$offset]);
144+
}
145+
146+
/**
147+
* Get the value for a given offset.
148+
*
149+
* @param TKey $offset
150+
* @return TValue|null
151+
*/
152+
public function offsetGet($offset): mixed
153+
{
154+
return $this->value($offset);
155+
}
156+
157+
/**
158+
* Set the value at the given offset.
159+
*
160+
* @param TKey $offset
161+
* @param TValue $value
162+
* @return void
163+
*/
164+
public function offsetSet($offset, $value): void
165+
{
166+
$this->attributes[$offset] = $value;
167+
}
168+
169+
/**
170+
* Unset the value at the given offset.
171+
*
172+
* @param TKey $offset
173+
* @return void
174+
*/
175+
public function offsetUnset($offset): void
176+
{
177+
unset($this->attributes[$offset]);
178+
}
179+
180+
/**
181+
* Handle dynamic calls to the fluent instance to set attributes.
182+
*
183+
* @param TKey $method
184+
* @param array{0: ?TValue} $parameters
185+
* @return $this
186+
*/
187+
public function __call($method, $parameters)
188+
{
189+
$this->attributes[$method] = count($parameters) > 0 ? reset($parameters) : true;
190+
191+
return $this;
192+
}
193+
194+
/**
195+
* Dynamically retrieve the value of an attribute.
196+
*
197+
* @param TKey $key
198+
* @return TValue|null
199+
*/
200+
public function __get($key)
201+
{
202+
return $this->value($key);
203+
}
204+
205+
/**
206+
* Dynamically set the value of an attribute.
207+
*
208+
* @param TKey $key
209+
* @param TValue $value
210+
* @return void
211+
*/
212+
public function __set($key, $value)
213+
{
214+
$this->offsetSet($key, $value);
215+
}
216+
217+
/**
218+
* Dynamically check if an attribute is set.
219+
*
220+
* @param TKey $key
221+
* @return bool
222+
*/
223+
public function __isset($key)
224+
{
225+
return $this->offsetExists($key);
226+
}
227+
228+
/**
229+
* Dynamically unset an attribute.
230+
*
231+
* @param TKey $key
232+
* @return void
233+
*/
234+
public function __unset($key)
235+
{
236+
$this->offsetUnset($key);
237+
}
238+
}

src/Html/Layout.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Illuminate\Contracts\Support\Renderable;
88
use Illuminate\Support\Facades\Blade;
9-
use Illuminate\Support\Fluent;
109
use Illuminate\Support\Traits\Macroable;
1110
use Illuminate\View\Component;
1211
use InvalidArgumentException;

src/Html/Parameters.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Yajra\DataTables\Html;
44

5-
use Illuminate\Support\Fluent;
6-
75
class Parameters extends Fluent
86
{
97
/**

src/Html/SearchPane.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Closure;
66
use Illuminate\Contracts\Support\Arrayable;
77
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
8-
use Illuminate\Support\Fluent;
98
use Yajra\DataTables\Html\Editor\Fields\Options;
109

1110
class SearchPane extends Fluent

0 commit comments

Comments
 (0)