PHP code example of chadsikorra / php-simple-enum

1. Go to this page and download the library: Download chadsikorra/php-simple-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/ */

    

chadsikorra / php-simple-enum example snippets


use Enums\SimpleEnumInterface;
use Enums\SimpleEnumTrait;

class DayOfWeek implements SimpleEnumInterface
{
    use SimpleEnumTrait;

    const Monday = 1;

    const Tuesday = 2;

    const Wednesday = 3;

    const Thursday = 4;

    const Friday = 5;

    const Saturday = 6;

    const Sunday = 7;
}

// Get all of the names
var_dump(DayOfWeek::names());

// Get all of the values
var_dump(DayOfWeek::values());

// View it as an array
var_dump(DayOfWeek::toArray());

// Check if a name is valid. This is case insensitive.
if (DayOfWeek::isValidName('Monday')) {
    // ...
}

// Check if a value is valid.
if (DayOfWeek::isValidValue(DayOfWeek::Monday)) {
    // ...
}

// Get the name for a specific value.
var_dump(DayOfWeek::getValueName(1));

// Get the value for a specific name. This is case insensitive.
var_dump(DayOfWeek::getNameValue('Monday'));

use Enums\FlagEnumInterface;
use Enums\FlagEnumTrait;

class FilePermission implements FlagEnumInterface
{
    use FlagEnumTrait;

    const Read = 1;

    const Write = 2;

    const Execute = 4;
}

$permission = new FilePermission(FilePermission::Read | FilePermission::Write);

// Remove a permission
$permission->remove('Write');

// Add a permission
$permission->add('Execute');

// Check for a permission
if ($permission->has('Read')) {
    // ...
}

// Get the flag value
var_dump($permission->getValue());

// Get all of the names for the flag as an array
var_dump($permission->getNames());

// View the string representation (comma delimited list)
var_dump((string) $permission);