1. Go to this page and download the library: Download bensampo/laravel-enum 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/ */
bensampo / laravel-enum example snippets
php artisan make:enum UserType
declare(strict_types=1);
namespace App\Enums;
use BenSampo\Enum\Enum;
final class UserType extends Enum
{
const Administrator = 0;
const Moderator = 1;
const Subscriber = 2;
const SuperAdministrator = 3;
}
UserType::Administrator // Has a value of 0
// Standard new PHP class, passing the desired enum value as a parameter
$enumInstance = new UserType(UserType::Administrator);
// Same as the constructor, instantiate by value
$enumInstance = UserType::fromValue(UserType::Administrator);
// Use an enum key instead of its value
$enumInstance = UserType::fromKey('Administrator');
// Statically calling the key name as a method, utilizing __callStatic magic
$enumInstance = UserType::Administrator();
// Attempt to instantiate a new Enum using the given key or value. Returns null if the Enum cannot be instantiated.
$enumInstance = UserType::coerce($someValue);
final class UserPermissions extends FlaggedEnum
{
const ReadComments = 1 << 0;
const WriteComments = 1 << 1;
const EditComments = 1 << 2;
const DeleteComments = 1 << 3;
// The next one would be `1 << 4` and so on...
}
final class UserPermissions extends FlaggedEnum
{
const ReadComments = 1 << 0;
const WriteComments = 1 << 1;
const EditComments = 1 << 2;
const DeleteComments = 1 << 3;
// Shortcuts
const Member = self::ReadComments | self::WriteComments; // Read and write.
const Moderator = self::Member | self::EditComments; // All the permissions a Member has, plus Edit.
const Admin = self::Moderator | self::DeleteComments; // All the permissions a Moderator has, plus Delete.
}
// Standard new PHP class, passing the desired enum values as an array of values or array of enum instances
$permissions = new UserPermissions([UserPermissions::ReadComments, UserPermissions::EditComments]);
$permissions = new UserPermissions([UserPermissions::ReadComments(), UserPermissions::EditComments()]);
// Static flags method, again passing the desired enum values as an array of values or array of enum instances
$permissions = UserPermissions::flags([UserPermissions::ReadComments, UserPermissions::EditComments]);
$permissions = UserPermissions::flags([UserPermissions::ReadComments(), UserPermissions::EditComments()]);
use BenSampo\Enum\Tests\Enums\UserType;
use Illuminate\Database\Eloquent\Model;
class Example extends Model
{
protected $casts = [
'random_flag' => 'boolean', // Example standard laravel cast
'user_type' => UserType::class, // Example enum cast
];
}
$example = Example::first();
$example->user_type // Instance of UserType
$example = Example::first();
// Set using enum value
$example->user_type = UserType::Moderator;
// Set using enum instance
$example->user_type = UserType::Moderator();
// Example Enum
final class UserType extends Enum
{
const ADMINISTRATOR = 0;
const MODERATOR = 1;
}
$instance = UserType::Moderator();
// Default
public function toArray()
{
return $this->value;
}
// Returns int(1)
// Return all properties
public function toArray()
{
return $this;
}
// Returns an array of all the properties
// array(3) {
// ["value"]=>
// int(1)"
// ["key"]=>
// string(9) "MODERATOR"
// ["description"]=>
// string(9) "Moderator"
// }
final class UserType extends Enum
{
const Administrator = 0;
const Moderator = 1;
public static function parseDatabase($value)
{
return (int) $value;
}
}
use App\Enums\UserType;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table): void {
$table->bigIncrements('id');
$table->timestamps();
$table->string('type')
->default(UserType::Moderator);
});
}
}
use App\Enums\UserType;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table): void {
$table->bigIncrements('id');
$table->timestamps();
$table->enum('type', UserType::getValues())
->default(UserType::Moderator);
});
}
}
use BenSampo\Enum\Rules\EnumValue;
public function store(Request $request)
{
$this->validate($request, [
'user_type' => ['
new EnumValue(UserType::class, false) // Turn off strict type checking.
use BenSampo\Enum\Rules\EnumKey;
public function store(Request $request)
{
$this->validate($request, [
'user_type' => ['
use BenSampo\Enum\Rules\Enum;
public function store(Request $request)
{
$this->validate($request, [
'user_type' => ['
use BenSampo\Enum\Enum;
use BenSampo\Enum\Contracts\LocalizedEnum;
final class UserType extends Enum implements LocalizedEnum
{
// ...
}
use BenSampo\Enum\Enum;
use BenSampo\Enum\Attributes\Description;
#[Description('List of available User types')]
final class UserType extends Enum
{
...
}
use BenSampo\Enum\Enum;
use BenSampo\Enum\Attributes\Description;
final class UserType extends Enum
{
const Administrator = 'Administrator';
#[Description('Super admin')]
const SuperAdministrator = 'SuperAdministrator';
}
UserType::fromValue(UserType::Administrator); // Returns instance of Enum with the value set to UserType::Administrator
var_dump(UserType::getInstances());
array(4) {
'Administrator' =>
class BenSampo\Enum\Tests\Enums\UserType#415 (3) {
public $key =>
string(13) "Administrator"
public $value =>
int(0)
public $description =>
string(13) "Administrator"
}
'Moderator' =>
class BenSampo\Enum\Tests\Enums\UserType#396 (3) {
public $key =>
string(9) "Moderator"
public $value =>
int(1)
public $description =>
string(9) "Moderator"
}
'Subscriber' =>
class BenSampo\Enum\Tests\Enums\UserType#393 (3) {
public $key =>
string(10) "Subscriber"
public $value =>
int(2)
public $description =>
string(10) "Subscriber"
}
'SuperAdministrator' =>
class BenSampo\Enum\Tests\Enums\UserType#102 (3) {
public $key =>
string(18) "SuperAdministrator"
public $value =>
int(3)
public $description =>
string(19) "Super administrator"
}
}
UserType::coerce(0); // Returns instance of UserType with the value set to UserType::Administrator
UserType::coerce('Administrator'); // Returns instance of UserType with the value set to UserType::Administrator
UserType::coerce(99); // Returns null (not a valid enum value)