granam/scalar-enum

Enumeration with scalar values

1.2.0 2021-03-24 16:45 UTC

This package is auto-updated.

Last update: 2024-11-25 00:50:22 UTC


README

Usage

  1. Use enum
  2. NULL is NULL, not Enum
  3. Installation

Use enum

<?php
$enum = \Granam\ScalarEnum\ScalarEnum::getEnum('foo bar');
echo $enum->getValue(); // foo bar
var_dump($enum->is('foo bar')); // true

NULL is NULL, enum can not hold it

You can not create ScalarEnum with NULL value. Just use NULL directly for such value.

<?php
try {
    \Granam\ScalarEnum\ScalarEnum::getEnum(null);
} catch(\Granam\ScalarEnum\Exceptions\UnexpectedValueToEnum $unexpectedValueToEnum) {
    echo $unexpectedValueToEnum->getMessage(); // Expected scalar or object with __toString method on strict mode, got NULL
}

Installation

composer.phar require granam/scalar-enum

or manually edit composer.json at your project and "require": block (extend existing)

"require": {
    "granam/scalar-enum": "dev-master"
}