PHPのEnum型
PHP v8.1から入った
https://www.php.net/releases/8.1/en.php#enumerations
https://www.php.net/manual/ja/language.enumerations.overview.php
それ以前は、ハックで自作Enumを作る感じになってた
https://qiita.com/Hiraku/items/71e385b56dcaa37629fe
https://qiita.com/suin/items/17ee61d7e75b422a7ec3
https://speakerdeck.com/takayukifujisawa/nazephpnihaenumganaifalseka
https://speakerdeck.com/twada/php-conference-2016?slide=40
code:php
final class ArrangeType {
const ENUM = [
'size' => 'size',
'color' => 'color',
'shape' => 'shape',
'ng' => 'ng',
];
private $scalar; // genericがないので型を書けない
function __construct($key) {
if (!static::isValidValue($key)) {
throw new \InvalidArgumentException;
}
$this->scalar = self::ENUM$key;
}
static function isValidValue($key) {
return array_key_exists($key, self::ENUM);
}
function __toString() {
return $this->scalar;
}
}