Skip to content

Commit a236107

Browse files
crynoboneStyleCIBottaylorotwell
authored
[11.x] Add Illuminate\Support\enum_value to resolve BackedEnum or UnitEnum to scalar (#53096)
* [11.x] Add `Illuminate\Support\mutate` to resolve `BackedEnum` or `UnitEnum` to scalar. Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip * Apply fixes from StyleCI * rename to scalar value * rename again --------- Signed-off-by: Mior Muhammad Zaki <[email protected]> Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 05b184c commit a236107

File tree

21 files changed

+133
-103
lines changed

21 files changed

+133
-103
lines changed

src/Illuminate/Auth/Access/Gate.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Illuminate\Auth\Access;
44

5-
use BackedEnum;
65
use Closure;
76
use Exception;
87
use Illuminate\Auth\Access\Events\GateEvaluated;
@@ -16,6 +15,8 @@
1615
use ReflectionClass;
1716
use ReflectionFunction;
1817

18+
use function Illuminate\Support\enum_value;
19+
1920
class Gate implements GateContract
2021
{
2122
use HandlesAuthorization;
@@ -200,7 +201,7 @@ protected function authorizeOnDemand($condition, $message, $code, $allowWhenResp
200201
*/
201202
public function define($ability, $callback)
202203
{
203-
$ability = $ability instanceof BackedEnum ? $ability->value : $ability;
204+
$ability = enum_value($ability);
204205

205206
if (is_array($callback) && isset($callback[0]) && is_string($callback[0])) {
206207
$callback = $callback[0].'@'.$callback[1];
@@ -405,10 +406,8 @@ public function authorize($ability, $arguments = [])
405406
*/
406407
public function inspect($ability, $arguments = [])
407408
{
408-
$ability = $ability instanceof BackedEnum ? $ability->value : $ability;
409-
410409
try {
411-
$result = $this->raw($ability, $arguments);
410+
$result = $this->raw(enum_value($ability), $arguments);
412411

413412
if ($result instanceof Response) {
414413
return $result;

src/Illuminate/Auth/Middleware/Authorize.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
namespace Illuminate\Auth\Middleware;
44

5-
use BackedEnum;
65
use Closure;
76
use Illuminate\Contracts\Auth\Access\Gate;
87
use Illuminate\Database\Eloquent\Model;
98

9+
use function Illuminate\Support\enum_value;
10+
1011
class Authorize
1112
{
1213
/**
@@ -36,9 +37,7 @@ public function __construct(Gate $gate)
3637
*/
3738
public static function using($ability, ...$models)
3839
{
39-
$ability = $ability instanceof BackedEnum ? $ability->value : $ability;
40-
41-
return static::class.':'.implode(',', [$ability, ...$models]);
40+
return static::class.':'.implode(',', [enum_value($ability), ...$models]);
4241
}
4342

4443
/**

src/Illuminate/Bus/Queueable.php

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

33
namespace Illuminate\Bus;
44

5-
use BackedEnum;
65
use Closure;
76
use Illuminate\Queue\CallQueuedClosure;
87
use Illuminate\Support\Arr;
98
use PHPUnit\Framework\Assert as PHPUnit;
109
use RuntimeException;
1110

11+
use function Illuminate\Support\enum_value;
12+
1213
trait Queueable
1314
{
1415
/**
@@ -82,9 +83,7 @@ trait Queueable
8283
*/
8384
public function onConnection($connection)
8485
{
85-
$this->connection = $connection instanceof BackedEnum
86-
? $connection->value
87-
: $connection;
86+
$this->connection = enum_value($connection);
8887

8988
return $this;
9089
}
@@ -97,9 +96,7 @@ public function onConnection($connection)
9796
*/
9897
public function onQueue($queue)
9998
{
100-
$this->queue = $queue instanceof BackedEnum
101-
? $queue->value
102-
: $queue;
99+
$this->queue = enum_value($queue);
103100

104101
return $this;
105102
}
@@ -112,9 +109,7 @@ public function onQueue($queue)
112109
*/
113110
public function allOnConnection($connection)
114111
{
115-
$resolvedConnection = $connection instanceof BackedEnum
116-
? $connection->value
117-
: $connection;
112+
$resolvedConnection = enum_value($connection);
118113

119114
$this->chainConnection = $resolvedConnection;
120115
$this->connection = $resolvedConnection;
@@ -130,9 +125,7 @@ public function allOnConnection($connection)
130125
*/
131126
public function allOnQueue($queue)
132127
{
133-
$resolvedQueue = $queue instanceof BackedEnum
134-
? $queue->value
135-
: $queue;
128+
$resolvedQueue = enum_value($queue);
136129

137130
$this->chainQueue = $resolvedQueue;
138131
$this->queue = $resolvedQueue;

src/Illuminate/Cache/RateLimiter.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Illuminate\Cache;
44

5-
use BackedEnum;
65
use Closure;
76
use Illuminate\Contracts\Cache\Repository as Cache;
87
use Illuminate\Support\InteractsWithTime;
9-
use UnitEnum;
8+
9+
use function Illuminate\Support\enum_value;
1010

1111
class RateLimiter
1212
{
@@ -263,10 +263,6 @@ public function cleanRateLimiterKey($key)
263263
*/
264264
private function resolveLimiterName($name): string
265265
{
266-
return match (true) {
267-
$name instanceof BackedEnum => $name->value,
268-
$name instanceof UnitEnum => $name->name,
269-
default => (string) $name,
270-
};
266+
return (string) enum_value($name);
271267
}
272268
}

src/Illuminate/Database/Console/ShowModelCommand.php

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

33
namespace Illuminate\Database\Console;
44

5-
use BackedEnum;
65
use Illuminate\Contracts\Container\BindingResolutionException;
76
use Illuminate\Database\Eloquent\Model;
87
use Illuminate\Database\Eloquent\Relations\Relation;
@@ -14,7 +13,8 @@
1413
use SplFileObject;
1514
use Symfony\Component\Console\Attribute\AsCommand;
1615
use Symfony\Component\Console\Output\OutputInterface;
17-
use UnitEnum;
16+
17+
use function Illuminate\Support\enum_value;
1818

1919
#[AsCommand(name: 'model:show')]
2020
class ShowModelCommand extends DatabaseInspectionCommand
@@ -474,11 +474,7 @@ protected function getColumnDefault($column, $model)
474474
{
475475
$attributeDefault = $model->getAttributes()[$column['name']] ?? null;
476476

477-
return match (true) {
478-
$attributeDefault instanceof BackedEnum => $attributeDefault->value,
479-
$attributeDefault instanceof UnitEnum => $attributeDefault->name,
480-
default => $attributeDefault ?? $column['default'],
481-
};
477+
return enum_value($attributeDefault, $column['default']);
482478
}
483479

484480
/**

src/Illuminate/Database/Eloquent/Casts/AsEnumArrayObject.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
88
use Illuminate\Support\Collection;
99

10+
use function Illuminate\Support\enum_value;
11+
1012
class AsEnumArrayObject implements Castable
1113
{
1214
/**
@@ -77,7 +79,7 @@ protected function getStorableEnumValue($enum)
7779
return $enum;
7880
}
7981

80-
return $enum instanceof BackedEnum ? $enum->value : $enum->name;
82+
return enum_value($enum);
8183
}
8284
};
8385
}

src/Illuminate/Database/Eloquent/Casts/AsEnumCollection.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
88
use Illuminate\Support\Collection;
99

10+
use function Illuminate\Support\enum_value;
11+
1012
class AsEnumCollection implements Castable
1113
{
1214
/**
@@ -73,7 +75,7 @@ protected function getStorableEnumValue($enum)
7375
return $enum;
7476
}
7577

76-
return $enum instanceof BackedEnum ? $enum->value : $enum->name;
78+
return enum_value($enum);
7779
}
7880
};
7981
}

src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
use RuntimeException;
4343
use ValueError;
4444

45+
use function Illuminate\Support\enum_value;
46+
4547
trait HasAttributes
4648
{
4749
/**
@@ -1253,9 +1255,7 @@ protected function getStorableEnumValue($expectedEnum, $value)
12531255
throw new ValueError(sprintf('Value [%s] is not of the expected enum type [%s].', var_export($value, true), $expectedEnum));
12541256
}
12551257

1256-
return $value instanceof BackedEnum
1257-
? $value->value
1258-
: $value->name;
1258+
return enum_value($value);
12591259
}
12601260

12611261
/**

src/Illuminate/Database/Eloquent/Relations/BelongsTo.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace Illuminate\Database\Eloquent\Relations;
44

5-
use BackedEnum;
65
use Illuminate\Database\Eloquent\Builder;
76
use Illuminate\Database\Eloquent\Collection;
87
use Illuminate\Database\Eloquent\Model;
98
use Illuminate\Database\Eloquent\Relations\Concerns\ComparesRelatedModels;
109
use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary;
1110
use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels;
1211

12+
use function Illuminate\Support\enum_value;
13+
1314
/**
1415
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
1516
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
@@ -367,7 +368,7 @@ protected function getForeignKeyFrom(Model $model)
367368
{
368369
$foreignKey = $model->{$this->foreignKey};
369370

370-
return $foreignKey instanceof BackedEnum ? $foreignKey->value : $foreignKey;
371+
return enum_value($foreignKey);
371372
}
372373

373374
/**

src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithDictionary.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Illuminate\Database\Eloquent\Relations\Concerns;
44

5-
use BackedEnum;
65
use InvalidArgumentException;
76
use UnitEnum;
87

8+
use function Illuminate\Support\enum_value;
9+
910
trait InteractsWithDictionary
1011
{
1112
/**
@@ -24,7 +25,7 @@ protected function getDictionaryKey($attribute)
2425
}
2526

2627
if ($attribute instanceof UnitEnum) {
27-
return $attribute instanceof BackedEnum ? $attribute->value : $attribute->name;
28+
return enum_value($attribute);
2829
}
2930

3031
throw new InvalidArgumentException('Model attribute value is an object but does not have a __toString method.');

0 commit comments

Comments
 (0)