miladrahimi/php-enum

A PHP Abstract Enum Class

v1.3 2021-08-04 09:34 UTC

This package is auto-updated.

Last update: 2024-11-10 11:44:54 UTC


README

Latest Stable Version Total Downloads Build Status Coverage Status License

PHP-Enum

To make your Enums feel alive, just make them extend Enum abstract class!

Installation

Install Composer and run following command in your project's root directory:

composer require miladrahimi/php-enum "1.*"

Documentation

Consider this enum class:

namespace MiladRahimi\Enum\Enum;

class SampleEnum extends Enum
{
    const UNO = 1;
    const ONE = 1;
    const TWO = 2;
    const STR = "sth";
}

Now you are able to use this methods:

SampleEnum::all(); // ['UNO' => 1, 'ONE' => 1, 'TWO' => 2, 'STR' => 'sth']

SampleEnum::keys(); // ['UNO', 'ONE', 'TWO', 'STR'];

SampleEnum::values(); // [1, 1, 2, 'sth']

SampleEnum::hasKey('ONE'); // true

SampleEnum::hasKey('xXx'); // false

SampleEnum::hasValue(2); // true

SampleEnum::hasValue('xXx'); // false

SampleEnum::valueOf('ONE'); // 1

SampleEnum::keysOf(1); // ['UNO', 'ONE']

SampleEnum::keyOf(1); // 'UNO'

SampleEnum::randomKey(); // One of 'UNO', 'ONE', 'TWO', 'STR'

SampleEnum::randomKeyExceptKeys(['ONE', 'TWO']); // One of 'UNO', 'STR'

SampleEnum::randomKeyExceptValues([SampleEnum::STR, SampleEnum::TWO]); // One of 'ONE', 'UNO'

SampleEnum::randomValue(); // One of 1, 2, 'sth'

SampleEnum::randomValueExceptValues([SampleEnum::STR, SampleEnum::TWO]); // One of SampleEnum::ONE, SampleEnum::UNO

SampleEnum::randomValueExceptKeys(['STR', 'TWO']); // One of SampleEnum::ONE, SampleEnum::UNO

License

PHP-Enum is initially created by Milad Rahimi and released under the MIT License.