Skip to content

Commit bcb334e

Browse files
committed
Merge branch 'master' into 3.2-merge
# Conflicts: # .github/workflows/test.yml # src/collection/tests/ArrTest.php # src/pipeline/composer.json # src/pipeline/tests/PipelineTest.php # src/support/src/Fluent.php # src/support/src/Traits/InteractsWithData.php # src/support/tests/FluentTest.php
2 parents ce1f642 + 6d6c19b commit bcb334e

File tree

5 files changed

+356
-13
lines changed

5 files changed

+356
-13
lines changed

src/Fluent.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Hyperf\Contract\Arrayable;
2020
use Hyperf\Contract\Jsonable;
2121
use Hyperf\Macroable\Macroable;
22+
use Hyperf\Support\Traits\InteractsWithData;
2223
use IteratorAggregate;
2324
use JsonSerializable;
2425
use Traversable;
@@ -38,8 +39,8 @@
3839
*/
3940
class Fluent implements ArrayAccess, Arrayable, IteratorAggregate, Jsonable, JsonSerializable
4041
{
41-
use Traits\InteractsWithData;
42-
use Macroable {
42+
use InteractsWithData;
43+
use Macroable{
4344
__call as macroCall;
4445
}
4546

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+
}

src/Traits/InteractsWithData.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
use Hyperf\Stringable\Str;
2020
use Hyperf\Stringable\Stringable;
2121
use stdClass;
22+
use UnitEnum;
2223

2324
use function Hyperf\Collection\data_get;
25+
use function Hyperf\Support\enum_value;
26+
use function Hyperf\Support\value;
2427

2528
trait InteractsWithData
2629
{
@@ -207,7 +210,7 @@ public function whenMissing($key, callable $callback, ?callable $default = null)
207210
}
208211

209212
/**
210-
* Retrieve data from the instnce as a Stringable instance.
213+
* Retrieve data from the instance as a Stringable instance.
211214
*
212215
* @param string $key
213216
* @param mixed $default
@@ -273,13 +276,14 @@ public function float($key, $default = 0.0)
273276
*
274277
* @param string $key
275278
* @param null|string $format
276-
* @param null|string $tz
279+
* @param null|string|UnitEnum $tz
277280
* @return null|Carbon
278281
*
279282
* @throws InvalidFormatException
280283
*/
281284
public function date($key, $format = null, $tz = null)
282285
{
286+
$tz = enum_value($tz);
283287
if ($this->isNotFilled($key)) {
284288
return null;
285289
}
@@ -298,15 +302,16 @@ public function date($key, $format = null, $tz = null)
298302
*
299303
* @param string $key
300304
* @param class-string<TEnum> $enumClass
305+
* @param null|mixed $default
301306
* @return null|TEnum
302307
*/
303-
public function enum($key, $enumClass)
308+
public function enum($key, $enumClass, $default = null)
304309
{
305310
if ($this->isNotFilled($key) || ! $this->isBackedEnum($enumClass)) {
306-
return null;
311+
return value($default);
307312
}
308313

309-
return $enumClass::tryFrom($this->data($key));
314+
return $enumClass::tryFrom($this->data($key)) ?? value($default);
310315
}
311316

312317
/**
@@ -324,9 +329,10 @@ public function enums($key, $enumClass)
324329
return [];
325330
}
326331

327-
return $this->collect($key)->map(function ($value) use ($enumClass) {
328-
return $enumClass::tryFrom($value);
329-
})->filter()->all();
332+
return $this->collect($key)
333+
->map(fn ($value) => $enumClass::tryFrom($value))
334+
->filter()
335+
->all();
330336
}
331337

332338
/**

0 commit comments

Comments
 (0)