PHP code example of githubjeka / enum

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

    

githubjeka / enum example snippets


final class SizeEnum extends \githubjeka\enum\BaseObjectEnum
{
    const XS = 'xs';
    const SMALL = 's';
    const MEDIUM = 'm';
    const LARGE = 'l';
    const XL = 'xl';
    const XXL = 'xxl';

    public static function getList(): array
    {
        return [
            self::XS => 'Extra small(xs)',
            self::SMALL => 'Small',
            self::MEDIUM => 'Medium',
            self::LARGE => 'Large',
            self::XL => 'Extra large(xl)',
            self::XXL => 'Extra extra large(xxl)',
        ];
    }
}

class Shirt
{
    private $size;

    public function __construct(SizeEnum $size)
    {
        $this->size = $size;
    }

    public function size(): SizeEnum
    {
        return $this->size;
    }
}

$sizeFromDb = 'xxl';
$size = new SizeEnum($sizeFromDb);
$shirt = new Shirt($size);

$shirt->size()->asKey(); // (string) xxl 

$shirt->size()->asLabel(); // (string) Extra extra large(xxl)

$shirt->size()->equals(new SizeEnum(SizeEnum::XS)); // (bool) false

echo $shirt->size(); // (string) xxl

const XS = '0';         // not recommend
const SMALL = '1';      // not recommend
const MEDIUM = 2;       // not recommend
const LARGE = 3;        // not recommend

const XS = 0;
const SMALL = 1;
const MEDIUM = 2;
const LARGE = 3;