PHP code example of fast-forward / enum

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

    

fast-forward / enum example snippets




declare(strict_types=1);

use FastForward\Enum\Helper\EnumHelper;
use FastForward\Enum\Trait\Comparable;
use FastForward\Enum\Trait\HasDescription;
use FastForward\Enum\Trait\HasNameLookup;
use FastForward\Enum\Trait\HasNames;
use FastForward\Enum\Trait\HasOptions;
use FastForward\Enum\Trait\HasValues;

enum Status: string
{
    use Comparable;
    use HasDescription;
    use HasNameLookup;
    use HasNames;
    use HasOptions;
    use HasValues;

    case Draft = 'draft';
    case Published = 'published';
}

Status::values(); // ['draft', 'published']
Status::names(); // ['Draft', 'Published']
Status::options(); // ['Draft' => 'draft', 'Published' => 'published']
Status::fromName('Draft'); // Status::Draft
Status::Draft->is(Status::Published); // false
Status::Draft->description(); // 'Draft'

EnumHelper::valueMap(Status::class); // ['draft' => Status::Draft, 'published' => Status::Published]



declare(strict_types=1);

use FastForward\Enum\Helper\EnumHelper;
use FastForward\Enum\LabeledEnumInterface;

enum Priority: int implements LabeledEnumInterface
{
    case Low = 1;
    case High = 2;

    public function label(): string
    {
        return match ($this) {
            self::Low => 'Low priority',
            self::High => 'High priority',
        };
    }
}

EnumHelper::labels(Priority::class); // ['Low priority', 'High priority']
EnumHelper::labelMap(Priority::class); // ['Low' => 'Low priority', 'High' => 'High priority']



declare(strict_types=1);

use FastForward\Enum\StateMachine\HasTransitions;
use FastForward\Enum\StateMachine\InvalidTransitionException;

enum ArticleWorkflow: string
{
    use HasTransitions;

    case Draft = 'draft';
    case Reviewing = 'reviewing';
    case Published = 'published';
    case Archived = 'archived';

    protected static function transitionMap(): array
    {
        return [
            self::Draft->name => [self::Reviewing, self::Archived],
            self::Reviewing->name => [self::Published, self::Draft],
            self::Published->name => [self::Archived],
            self::Archived->name => [],
        ];
    }

    protected static function initialStateCases(): array
    {
        return [self::Draft];
    }
}

ArticleWorkflow::Draft->canTransitionTo(ArticleWorkflow::Reviewing); // true
ArticleWorkflow::Archived->isTerminal(); // true
ArticleWorkflow::initialStates(); // [ArticleWorkflow::Draft]

try {
    ArticleWorkflow::Reviewing->assertCanTransitionTo(ArticleWorkflow::Archived);
} catch (InvalidTransitionException $exception) {
    // Invalid transition
}



declare(strict_types=1);

use FastForward\Enum\Calendar\Month;
use FastForward\Enum\Calendar\Quarter;
use FastForward\Enum\Calendar\Semester;
use FastForward\Enum\Calendar\Weekday;
use FastForward\Enum\Common\Priority;
use FastForward\Enum\Common\Severity;
use FastForward\Enum\Comparison\ComparisonOperator;
use FastForward\Enum\Container\ServiceLifetime;
use FastForward\Enum\DateTime\IntervalUnit;
use FastForward\Enum\Event\DispatchMode;
use FastForward\Enum\Http\Scheme;
use FastForward\Enum\Logger\LogLevel;
use FastForward\Enum\Outcome\Result;
use FastForward\Enum\Pipeline\FailureMode;
use FastForward\Enum\Process\SignalBehavior;
use FastForward\Enum\Runtime\Environment;
use FastForward\Enum\Sort\CaseSensitivity;
use FastForward\Enum\Sort\ComparisonResult;
use FastForward\Enum\Sort\NullsPosition;
use FastForward\Enum\Sort\SortDirection;

Environment::Production->isProduction(); // true
Priority::Critical->isHigherThan(Priority::Normal); // true
Severity::Error->isAtLeast(Severity::Warning); // true
LogLevel::Critical->isAtLeast(LogLevel::Warning); // true
Result::Partial->isSuccessful(); // true
ComparisonOperator::In->compare('draft', ['draft', 'published']); // true
IntervalUnit::Hour->seconds(2); // 7200
DispatchMode::Async->isAsync(); // true
ServiceLifetime::Singleton->isReusable(); // true
FailureMode::StopOnFailure->stopsOnFailure(); // true
Scheme::Https->defaultPort(); // 443
SignalBehavior::Handle->isTerminalControl(); // true
Weekday::Saturday->isWeekend(); // true
Month::December->quarter(); // 4
Quarter::Q2->months(); // [Month::April, Month::May, Month::June]
Semester::H2->quarters(); // [Quarter::Q3, Quarter::Q4]
SortDirection::Descending->reverse(); // SortDirection::Ascending
NullsPosition::Last->compareNullability(null, 'value'); // 1
CaseSensitivity::Insensitive->equals('Draft', 'draft'); // true
ComparisonResult::fromComparisonResult(-1); // ComparisonResult::RightGreater