1. Go to this page and download the library: Download mimosafa/php-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/ */
mimosafa / php-enumeration example snippets
use Enumeration\PureEnum;
/**
* @method static self PENDING()
* @method static self PUBLISHED()
* @method static self ARCHIVED()
*/
class Status extends PureEnum
{
public static function toArray(): array
{
return ['PENDING', 'PUBLISHED', 'ARCHIVED'];
}
}
$status = Status::PUBLISHED();
assert($status->name === 'PUBLISHED');
assert(Status::of('PENDING') === Status::PENDING());
use Enumeration\PureEnum;
use function Safe\glob; // Assuming Safe\glob for robustness
/**
* @method static self BACKED_ENUM_PHP()
* @method static self ENUMERATE_CONSTANTS_TRAIT_PHP()
* @method static self PURE_ENUM_PHP()
*/
class SourceFiles extends PureEnum
{
public static function toArray(): array
{
$files = glob(__DIR__ . '/src/*.php'); // Adjust path as needed
return array_map(fn($file) => strtoupper(str_replace('.', '_', basename($file))), $files);
}
}
// Example usage:
// assert(SourceFiles::PURE_ENUM_PHP() instanceof SourceFiles);
use Enumeration\BackedEnum;
use Enumeration\EnumerateConstantsTrait;
abstract class UserRole extends BackedEnum
{
use EnumerateConstantsTrait;
const Reader = 1;
const Editor = 2;
const Admin = 3;
const SuperAdmin = 4;
}
// An enum for regular site roles, excluding SuperAdmin.
class SiteUserRole extends UserRole
{
protected static function excludedConstantsFromEnumeration(): array
{
return ['SuperAdmin'];
}
}
// An enum for administrative roles.
class AdminRole extends UserRole
{
protected static function
use Enumeration\Attributes\ExcludeConstants;
use Enumeration\Attributes\IncludeConstants;
#[ExcludeConstants('SuperAdmin')]
class SiteUserRole extends UserRole
{
}
#[IncludeConstants('Admin', 'SuperAdmin')]
class AdminRole extends UserRole
{
}
// Returns [1, 2, 3]
SiteUserRole::values();
// Returns ['Admin', 'SuperAdmin']
AdminRole::names();
// Throws a ValueError because 'Reader' is not in AdminRole
AdminRole::of('Reader');
// You can still use the parent class for type hinting
function grantPermission(UserRole $role)
{
// ...
}
// Both are valid
grantPermission(SiteUserRole::Admin());
grantPermission(AdminRole::SuperAdmin());
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.