2017 © Pedro Peláez
 

library polycast

Safely cast values to int, float, or string

image

theodorejb/polycast

Safely cast values to int, float, or string

  • Monday, October 26, 2015
  • by theodorejb
  • Repository
  • 5 Watchers
  • 48 Stars
  • 28,074 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 12 Versions
  • 20 % Grown

The README.md

PolyCast

Provides safe_int, safe_float, and safe_string functions. The functions return true if a value can be cast to the designated type without data loss, and false if it cannot., (*1)

Three complementary functions are also included: to_int, to_float, and to_string. These functions cast and return a value if the corresponding safe_ function returns true, and throw a CastException if it returns false., (*2)

This library was originally based on the Safe Casting Functions RFC proposed (but ultimately declined) for PHP 7. For additional background info see PolyCast: a library for safe type conversion in PHP., (*3)

Acceptable casts

safe_int

  • Integers
  • Floats without a remainder between PHP_INT_MIN and PHP_INT_MAX
  • Strings with an optional positive/negative sign, without leading zeros, and containing the digits 0-9 with a value between PHP_INT_MIN and PHP_INT_MAX.

safe_float

  • Floats
  • Integers
  • Strings with an optional positive/negative sign matching the format described at https://php.net/manual/en/language.types.float.php.

safe_string

  • Strings
  • Integers
  • Floats
  • Objects with a __toString method

The safe_ functions will always return false if passed null, true or false, an array, resource, or object (with the exception of objects with a __toString method passed to safe_string)., (*4)

Install via Composer

composer require theodorejb/polycast, (*5)

Usage examples

Input validation

use function theodorejb\polycast\{ safe_int, safe_float, safe_string };

if (!safe_string($_POST['name'])) {
    echo 'Name must be a string';
} elseif (!safe_int($_POST['quantity'])) {
    echo 'Quantity must be an integer';
} elseif (!safe_float($_POST['price'])) {
    echo 'Price must be a number';
} else {
    addProduct($_POST['name'], (int)$_POST['quantity'], (float)$_POST['price']);
}

function addProduct(string $name, int $quantity, float $price)
{
    // ... a database query would go here
}

Safe type conversion

use theodorejb\polycast;

try {
    $totalRevenue = 0.0;
    $totalTransactions = 0;

    foreach ($csvRows as $row) {
        $totalRevenue += polycast\to_float($row['monthly_revenue']);
        $totalTransactions += polycast\to_int($row['monthly_transactions']);
    }

    // do something with totals
} catch (polycast\CastException $e) {
    echo "Error: " . $e->getMessage();
    var_dump($e->getTrace());
}

Author

Theodore Brown
https://theodorejb.me, (*6)

License

MIT, (*7)

The Versions

26/10 2015

dev-master

9999999-dev

Safely cast values to int, float, or string

  Sources   Download

MIT

The Development Requires

by Theodore Brown

25/10 2015

v1.0.0

1.0.0.0

Safely cast values to int, float, or string

  Sources   Download

MIT

The Development Requires

by Theodore Brown

18/03 2015

v0.9.0

0.9.0.0

Safely cast values to int, float, or string

  Sources   Download

MIT

The Development Requires

by Theodore Brown

17/03 2015

v0.8.0

0.8.0.0

Safely cast values to int, float, or string

  Sources   Download

MIT

The Development Requires

by Theodore Brown

09/03 2015

v0.7.0

0.7.0.0

Safely cast values to int, float, or string

  Sources   Download

MIT

The Development Requires

by Theodore Brown

20/11 2014

v0.6.0

0.6.0.0

Safely cast values to int, float, or string

  Sources   Download

MIT

The Development Requires

by Theodore Brown

12/11 2014

v0.5.0

0.5.0.0

Safely cast values to int, float, or string

  Sources   Download

MIT

The Development Requires

by Theodore Brown

21/10 2014

v0.4.1

0.4.1.0

Safely cast values to int, float, or string

  Sources   Download

MIT

The Development Requires

by Theodore Brown

21/10 2014

v0.4.0

0.4.0.0

Safely cast values to int, float, or string

  Sources   Download

MIT

The Development Requires

by Theodore Brown

09/10 2014

v0.3.0

0.3.0.0

Safely cast values to int, float, or string

  Sources   Download

MIT

The Development Requires

by Theodore Brown

07/10 2014

v0.2.0

0.2.0.0

Safely cast values to int, float, or string

  Sources   Download

MIT

The Development Requires

by Theodore Brown

07/10 2014

v0.1.1

0.1.1.0

Safely cast values to int, float, or string

  Sources   Download

MIT

The Development Requires

by Theodore Brown