8000 [11.x] Improves `Collection` support for enums using `firstWhere()` a… · tibbsa/laravel-framework@b8bb5e9 · GitHub
[go: up one dir, main page]

Skip to content

Commit b8bb5e9

Browse files
[11.x] Improves Collection support for enums using firstWhere() and value() (laravel#53777)
* [11.x] Improves `Collection` support for enums using `firstWhere()` and `value()` Fixes laravel#53676 Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> --------- Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent 8b889c7 commit b8bb5e9

File tree

6 files changed

+68
-26
lines changed

6 files changed

+68
-26
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
},
133133
"autoload": {
134134
"files": [
135+
"src/Illuminate/Collections/functions.php",
135136
"src/Illuminate/Collections/helpers.php",
136137
"src/Illuminate/Events/functions.php",
137138
"src/Illuminate/Filesystem/functions.php",

src/Illuminate/Collections/Traits/EnumeratesValues.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use UnitEnum;
2020
use WeakMap;
2121

22+
use function Illuminate\Support\enum_value;
23+
2224
/**
2325
* @template TKey of array-key
2426
*
@@ -1092,10 +1094,15 @@ protected function operatorForWhere($key, $operator = null, $value = null)
10921094
}
10931095

10941096
return function ($item) use ($key, $operator, $value) {
1095-
$retrieved = data_get($item, $key);
1097+
$retrieved = enum_value(data_get($item, $key));
1098+
$value = enum_value($value);
10961099

10971100
$strings = array_filter([$retrieved, $value], function ($value) {
1098-
return is_string($value) || (is_object($value) && method_exists($value, '__toString'));
1101+
return match (true) {
1102+
is_string($value) => true,
1103+
$value instanceof \Stringable => true,
1104+
default => false,
1105+
};
10991106
});
11001107

11011108
if (count($strings) < 2 && count(array_filter([$retrieved, $value], 'is_object')) == 1) {

src/Illuminate/Collections/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"Illuminate\\Support\\": ""
2525
},
2626
"files": [
27+
"functions.php",
2728
"helpers.php"
2829
]
2930
},
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Illuminate\Support;
4+
5+
if (! function_exists('Illuminate\Support\enum_value')) {
6+
/**
7+
* Return a scalar value for the given value that might be an enum.
8+
*
9+
* @internal
10+
*
11+
* @template TValue
12+
* @template TDefault
13+
*
14+
* @param TValue $value
15+
* @param TDefault|callable(TValue): TDefault $default
16+
* @return ($value is empty ? TDefault : mixed)
17+
*/
18+
function enum_value($value, $default = null)
19+
{
20+
return transform($value, fn ($value) => match (true) {
21+
$value instanceof \BackedEnum => $value->value,
22+
$value instanceof \UnitEnum => $value->name,
23+
24+
default => $value,
25+
}, $default ?? $value);
26+
}
27+
}

src/Illuminate/Support/functions.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,6 @@ function defer(?callable $callback = null, ?string $name = null, bool $always =
2828
}
2929
}
3030

31-
if (! function_exists('Illuminate\Support\enum_value')) {
32-
/**
33-
* Return a scalar value for the given value that might be an enum.
34-
*
35-
* @internal
36-
*
37-
* @template TValue
38-
* @template TDefault
39-
*
40-
* @param TValue $value
41-
* @param TDefault|callable(TValue): TDefault $default
42-
* @return ($value is empty ? TDefault : mixed)
43-
*/
44-
function enum_value($value, $default = null)
45-
{
46-
return transform($value, fn ($value) => match (true) {
47-
$value instanceof \BackedEnum => $value->value,
48-
$value instanceof \UnitEnum => $value->name,
49-
50-
default => $value,
51-
}, $default ?? $value);
52-
}
53-
}
54-
5531
if (! function_exists('Illuminate\Support\php_binary')) {
5632
/**
5733
* Determine the PHP Binary.

tests/Support/SupportCollectionTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,20 @@ public function testFirstWhere($collection)
257257
$this->assertNull($data->firstWhere(fn ($value) => ($value['nonexistent'] ?? null) === 'key'));
258258
}
259259

260+
#[DataProvider('collectionClassProvider')]
261+
public function testFirstWhereUsingEnum($collection)
262+
{
263+
$data = new $collection([
264+
['id' => 1, 'name' => StaffEnum::Taylor],
265+
['id' => 2, 'name' => StaffEnum::Joe],
266+
['id' => 3, 'name' => StaffEnum::James],
267+
]);
268+
269+
$this->assertSame(1, $data->firstWhere('name', 'Taylor')['id']);
270+
$this->assertSame(2, $data->firstWhere('name', StaffEnum::Joe)['id']);
271+
$this->assertSame(3, $data->firstWhere('name', StaffEnum::James)['id']);
272+
}
273+
260274
#[DataProvider('collectionClassProvider')]
261275
public function testLastReturnsLastItemInCollection($collection)
262276
{
@@ -1130,6 +1144,15 @@ public function testValue($collection)
11301144
$this->assertEquals('bar', $c->where('id', 2)->value('pivot.value'));
11311145
}
11321146

1147+
#[DataProvider('collectionClassProvider')]
1148+
public function testValueUsingEnum($collection)
1149+
{
1150+
$c = new $collection([['id' => 1, 'name' => StaffEnum::Taylor], ['id' => 2, 'name' => StaffEnum::Joe]]);
1151+
1152+
$this->assertSame(StaffEnum::Taylor, $c->value('name'));
1153+
$this->assertEquals(StaffEnum::Joe, $c->where('id', 2)->value('name'));
1154+
}
1155+
11331156
#[DataProvider('collectionClassProvider')]
11341157
public function testBetween($collection)
11351158
{
@@ -5737,3 +5760,10 @@ class TestCollectionSubclass extends Collection
57375760
{
57385761
//
57395762
}
5763+
5764+
enum StaffEnum
5765+
{
5766+
case Taylor;
5767+
case Joe;
5768+
case James;
5769+
}

0 commit comments

Comments
 (0)
0