PHP code example of oskarstark / enum-helper

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

    

oskarstark / enum-helper example snippets




declare(strict_types=1);

namespace App\Enum;

enum Color: string
{
    case RED = 'red';
    case BLUE = 'blue';
}

App\Enum\Color::RED->equals(App\Enum\Color::BLUE); // returns false

App\Enum\Color::RED->notEquals(App\Enum\Color::RED); // returns false

    App\Enum\Color::RED->equalsOneOf([
        App\Enum\Color::BLUE,
        App\Enum\Color::RED,
    ]); // returns true



declare(strict_types=1);

namespace App\Enum;

use OskarStark\Enum\Trait\Comparable;
use OskarStark\Enum\Trait\ToArray;

enum Color: string
{
    use Comparable;
    use ToArray;

    case RED = 'red';
    case BLUE = 'blue';
    case GREEN = 'green';
    
    public function isNice(): bool
    {
        return self::equalsOneOf([
            self::BLUE,
            self::GREEN
        ]);
    }
}

App\Enum\Color::RED->isNice(); // returns false
App\Enum\Color::BLUE->isNice(); // returns true

App\Enum\Color::toArray(); // returns ['RED' => 'red', 'BLUE' => 'blue']

App\Enum\NonBackedEnum::toArray(); // returns ['RED' => 'RED', 'BLUE' => 'BLUE']