codebot / phpenum
Enum helper, that will help you to define Enum types easily by defining just constants.
Installs: 12 390
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-11-19 01:31:40 UTC
README
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
Enum 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);
If you want the case of array keys to be uppercase - pass false
as $caseLower
and true
as $caseUpper
.
$status = new Status(false, true);