granam/boolean-enum

Enumeration with booleans only

1.1.0 2021-03-23 15:28 UTC

This package is auto-updated.

Last update: 2024-11-24 17:45:31 UTC


README

Usage

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

Use enum

<?php
use \Granam\BooleanEnum\BooleanEnum;

$enum = BooleanEnum::getEnum(true);
echo $enum->getValue(); // 1
var_dump($enum->getValue()); // (bool) true
var_dump($enum->is(true)); // true
var_dump($enum->is(false)); // false
var_dump($enum->is(1)); // false
var_dump($enum->is($enum)); // true

NULL is NULL, enum can not hold it

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

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

Installation

composer.phar require granam/boolean-enum

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

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