ll-kuma-ll/php-enum

There is no license information available for the latest version (v1.0.0) of this package.

enumrated type class

v1.0.0 2017-10-18 15:59 UTC

This package is not auto-updated.

Last update: 2025-05-11 06:21:48 UTC


README

列挙型をPHPで利用するためにクラスで実装。
毎度作成するのが面倒なので、composerライブラリとして読み込める様に作成。

Composer設定

php composer.phar require ll-kuma-ll/php-enum

定義サンプル

namespace Foo;

use LLkumaLL\Enum\Enum;

class Sample extends Enum
{
    const ENUM = [
        'VALUE_1' => 'label 1',
        'VALUE_2' => 'label 2',
    ];
}

利用サンプル

use LLkumaLL\Enum\Manager;
use Foo\Sample;

// 単独で使いたい場合
$enum = Sample::VALUE_1();
// 'label 1' が出力される
echo $enum->label();
// 'VALUE_1' が出力される
echo $enum->value();

// まとめて取り扱いたい場合
$manager = new Manager(Sample::class);

// ENUM定数配列の定義分全部をループ処理
foreach ($manager->createAll() as $const => $enum) {
    // '同じ'が出力される
    echo $const == $enum->value() ? '同じ' : '違う';
}