PHP code example of datomatic / laravel-enum-helper
1. Go to this page and download the library: Download datomatic/laravel-enum-helper 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/ */
datomatic / laravel-enum-helper example snippets
use Datomatic\LaravelEnumHelper\LaravelEnumHelper;
// Pure enum
enum Status
{
use LaravelEnumHelper;
case PENDING;
case ACCEPTED;
case DISCARDED;
case NO_RESPONSE;
public function color(): string
{
return match ($this) {
self::PENDING => '#000000',
self::ACCEPTED => '#0000FF',
self::DISCARDED => '#FF0000',
self::NO_RESPONSE => '#FFFFFF',
};
}
}
// BackedEnum
enum StatusString
{
use LaravelEnumHelper;
case PENDING = 'P';
case ACCEPTED = 'A';
case DISCARDED = 'D';
case NO_RESPONSE = 'N';
// or public function color(?string $lang = null): string
public function color(): string
{
...
}
// /lang/it/enums.php
return [
// If you need to translate just the description property
Status::class => [
Status::PENDING() => 'In attesa', // using invokable trait functionality
'ACCEPTED' => 'Accettato', // using the name of pure enum case
'DISCARDED' => 'Rifiutato',
'NO_RESPONSE' => 'Nessuna Risposta',
],
// If you need to translate multiple properties (e.g. description, excerpt)
StatusString::class => [
'description' => [ // using invokable trait functionality
StatusString::PENDING() => 'In attesa',
StatusString::ACCEPTED() => 'Accettato',
...
],
'excerpt' => [ // using the value of BackedEnum case
"P" => 'In attesa',
"A" => 'Accettato',
...
sh
php artisan enum:annotate {class?} {--folder=}
# Single enum
php artisan enum:annotate Enums/FQN/ClassName
# All enums in a folder
php artisan enum:annotate --folder=Enums/Folder/Path
/lang
/en
enums.php
/it
enums.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.