grachev / enum
This package is abandoned and no longer maintained.
The author suggests using the grachevko/Enum package instead.
PHP Enum implementation
2.2
2022-02-03 17:42 UTC
Requires
- php: >=7.3
- ext-ctype: *
Requires (Dev)
- doctrine/dbal: ^2
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^0.12.94
- phpstan/phpstan-phpunit: ^0.12.22
- phpstan/phpstan-strict-rules: ^0.12.10
- phpunit/phpunit: ^9.5
- symfony/serializer: ^5.3
- vimeo/psalm: ^4.9
README
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; }