donatorsky / enum
此包已废弃,不再维护。未建议替代包。
PHP 7.1+ 的枚举库
1.0.0
2017-07-15 08:49 UTC
Requires
- php: >=7.1.0
Requires (Dev)
- phpunit/phpunit: ^6.2
This package is auto-updated.
Last update: 2023-12-29 02:41:28 UTC
README
PHP 7.1+ 的枚举库
当需要只允许接受有限集合中的一个值时,枚举类型非常有用。它还有助于逻辑上将它们分组在同一个地方。
其接口主要基于java.lang.Enum,但实现为其添加了一些额外的魔法。
基本用法
namespace My\App; /** * List of all available days. * * @method static Day MONDAY() * @method static Day TUESDAY() * @method static Day WEDNESDAY() * @method static Day THURSDAY() * @method static Day FRIDAY() * @method static Day SATURDAY() * @method static Day SUNDAY() */ class Day extends \Donatorsky\Enum\Enum { // Make available values private/protected if You want to force using getters private const MONDAY = 1; private const TUESDAY = 2; private const WEDNESDAY = 3; private const THURSDAY = 4; private const FRIDAY = 5; private const SATURDAY = 6; private const SUNDAY = 7; /** * @var int */ protected $value; /** * Day constructor. * * @param int $value */ protected function __construct(int $value) { $this->value = $value; } /** * @return int */ public function getValue(): int { return $this->value; } } // Somewhere deep in code... function getDeveloperState(Day $day): string { switch ($day) { case Day::MONDAY(): return 'Get redy to work'; break; case Day::TUESDAY(): case Day::WEDNESDAY(): case Day::THURSDAY(): return 'Work hard'; break; case Day::FRIDAY(): return 'Save all work'; break; case Day::SATURDAY(): case Day::SUNDAY(): return 'Save all work'; break; // If You add new constants default: return 'Where am I?!'; break; } } // Let's see what to do on friday... echo getDeveloperState(Day::FRIDAY()) . PHP_EOL; // Or today /** * In fact fromValue() will always return object of type that was called on * @var Day $today */ $today = Day::fromValue(date('N'), false); echo getDeveloperState($today) . PHP_EOL; // Get constant name of current day echo $today->name() . PHP_EOL; // Or check if it is specified day echo 'Is monday today? ' . ($today->is('MONDAY') ? 'Yes' : 'No') . PHP_EOL; // Or check it in other way echo 'Is monday today? ' . ($today->equals(Day::MONDAY()) ? 'Yes' : 'No') . PHP_EOL; // Tell me who am I... // Note: Also __toString() uses hashCode(), so "$today" will work unless hashcode() is overwritten echo $today->hashCode() . PHP_EOL; // Call (optional) getter of value echo 'Value of current day is ' . $today->getValue(); // Let's see if there is a day named... echo 'Is NON_EXISTING_DAY real? ' . (Day::has('NON_EXISTING_DAY') ? 'Yes' : 'No') . PHP_EOL; // So which days are true? echo 'Please, choose one of following days: ' . implode(', ', Day::keys()) . PHP_EOL; // Ok, last question // Note: valueOf() returns object of Day class echo 'What is the value of WEDNESDAY? ' . Day::valueOf(Day::class, 'WEDNESDAY')->getValue();
与其他实现的主要区别在于,它允许常量具有复杂值
namespace Donatorsky\Enum\Tests; use Donatorsky\Enum\Enum; /** * Class Rectangle * * @method static Rectangle SMALL() * @method static Rectangle MEDIUM() * @method static Rectangle LARGE() * * @package Donatorsky\Enum\Tests */ class Rectangle extends Enum { public const SMALL = [ 2, 3, 'RED']; public const MEDIUM = [ 5, 8, 'GREEN']; public const LARGE = [13, 21, 'BLUE']; /** * @var float */ protected $width; /** * @var float */ protected $height; /** * @var float */ protected $area; /** * @var Color */ protected $color; /** * Rectangle constructor. * * @param float $width * @param float $height * @param string $color */ protected function __construct(float $width, float $height, string $color) { $this->width = $width; $this->height = $height; $this->area = $width * $height; $this->color = Color::$color(); } /** * @return float */ public function getWidth(): float { return $this->width; } /** * @return float */ public function getHeight(): float { return $this->height; } /** * @return Color */ public function getColor(): Color { return $this->color; } /** * @return float */ public function getArea(): float { return $this->area; } }