All Projects → theodorejb → PolyCast

theodorejb / PolyCast

Licence: MIT license
Safely cast values to int, float, or string in PHP

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to PolyCast

Returns
Make your functions return something meaningful, typed, and safe!
Stars: ✭ 2,015 (+3775%)
Mutual labels:  type-safety
Ts Toolbelt
ts-toolbelt is the largest, and most tested type library available right now, featuring +200 utilities. Our type collection packages some of the most advanced mapped types, conditional types, and recursive types on the market.
Stars: ✭ 3,099 (+5859.62%)
Mutual labels:  type-safety
Unchained
A fully type safe, compile time only units library.
Stars: ✭ 70 (+34.62%)
Mutual labels:  type-safety
Domino Ui
Domino-ui
Stars: ✭ 138 (+165.38%)
Mutual labels:  type-safety
Squid
Squid – type-safe metaprogramming and compilation framework for Scala
Stars: ✭ 172 (+230.77%)
Mutual labels:  type-safety
Dimensioned
Compile-time dimensional analysis for various unit systems using Rust's type system.
Stars: ✭ 235 (+351.92%)
Mutual labels:  type-safety
Babel Plugin Runtyper
⚡️ Runtime type-checker for JavaScript
Stars: ✭ 117 (+125%)
Mutual labels:  type-safety
vault
A typed, persistent store for values of arbitrary types
Stars: ✭ 55 (+5.77%)
Mutual labels:  type-safety
Typestrict
ESLint config focused on maximizing type safety 💪
Stars: ✭ 182 (+250%)
Mutual labels:  type-safety
request-parser
Small PHP Library for type-safe input handling
Stars: ✭ 53 (+1.92%)
Mutual labels:  type-safety
Quantities
Type-safe physical computations and unit conversions in Idris ⚖ 🌡 ⏲ 🔋 📐
Stars: ✭ 146 (+180.77%)
Mutual labels:  type-safety
Orm Lite
Header-Only, Strong-Typed, Compile-time Object Relation Mapping (ORM) in Modern C++ :-)
Stars: ✭ 164 (+215.38%)
Mutual labels:  type-safety
Dry Schema
Coercion and validation for data structures
Stars: ✭ 249 (+378.85%)
Mutual labels:  type-safety
Hegel
An advanced static type checker
Stars: ✭ 1,804 (+3369.23%)
Mutual labels:  type-safety
kanji
A strongly typed GraphQL API framework
Stars: ✭ 12 (-76.92%)
Mutual labels:  type-safety
Typecov
Track missing type coverage to ensure type safety
Stars: ✭ 128 (+146.15%)
Mutual labels:  type-safety
Percentage
A percentage type for Swift
Stars: ✭ 225 (+332.69%)
Mutual labels:  type-safety
tsafe
🔩 The missing TypeScript utils
Stars: ✭ 285 (+448.08%)
Mutual labels:  type-safety
regbits
C++ templates for type-safe bit manipulation
Stars: ✭ 53 (+1.92%)
Mutual labels:  type-safety
Truth
A Domain Representation Language
Stars: ✭ 23 (-55.77%)
Mutual labels:  type-safety

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.

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.

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.

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

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).

Install via Composer

composer require theodorejb/polycast

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

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].