PHP code example of webfox / laravel-backed-enums
1. Go to this page and download the library: Download webfox/laravel-backed-enums library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
webfox / laravel-backed-enums example snippets
use Webfox\LaravelBackedEnums\BackedEnum;
use Webfox\LaravelBackedEnums\IsBackedEnum;
enum VolumeUnitEnum: string implements BackedEnum
{
use IsBackedEnum;
case MILLIGRAMS = "milligrams";
case GRAMS = "grams";
case KILOGRAMS = "kilograms";
case TONNE = "tonne";
}
// resources/lang/en/enums.php
return [
VolumeUnitEnum::class => [
VolumeUnitEnum::MILLIGRAMS->value => "mg",
VolumeUnitEnum::GRAMS->value => "g",
VolumeUnitEnum::KILOGRAMS->value => "kg",
VolumeUnitEnum::TONNE->value => "t"
]
];
VolumeUnitEnum::MILLIGRAMS->label(); // "mg"
VolumeUnitEnum::labelFor(VolumeUnitEnum::TONNE); // "t"
// in blade
{{ VolumeUnitEnum::KILOGRAMS }} // "kg"
public function withMeta(): array
{
return match ($this) {
self::MILLIGRAMS => [
'background_color' => 'bg-green-100',
'text_color' => 'text-green-800',
],
self::GRAMS => [
'background_color' => 'bg-red-100',
'text_color' => 'text-red-800',
],
self::KILOGRAMS, self::TONNE => [
'background_color' => 'bg-gray-100',
'text_color' => 'text-gray-800',
],
default => [
'background_color' => 'bg-blue-100',
'text_color' => 'text-blue-800',
],
};
}
VolumeUnitEnum::options();
[
[
'name' => 'MILLIGRAMS',
'value' => 'milligrams',
'label' => 'mg',
'meta' => [
'background_color' => 'bg-green-100',
'text_color' => 'text-green-800',
],
],
[
'name' => 'GRAMS',
'value' => 'grams',
'label' => 'g',
'meta' => [
'background_color' => 'bg-red-100',
'text_color' => 'text-red-800',
],
...
]
]
VolumeUnitEnum::names();
[
'MILLIGRAMS',
'GRAMS',
'KILOGRAMS',
'TONNE',
]
VolumeUnitEnum::values();
[
'milligrams',
'grams',
'killograms',
'tonne',
]
VolumeUnitEnum::labels();
[
'mg',
'g',
'kg',
't',
]
VolumeUnitEnum::map();
[
'MILLIGRAMS' => 'mg',
'GRAMS' => 'g',
'KILOGRAMS' => 'kg',
'TONNE' => 't',
]
VolumeUnitEnum::MILLIGRAMS->toArray();
[
'name' => 'MILLIGRAMS',
'value' => 'milligrams',
'label' => 'mg',
'meta' => [
'color' => 'bg-green-100',
'text_color' => 'text-green-800',
],
]
VolumeUnitEnum::MILLIGRAMS->toHtml();
mg
VolumeUnitEnum::MILLIGRAMS->is(VolumeUnitEnum::MILLIGRAMS); //true
VolumeUnitEnum::MILLIGRAMS->is('MILLIGRAMS'); //true
VolumeUnitEnum::MILLIGRAMS->is('invalid'); //exception
VolumeUnitEnum::MILLIGRAMS->isNot(VolumeUnitEnum::GRAMS); //true
VolumeUnitEnum::MILLIGRAMS->isNot('GRAMS'); //true
VolumeUnitEnum::MILLIGRAMS->isNot('invalid'); //exception
VolumeUnitEnum::MILLIGRAMS->isAny(['GRAMS', VolumeUnitEnum::TONNE]); // false
VolumeUnitEnum::MILLIGRAMS->isAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::MILLIGRAMS]); // true
VolumeUnitEnum::MILLIGRAMS->isNotAny(['GRAMS', VolumeUnitEnum::TONNE]); // true
VolumeUnitEnum::MILLIGRAMS->isNotAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::MILLIGRAMS]); // false
Bash
php artisan make:enum OrderStatus
public function rules(): array
{
return [
'volume_unit' => [VolumeUnitEnum::rule()],
];
}