-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- make status_slug and status_name attributes work
- Loading branch information
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Javaabu\Helpers\Enums; | ||
|
||
interface IsEnum | ||
{ | ||
public static function labels(): array; | ||
|
||
public function getLabel(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Javaabu\Helpers\Enums; | ||
|
||
|
||
use BackedEnum; | ||
|
||
trait NativeEnumsTrait | ||
{ | ||
public function getLabel(): string | ||
{ | ||
return self::labels()[$this->value]; | ||
} | ||
|
||
public static function getLabelFromKey(string $key): string | ||
{ | ||
return self::labels()[$key] ?? ''; | ||
} | ||
|
||
public static function getKeys(): array | ||
{ | ||
return array_column(self::cases(), 'value'); | ||
} | ||
|
||
public static function slugs(string|BackedEnum $input) | ||
{ | ||
if ($input instanceof BackedEnum) { | ||
$input = $input->value; | ||
} | ||
|
||
$status_slugs = []; | ||
$cases = self::cases(); | ||
|
||
foreach ($cases as $case) { | ||
$status_slugs[$case->value] = $case->value; | ||
} | ||
|
||
return $status_slugs[$input] ?? ''; | ||
} | ||
|
||
public static function names(string|BackedEnum $input) | ||
{ | ||
if ($input instanceof BackedEnum) { | ||
$input = $input->value; | ||
} | ||
|
||
$status_names = []; | ||
$cases = self::cases(); | ||
|
||
foreach ($cases as $case) { | ||
$status_names[$case->value] = $case->getLabel(); | ||
} | ||
|
||
return $status_names[$input] ?? ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Javaabu\Helpers\Enums; | ||
|
||
|
||
enum UserStatuses: string implements IsEnum | ||
{ | ||
use NativeEnumsTrait; | ||
|
||
case APPROVED = 'approved'; | ||
case PENDING = 'pending'; | ||
case BANNED = 'banned'; | ||
|
||
/** | ||
* Initialize Messages | ||
*/ | ||
public static function messages(): array | ||
{ | ||
return [ | ||
self::APPROVED->value => __('Your account is approved.'), | ||
self::PENDING->value => __('Your account needs to be approved before you can access it.'), | ||
self::BANNED->value => __('Your account has been banned.'), | ||
]; | ||
} | ||
|
||
public function getMessage(): string | ||
{ | ||
return self::messages()[$this->value]; | ||
} | ||
|
||
public static function getMessageFromKey(string $key): string | ||
{ | ||
return self::messages()[$key] ?? ''; | ||
} | ||
|
||
public static function labels(): array | ||
{ | ||
return [ | ||
self::APPROVED->value => __("Approved"), | ||
self::PENDING->value => __("Pending"), | ||
self::BANNED->value => __("Banned"), | ||
]; | ||
} | ||
} |