granam/float-enum

Enumeration with floats only

1.1.0 2021-03-23 17:48 UTC

This package is auto-updated.

Last update: 2024-12-24 18:13:45 UTC


README

Usage

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

Use enum

<?php
use \Granam\FloatEnum\FloatEnum;

$enum = FloatEnum::getEnum(123);
echo $enum->getValue(); // 123
var_dump($enum->getValue()); // (float) 123.0
var_dump($enum->is(123.0)); // true
var_dump($enum->is(123)); // false
var_dump($enum->is('123.0')); // false
var_dump($enum->is($enum)); // true

NULL is NULL, enum can not hold it

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

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

Installation

composer.phar require granam/float-enum

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

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