PHP code example of sourceboat / laravel-enumeration

1. Go to this page and download the library: Download sourceboat/laravel-enumeration 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/ */

    

sourceboat / laravel-enumeration example snippets


php artisan make:enum UserType

// resources/lang/en/enums.php


use App\Enums\UserType;

return [

    UserType::class => [
        UserType::Moderator => 'Moderator',
    ],

];

// resources/lang/da/enums.php

use App\Enums\UserType;

return [

    UserType::class => [
        UserType::Moderator => 'studievært',
    ],

];



namespace App\Models;

use App\Enums\UserType;
use Illuminate\Database\Eloquent\Model;
use Sourceboat\Enumeration\Casts\Enum;

class User extends Model
{
    protected $casts = [
        'type' => Enums::class . ':' . UserType::class,
    ];
}



namespace App\Models;

use App\Enums\UserType;
use Illuminate\Database\Eloquent\Model;
use Sourceboat\Enumeration\Casts\Enum;

class User extends Model
{
    protected $casts = [
        'type' => Enums::class . ':' . UserType::class . ',0', // appending the 0 means it is not nullable,
    ];                                                         // this seems counter intuitive, but thats the way it is recognized as `false`,
}                                                              // as `false` is somehow evaluated as `true`.

// For example when the value has been changed manually in the database. Let's say the type is `10`.
$type = $user->type

// Then the following will be the case:

echo $type === UserType::defaultMember(); // "true"
echo $type->value; // "0"



namespace App\Models;

use App\Enums\UserType;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $casts = [
        'type' => UserType::class, 
    ];
}
 php
UserType::Moderator() // Returns an instance of UserType with UserType::Moderator()->value === 1
 php
UserType::keys(); // Returns ['Administrator', 'Moderator', 'Subscriber', 'SuperAdministrator']
 php
UserType::values(); // Returns [0, 1, 2, 3]
 php
UserType::Moderator()->key(); // Returns 'Moderator'
 php
UserType::Moderator()->value(); // Returns 1
 php
UserType::SuperAdministrator()->localized(); // Returns for example 'Super Administrator', but `enums.App\Enums\UserType.3` when not set.
 php
UserType::SuperAdministrator()->is(UserType::Moderator()); // -> false
UserType::SuperAdministrator()->is(UserType::SuperAdministrator()); // -> true
 php
UserType::SuperAdministrator()->isModerator(); // -> false
UserType::SuperAdministrator()->isSuperAdministrator(); // -> true
UserType::SuperAdministrator()->isStudent(); // -> throws Eloquent\Enumeration\Exception\UndefinedMemberException
 php
UserType::randomMember(); // Returns Administrator(), Moderator(), Subscriber() or SuperAdministrator()
 php
UserType::defaultMember(); // Returns Administrator()
 php
UserType::membersByBlacklist([UserType::Moderator()]); // Returns Administrator(), Subscriber() and SuperAdministrator()
 php
UserType::toSelectArray(); // Returns [0 => 'Administrator', 1 => 'Moderator', 2 => 'Subscriber', 3 => 'SuperAdministrator']
 php
UserType::toLocalizedSelectArray(); // Returns [0 => 'Administrator', 1 => 'Moderator', 2 => 'Subscriber', 3 => 'Super Administrator']
 php
public function store(Request $request)
{
    $this->validate($request, [
        'user_type' => [
            '
 php
public function store(Request $request)
{
    $this->validate($request, [
        'user_type' => [
            'lues `1` and `2`
        ],
    ]);
}
 php
public function store(Request $request)
{
    $this->validate($request, [
        'user_type' => [
            ' all values but the values `0` and `3`
        ],
    ]);
}
 php
// enums.php

use App\Enums\UserType;

return [
    UserType::class => [
        'permissions' => [
            UserType::Administrator => [
                'user.foreign.edit',
                'user.foreign.delete',
            ],
            UserType::Subscriber => [
                'user.self.edit',
                'user.self.delete',
            ],
        ]
    ]
]
 php 
UserType::Subscriber()->config('permissions'); // which return the given array.
UserType::Moderator()->config('permissions', ['thread.foreign.archive']); // you can also define a default value.