PHP code example of cloudstek / php-enum

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

    

cloudstek / php-enum example snippets


use Cloudstek\Enum\Enum;

/**
 * @method static self TODO()
 * @method static self IN_PROGRESS()
 * @method static self DONE()
 */
class TaskStatus extends Enum
{
    private const TODO = 'todo';
    private const IN_PROGRESS = 'in_progress';
    private const DONE = 'done';
}

TaskStatus::TODO !== TaskStatus::TODO()

class Task
{
    /** @var TaskStatus */
    private $status;

    /**
     * Set status
     *
     * @param TaskStatus $status
     */
    public function setStatus(TaskStatus $status)
    {
        $this->status = $status;
    }

    // ..
}

class Task
{
    /** @var TaskStatus */
    private $status;

    /**
     * Set status
     *
     * @param TaskStatus|string $status
     * 
     * @throws \UnexpectedValueException On unknown status.
     */
    public function setStatus($status)
    {
        $this->status = TaskStatus::get($status);
    }

    // ..
}

// Compare by instance
TaskStatus::TODO() === TaskStatus::TODO();                 // true
TaskStatus::TODO() === TaskStatus::get('todo');            // true
TaskStatus::get('TODO') === TaskStatus::get('todo');       // true
TaskStatus::TODO() === TaskStatus::get(TaskStatus::TODO()) // true

TaskStatus::TODO() === TaskStatus::DONE();                 // false
TaskStatus::TODO() === TaskStatus::get('done');            // false

// Compare by value
(string) TaskStatus::TODO() === 'todo';                    // true
TaskStatus::TODO()->getValue() === 'todo';                 // true

use Cloudstek\Enum\Enum;

class FooEnum extends Enum
{
    private const FOO = 'foo';
}

class BarEnum extends FooEnum
{
    private const BAR = 'bar';
}

class Foo
{
    public function doSomething(FooEnum $foo)
    {
        // Do something...
    }
}

$foo = new Foo();
$foo->doSomething(FooEnum::FOO()); // Allowed and OK, we were expecting FooEnum
$foo->doSomething(BarEnum::BAR()); // Allowed but not OK, we got BarEnum!

use Cloudstek\Enum\Enum;

abstract class BaseEnum extends Enum
{
    private const HELLO = 'world';
}

final class FooEnum extends BaseEnum
{
    private const FOO = 'foo';
}

final class BarEnum extends BaseEnum
{
    private const BAR = 'bar';
}

class Foo
{
    public function doSomething(FooEnum $foo)
    {
      // Do something...
    }
}

$foo = new Foo();
$foo->doSomething(FooEnum::FOO()); // Allowed and OK, we were expecting FooEnum
$foo->doSomething(BarEnum::BAR()); // Fatal error

class Foo
{
    public function doSomething(BaseEnum $foo)
    {
      // Do something...
    }
}

$foo = new Foo();
$foo->doSomething(FooEnum::FOO()); // OK
$foo->doSomething(BarEnum::BAR()); // OK

// Update task
$status = TaskStatus::TODO();

$db->update($task, [
    'status' => $status->getName() // 'status' => 'todo'
]);

// Fetch task
$taskRow = $db->tasks->fetchOne(13); // [..., 'status' => 'todo', ...]

$task = new Task();
// ..
$task->setStatus(TaskStatus::get($taskRow['status']));

// or if you call TaskStatus::get() in Task::setStatus()
$task->setStatus($taskRow['status']);
bash
composer