granam/integer

Lightweight integer container with stand-alone converter

Installs: 21 980

Dependents: 20

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 0

Open Issues: 0

Type:project

7.1.0 2021-03-24 09:07 UTC

README

Build Status

Hint

First of all, make sure you don't need just a simple built-in int validation.

Versions requirements

  • 6.* requires PHP 7.1+ composer require granam/integer:6.*
  • 5.* requires PHP 7.0+ composer require granam/integer:5.*
  • 4.* requires PHP 5.6+ composer require granam/integer:4.*
  • 3.* requires PHP 5.4+ composer require granam/integer:3.*
<?php
use Granam\Integer\IntegerObject;
use Granam\Integer\Tools\Exceptions\WrongParameterType;

$integer = new IntegerObject(12345);

// int(12345)
var_dump($integer->getValue());

$integerFromString = new IntegerObject('124578');
// int(124578)
var_dump($integerFromString->getValue());

$integerFromFloatString = new IntegerObject('987.0');
// int(987)
var_dump($integerFromFloatString->getValue());

try {
    new IntegerObject(987.123);
} catch (WrongParameterType $integerException) {
   // Something get wrong: Some value has been lost on cast. Got '987.456', cast into integer 987
   echo('Something get wrong: ' . $integerException->getMessage() . "\n");
 }

$integerFromTrue = new IntegerObject(true);
// int(1)
var_dump($integerFromTrue->getValue());

$integerFromNull = new IntegerObject(null);
// int(0)
var_dump($integerFromNull->getValue());
// ...

$stringWithAlmostInteger = '0.9999999999999999';
$integerFromStringWithAlmostInteger = new IntegerObject($stringWithAlmostInteger);
// int(1)
var_dump($integerFromStringWithAlmostInteger->getValue());
// int(0) -- because of (int)(float)$value
var_dump((int)$stringWithAlmostInteger);