PHP Enum - get rid of boilerplate mappings

hakobyansen

Senik Hakobyan

Posted on May 6, 2021

PHP Enum - get rid of boilerplate mappings

Hey there. Just decided to put together a small package, that will help me to get rid of boilerplate mappings like public function getAll(): array // returns some static array of strings.

You just need to define constants, and my Enum\Enum class will do the rest for you, thanks to Reflection API.

Here is an example:

<?php
namespace App;

use Enum\Enum;

class Status extends Enum
{
    const ACTIVE   = 'active';
    const INACTIVE = 'inactive';
    const BLOCKED  = 'blocked';
}

$status = new Status();

$status->toArray(); // will return [ 'active' => 'active', 'inactive' => 'inactive', 'blocked' => 'blocked' ]
$status->hasValue('active'); // will return boolean
$status->hasKey('blocked'); // will return boolean
Enter fullscreen mode Exit fullscreen mode

Enum's constructor has 2 parameters: $caseLower=true and $caseUpper=false.

If you want the case of array keys to be same as constants case - pass false as $caseLower.

$status = new Status(false);
Enter fullscreen mode Exit fullscreen mode

If you want the case of array keys to be uppercase - pass false as $caseLower and true as $caseUpper.

$status = new Status(false, true);
Enter fullscreen mode Exit fullscreen mode

We just created our custom type Enum. Now I can even use it to have better typed properties in my classes. For example:

<?php

namespace App;

use Enum\Enum;

class Person
{
    public int $identifier;
    public string $name;
    public Enum $status;
}
Enter fullscreen mode Exit fullscreen mode

Now I know exactly what type has my $status and I know exactly how I should work with that type. I find this much better than using some raw arrays.

GitHub repo:

GitHub logo hakobyansen / phpenum

All constants in your class, which extends Enum, will be converted to array of enum options.

PHP Enum

This package provides you a class named Enum. It is designed to help you to structure your Enum classes easily and get rid of the most boilerplate code.

Usage

Installation

Install via composer:

composer require codebot/phpenum

How to use

All your Enum classes should extend the abstract Enum class. All constants in your class, which extends Enum, will be converted to enum options.

<?php

namespace App;

use Enum\Enum;

class Status extends Enum
{
    const ACTIVE   = 'active';
    const INACTIVE = 'inactive';
    const BLOCKED  = 'blocked';
}

$status = new Status();

$status->toArray(); // will return [ 'active' => 'active', 'inactive' => 'inactive', 'blocked' => 'blocked' ]
$status->hasValue('active'); // will return boolean
$status->hasKey('blocked'); // will return boolean
Enter fullscreen mode Exit fullscreen mode

Enum constructor has 2 parameters: $caseLower=true and…

Have a nice day!

💖 💪 🙅 🚩
hakobyansen
Senik Hakobyan

Posted on May 6, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related