Skip to content

grachevko/Enum

Repository files navigation

PHP Enum implementation

Latest Stable Version Total Downloads Scrutinizer Code Quality Code Coverage Build Status SensioLabsInsight

Installation

composer require premier/enum 

Usage

namespace Premier\Enum; /**  * @method static DriveWheel front()  * @method static DriveWheel rear()  * @method static DriveWheel allDrive()  * @method static DriveWheel fromCode(string $code)  * @method bool isFront()  * @method bool isRear()  * @method bool isAllDrive()  * @method string toCode()  */ final class DriveWheel extends Enum { private const FRONT = 1; private const REAR = 2; private const ALL_DRIVE = 3; protected static $code = [ self::FRONT => 'FWD', self::REAR => 'RWD', self::ALL_DRIVE => 'AWD', ]; } // New instance $drive = DriveWheel::create(1); // or $drive = DriveWheel::front(); // or $drive = DriveWheel::fromCode('FWD'); // or $drive = DriveWheel::from('code', 'FWD'); // Array instances DriveWheel::all(); // [DriveWheel::front(), DriveWheel::rear(), DriveWheel::allDrive()] DriveWheel::all(['FWD', 'RWD'], $reverse = false, $property = 'code'); // [DriveWheel::front(), DriveWheel::rear()] DriveWheel::all([1, 2], $reverse = true); // [DriveWheel::allDrive()] // Methods $drive->toId(); // 1 $drive->to('id'); // 1 (string) $drive; // '1' $drive->toName(); // 'front' $drive->toCode(); // 'FWD' $drive->to('code'); // 'FWD' $drive->isFront(); // true $drive->isRear(); // false $drive->eq(DriveWheel::front()); // false $drive->eq(DriveWheel::rear()); // false

Design

  • All constants MUST be private
  • All constants MUST be type of integer
  • All properties MUST NOT be public
  • All properties MUST be static
  • Properties MUST contain values for all defined constants

Doctrine

This lib distributed with Type class for Doctrine to easy configure each Enum as Doctrine Type.

Premier\Enum\Doctrine\EnumType::register(Premier\Enum\Gender::class, 'gender_enum', $property = 'id'); class Entity { /**  * @var Gender  *   * @ORM\Column(type="gender_enum")   */ public $gender; }

About

PHP Enum implementation

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors