PHP code example of vicgutt / php-enhanced-enum

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

    

vicgutt / php-enhanced-enum example snippets


use VicGutt\PhpEnhancedEnum\Concerns\Enumerable;
use VicGutt\PhpEnhancedEnum\Contracts\EnumerableContract;

enum MyEnum: string implements EnumerableContract
{
    use Enumerable;
    
    case CASE1 = 'case1';
    case CASE2 = 'case2';
}

use VicGutt\PhpEnhancedEnum\Concerns\Enumerable;
use VicGutt\PhpEnhancedEnum\Contracts\EnumerableContract;

/**
 * A "unit enum"
 */
enum MovementsEnum implements EnumerableContract
{
    use Enumerable;

    case ME_TOO;
    case BLACK_LIVES_MATTER;
    case LGBTQ_PLUS;
    case AND_MANY_MANY_MORE;
}

/**
 * An integer typed "backed enum"
 */
enum PromiseEnum: int implements EnumerableContract
{
    use Enumerable;

    case PENDING = 1;
    case RESOLVED = 2;
    case REJECTED = 3;
}

/**
 * A string typed "backed enum"
 */
enum StatusEnum: string implements EnumerableContract
{
    use Enumerable;

    case PENDING = 'pending';
    case SUCCEEDED = 'succeeded';
    case FAILED = 'failed';
}

MovementsEnum::keys(); // ['ME_TOO', 'BLACK_LIVES_MATTER', 'LGBTQ_PLUS', 'AND_MANY_MANY_MORE']
PromiseEnum::keys(); // ['PENDING', 'RESOLVED', 'REJECTED']
StatusEnum::keys(); // ['PENDING', 'SUCCEEDED', 'FAILED']

MovementsEnum::values(); // ['ME_TOO', 'BLACK_LIVES_MATTER', 'LGBTQ_PLUS', 'AND_MANY_MANY_MORE']
PromiseEnum::values(); // [1, 2, 3]
StatusEnum::values(); // ['pending', 'succeeded', 'failed']

MovementsEnum::entries(); // ['ME_TOO' => 'ME_TOO', 'BLACK_LIVES_MATTER' => 'BLACK_LIVES_MATTER', 'LGBTQ_PLUS' => 'LGBTQ_PLUS', 'AND_MANY_MANY_MORE' => 'AND_MANY_MANY_MORE']
PromiseEnum::entries(); // ['PENDING' => 1, 'RESOLVED' => 2, 'REJECTED' => 3]
StatusEnum::entries(); // ['PENDING' => 'pending', 'SUCCEEDED' => 'succeeded', 'FAILED' => 'failed']

MovementsEnum::ME_TOO->is(MovementsEnum::ME_TOO); // true
MovementsEnum::ME_TOO->is(MovementsEnum::BLACK_LIVES_MATTER); // false
MovementsEnum::ME_TOO->is('ME_TOO'); // true
MovementsEnum::ME_TOO->is('Me_TOO'); // false
MovementsEnum::ME_TOO->is(123); // false

PromiseEnum::PENDING->is(PromiseEnum::PENDING); // true
PromiseEnum::PENDING->is(StatusEnum::PENDING); // false
PromiseEnum::PENDING->is(1); // true
PromiseEnum::PENDING->is('1'); // false
PromiseEnum::PENDING->is('yolo'); // false

StatusEnum::PENDING->is(StatusEnum::PENDING); // true
StatusEnum::PENDING->is(PromiseEnum::PENDING); // false
StatusEnum::PENDING->is('pending'); // true
StatusEnum::PENDING->is('PENDING'); // false
StatusEnum::PENDING->is(123); // false

MovementsEnum::ME_TOO->isAny([MovementsEnum::ME_TOO]); // true
MovementsEnum::ME_TOO->isAny([MovementsEnum::ME_TOO, MovementsEnum::BLACK_LIVES_MATTER]); // true
MovementsEnum::ME_TOO->isAny(['ME_TOO', MovementsEnum::BLACK_LIVES_MATTER]); // true
MovementsEnum::ME_TOO->isAny(MovementsEnum::BLACK_LIVES_MATTER, 'Me_TOO', 123]); // false

PromiseEnum::PENDING->isAny([PromiseEnum::PENDING]); // true
PromiseEnum::PENDING->isAny([PromiseEnum::PENDING, MovementsEnum::BLACK_LIVES_MATTER]); // true
PromiseEnum::PENDING->isAny([1, MovementsEnum::BLACK_LIVES_MATTER]); // true
PromiseEnum::PENDING->isAny(MovementsEnum::BLACK_LIVES_MATTER, 'PENDING', 123]); // false

StatusEnum::PENDING->isAny([StatusEnum::PENDING]); // true
StatusEnum::PENDING->isAny([StatusEnum::PENDING, MovementsEnum::BLACK_LIVES_MATTER]); // true
StatusEnum::PENDING->isAny(['pending', MovementsEnum::BLACK_LIVES_MATTER]); // true
StatusEnum::PENDING->isAny(MovementsEnum::BLACK_LIVES_MATTER, 'PENDING', 123]); // false

MovementsEnum::ME_TOO->isAll([MovementsEnum::ME_TOO]); // true
MovementsEnum::ME_TOO->isAll([MovementsEnum::ME_TOO, 'ME_TOO']); // true
MovementsEnum::ME_TOO->isAll([MovementsEnum::ME_TOO, 'ME_TOO', MovementsEnum::BLACK_LIVES_MATTER]); // false

PromiseEnum::PENDING->isAll([PromiseEnum::PENDING]); // true
PromiseEnum::PENDING->isAll([PromiseEnum::PENDING, 1]); // true
PromiseEnum::PENDING->isAll([PromiseEnum::PENDING, 1, MovementsEnum::BLACK_LIVES_MATTER]); // false

StatusEnum::PENDING->isAll([StatusEnum::PENDING]); // true
StatusEnum::PENDING->isAll([StatusEnum::PENDING, 'pending']); // true
StatusEnum::PENDING->isAll([StatusEnum::PENDING, 'pending', MovementsEnum::BLACK_LIVES_MATTER]); // false
bash
composer