Skip to content

Commit 6d6c19b

Browse files
authored
Add InteractsWithData trait and enhance Fluent class (#7476)
1 parent deb522f commit 6d6c19b

File tree

5 files changed

+873
-3
lines changed

5 files changed

+873
-3
lines changed

src/Fluent.php

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
use ArrayAccess;
1616
use ArrayIterator;
1717
use Closure;
18+
use Hyperf\Collection\Arr;
1819
use Hyperf\Contract\Arrayable;
1920
use Hyperf\Contract\Jsonable;
2021
use Hyperf\Macroable\Macroable;
22+
use Hyperf\Support\Traits\InteractsWithData;
2123
use IteratorAggregate;
2224
use JsonSerializable;
2325
use Traversable;
2426

27+
use function Hyperf\Collection\data_get;
28+
use function Hyperf\Collection\data_set;
29+
2530
/**
2631
* Most of the methods in this file come from illuminate/support,
2732
* thanks Laravel Team provide such a useful class.
@@ -34,6 +39,7 @@
3439
*/
3540
class Fluent implements ArrayAccess, Arrayable, IteratorAggregate, Jsonable, JsonSerializable
3641
{
42+
use InteractsWithData;
3743
use Macroable{
3844
__call as macroCall;
3945
}
@@ -52,9 +58,7 @@ class Fluent implements ArrayAccess, Arrayable, IteratorAggregate, Jsonable, Jso
5258
*/
5359
public function __construct($attributes = [])
5460
{
55-
foreach ($attributes as $key => $value) {
56-
$this->attributes[$key] = $value;
57-
}
61+
$this->fill($attributes);
5862
}
5963

6064
/**
@@ -123,6 +127,29 @@ public function __toString(): string
123127
return $this->toJson();
124128
}
125129

130+
/**
131+
* Get all of the attributes from the fluent instance.
132+
*
133+
* @param null|array|mixed $keys
134+
* @return array
135+
*/
136+
public function all($keys = null)
137+
{
138+
$data = $this->data();
139+
140+
if (! $keys) {
141+
return $data;
142+
}
143+
144+
$results = [];
145+
146+
foreach (is_array($keys) ? $keys : func_get_args() as $key) {
147+
Arr::set($results, $key, Arr::get($data, $key));
148+
}
149+
150+
return $results;
151+
}
152+
126153
/**
127154
* Get an attribute from the fluent instance.
128155
*
@@ -133,6 +160,47 @@ public function __toString(): string
133160
* @return TGetDefault|TValue
134161
*/
135162
public function get($key, $default = null)
163+
{
164+
return data_get($this->attributes, $key, $default);
165+
}
166+
167+
/**
168+
* Set an attribute on the fluent instance using "dot" notation.
169+
*
170+
* @param TKey $key
171+
* @param TValue $value
172+
* @return $this
173+
*/
174+
public function set($key, $value)
175+
{
176+
data_set($this->attributes, $key, $value);
177+
178+
return $this;
179+
}
180+
181+
/**
182+
* Fill the fluent instance with an array of attributes.
183+
*
184+
* @param iterable<TKey, TValue> $attributes
185+
* @return $this
186+
*/
187+
public function fill($attributes)
188+
{
189+
foreach ($attributes as $key => $value) {
190+
$this->attributes[$key] = $value;
191+
}
192+
193+
return $this;
194+
}
195+
196+
/**
197+
* Get an attribute from the fluent instance.
198+
*
199+
* @param string $key
200+
* @param mixed $default
201+
* @return mixed
202+
*/
203+
public function value($key, $default = null)
136204
{
137205
if (array_key_exists($key, $this->attributes)) {
138206
return $this->attributes[$key];
@@ -141,6 +209,20 @@ public function get($key, $default = null)
141209
return value($default);
142210
}
143211

212+
/**
213+
* Get the value of the given key as a new Fluent instance.
214+
*
215+
* @param string $key
216+
* @param mixed $default
217+
* @return static
218+
*/
219+
public function scope($key, $default = null)
220+
{
221+
return new static(
222+
(array) $this->get($key, $default)
223+
);
224+
}
225+
144226
/**
145227
* Get the attributes from the fluent instance.
146228
*
@@ -253,4 +335,16 @@ public function getIterator(): Traversable
253335
{
254336
return new ArrayIterator($this->attributes);
255337
}
338+
339+
/**
340+
* Get data from the fluent instance.
341+
*
342+
* @param string $key
343+
* @param mixed $default
344+
* @return mixed
345+
*/
346+
protected function data($key = null, $default = null)
347+
{
348+
return $this->get($key, $default);
349+
}
256350
}

src/Functions.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Hyperf\Support;
1414

15+
use BackedEnum;
1516
use Carbon\Carbon;
1617
use Closure;
1718
use DateTimeZone;
@@ -21,6 +22,7 @@
2122
use Hyperf\Stringable\StrCache;
2223
use Hyperf\Support\Backoff\ArrayBackoff;
2324
use Throwable;
25+
use UnitEnum;
2426

2527
/**
2628
* Return the default value of the given value.
@@ -312,3 +314,25 @@ function today($tz = null)
312314
{
313315
return Carbon::today($tz);
314316
}
317+
318+
/**
319+
* Return a scalar value for the given value that might be an enum.
320+
*
321+
* @internal
322+
*
323+
* @template TValue
324+
* @template TDefault
325+
*
326+
* @param TValue $value
327+
* @param callable(TValue): TDefault|TDefault $default
328+
* @return ($value is empty ? TDefault : mixed)
329+
*/
330+
function enum_value($value, $default = null)
331+
{
332+
return match (true) {
333+
$value instanceof BackedEnum => $value->value,
334+
$value instanceof UnitEnum => $value->name,
335+
336+
default => $value ?? value($default),
337+
};
338+
}

0 commit comments

Comments
 (0)