You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can use the `HasEnums` trait to enable automatic casting of properties to enums. When used, it also automatically checks if a value set to an enum property is valid, if not it throws an `UndefinedMemberException`.
265
+
## Custom Cast
275
266
276
-
To enable this functionality you will have to use the trait and set the enums property on your model.
267
+
You can use the `Enum` custom cast to enable automatic casting of properties to enums. When used, it will set the property to the default members value when set to an invalid value, same goes when the initial value is invalid when getting it, it will return the default member.
277
268
278
269
```php
279
270
<?php
@@ -282,19 +273,17 @@ namespace App\Models;
282
273
283
274
use App\Enums\UserType;
284
275
use Illuminate\Database\Eloquent\Model;
285
-
use Sourceboat\Enumeration\Traits\HasEnums;
276
+
use Sourceboat\Enumeration\Casts\Enum;
286
277
287
278
class User extends Model
288
279
{
289
-
use HasEnums;
290
-
291
-
protected $enums = [
292
-
'type' => UserType::class,
280
+
protected $casts = [
281
+
'type' => Enums::class . ':' . UserType::class,
293
282
];
294
283
}
295
284
```
296
285
297
-
The enums property is a simple mapping of model attributes to enum classes. For cases where you want an attribute to be nullable, but don't want to have a `null`-value member in your enum, you can specify this explicitly:
286
+
By default the custom cast treats the property as nullable, meaning that the property can be set to null and also return it, instead of an enum member, this can the disabled on an per property basis:
'type' => Enums::class . ':' . UserType::class . ',0', // appending the 0 means it is not nullable,
301
+
]; // this seems counter intuitive, but thats the way it is recognized as `false`,
302
+
} // as `false` is somehow evaluated as `true`.
316
303
```
317
304
318
305
If the casted attribute is not set to be nullable and/or has a value not represented by the enum, you will get the default member of the enum when accessing the attribute.
@@ -329,7 +316,7 @@ echo $type->value; // "0"
329
316
330
317
## Weighted Enums
331
318
332
-
It is Possible to define weights for enum members. the standard way is to define the weights a config file and them implement the `Weighted`-interface with the `IsWeighted`-trait and define the path to your config. The weigths can be defined as integer or float values.
319
+
It is Possible to define weights for enum members. the standard way is to define the weights a config file and them implement the `Weighted`-interface with the `IsWeighted`-trait and define the path to your config. The weights can be defined as integer or float values.
333
320
334
321
```php
335
322
// The Enum
@@ -355,8 +342,6 @@ final class UserType extends Enumeration implements Weighted
355
342
const Moderator = 1;
356
343
const Subscriber = 2;
357
344
const SuperAdministrator = 3;
358
-
359
-
protected static $weightOptionsKey = 'test'; // optional, by standard its `sprintf('enums.%s.weights', static::class)`
final class UserType extends Enumeration implements Configurable
412
+
{
413
+
use IsConfigurable;
414
+
415
+
const Administrator = 0;
416
+
const Moderator = 1;
417
+
const Subscriber = 2;
418
+
const SuperAdministrator = 3;
419
+
}
420
+
```
421
+
422
+
```php
423
+
// enums.php
424
+
<?php
425
+
use App\Enums\UserType;
426
+
427
+
return [
428
+
UserType::class => [
429
+
'permissions' => [
430
+
UserType::Administrator => [
431
+
'user.foreign.edit',
432
+
'user.foreign.delete',
433
+
],
434
+
UserType::Subscriber => [
435
+
'user.self.edit',
436
+
'user.self.delete',
437
+
],
438
+
]
439
+
]
440
+
]
441
+
```
442
+
443
+
then you can access these values by:
444
+
445
+
```php
446
+
UserType::Subscriber()->config('permissions'); // which return the given array.
447
+
UserType::Moderator()->config('permissions', ['thread.foreign.archive']); // you can also define a default value.
448
+
```
449
+
450
+
402
451
## License information
403
452
404
453
Much of the functionality in this Package is inspired by [bensampo/laravel-enum](https://github.com/bensampo/laravel-enum) and some code has been taken from it and modified, for example the `MakeEnumCommand.php`, the `EnumServiceProvider.php` and this readme.
0 commit comments