PHP code example of xakepehok / enum-helper

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

    

xakepehok / enum-helper example snippets




class Status extends \XAKEPEHOK\EnumHelper\EnumHelper
{
    
    const STATUS_PROCESSING = 'processing';
    const STATUS_APPROVED = 'approved';
    const STATUS_CANCELED = 'canceled';

    private $status;

    public function __construct($status) 
    {
        self::guardValidValue($status);
        $this->status = $status;
    }

    public  function getStatus()
    {
        return $this->status;
    }
    
    public static function values() : array{
        return [
            self::STATUS_PROCESSING,
            self::STATUS_APPROVED,
            self::STATUS_CANCELED,
        ];
    }    
}


$statusValue = Status::STATUS_PROCESSING;

$result = Status::switchCase($statusValue, [
    Status::STATUS_PROCESSING => function ($case) {
        //doSomethins();
        return null; //
    },
    Status::STATUS_APPROVED => function ($case) {
        return true;
    },
    Status::STATUS_CANCELED => false,
]);


$array = Status::associative([
    Status::STATUS_PROCESSING => 0,
    Status::STATUS_APPROVED => 1,
    Status::STATUS_CANCELED => -1,
]);


Status::isVlaid(Status::STATUS_PROCESSING); //true
Status::isVlaid('qwerty'); //false


Status::guardValidValue(Status::STATUS_PROCESSING); //OK
Status::guardValidValue('qwerty'); //throw exception