{"id":16924468,"url":"https://github.com/theodorejb/polycast","last_synced_at":"2025-09-01T02:43:34.925Z","repository":{"id":21527879,"uuid":"24847130","full_name":"theodorejb/PolyCast","owner":"theodorejb","description":"Safely cast values to int, float, or string in PHP","archived":false,"fork":false,"pushed_at":"2022-06-29T02:34:19.000Z","size":64,"stargazers_count":51,"open_issues_count":0,"forks_count":4,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-06-24T23:06:55.409Z","etag":null,"topics":["php","type-safety"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/theodorejb.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-10-06T13:34:04.000Z","updated_at":"2024-09-14T10:38:22.000Z","dependencies_parsed_at":"2022-07-16T14:18:23.190Z","dependency_job_id":null,"html_url":"https://github.com/theodorejb/PolyCast","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/theodorejb/PolyCast","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theodorejb%2FPolyCast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theodorejb%2FPolyCast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theodorejb%2FPolyCast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theodorejb%2FPolyCast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/theodorejb","download_url":"https://codeload.github.com/theodorejb/PolyCast/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theodorejb%2FPolyCast/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273067850,"owners_count":25039908,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-01T02:00:09.058Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","type-safety"],"created_at":"2024-10-13T20:05:16.130Z","updated_at":"2025-09-01T02:43:34.898Z","avatar_url":"https://github.com/theodorejb.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PolyCast\n\nProvides `safe_int`, `safe_float`, and `safe_string` functions.\nThe functions return true if a value can be cast to the designated type without\ndata loss, and false if it cannot.\n\nThree complementary functions are also included: `to_int`, `to_float`, and\n`to_string`. These functions cast and return a value if the corresponding\n*safe_* function returns true, and throw a `CastException` if it returns false.\n\nThis library was originally based on the [Safe Casting Functions RFC](https://wiki.php.net/rfc/safe_cast)\nproposed (but ultimately declined) for PHP 7. For additional background info see\n[PolyCast: a library for safe type conversion in PHP](https://theodorejb.me/2015/10/25/polycast/).\n\n## Acceptable casts\n\n### `safe_int`\n\n* Integers\n* Floats without a remainder between `PHP_INT_MIN` and `PHP_INT_MAX`\n* Strings with an optional positive/negative sign, without leading zeros, and\ncontaining the digits 0-9 with a value between `PHP_INT_MIN` and `PHP_INT_MAX`.\n\n### `safe_float`\n\n* Floats\n* Integers\n* Strings with an optional positive/negative sign matching the format described\nat https://php.net/manual/en/language.types.float.php.\n\n### `safe_string`\n\n* Strings\n* Integers\n* Floats\n* Objects with a `__toString` method\n\nThe *safe_* functions will always return false if passed `null`, `true` or\n`false`, an array, resource, or object (with the exception of objects with a\n`__toString` method passed to `safe_string`).\n\n## Install via Composer\n\n`composer require theodorejb/polycast`\n\n## Usage examples\n\n### Input validation\n\n```php\nuse function theodorejb\\polycast\\{ safe_int, safe_float, safe_string };\n\nif (!safe_string($_POST['name'])) {\n    echo 'Name must be a string';\n} elseif (!safe_int($_POST['quantity'])) {\n    echo 'Quantity must be an integer';\n} elseif (!safe_float($_POST['price'])) {\n    echo 'Price must be a number';\n} else {\n    addProduct($_POST['name'], (int)$_POST['quantity'], (float)$_POST['price']);\n}\n\nfunction addProduct(string $name, int $quantity, float $price)\n{\n    // ... a database query would go here\n}\n```\n\n### Safe type conversion\n\n```php\nuse theodorejb\\polycast;\n\ntry {\n    $totalRevenue = 0.0;\n    $totalTransactions = 0;\n\n    foreach ($csvRows as $row) {\n        $totalRevenue += polycast\\to_float($row['monthly_revenue']);\n        $totalTransactions += polycast\\to_int($row['monthly_transactions']);\n    }\n\n    // do something with totals\n} catch (polycast\\CastException $e) {\n    echo \"Error: \" . $e-\u003egetMessage();\n    var_dump($e-\u003egetTrace());\n}\n```\n\n## Author\n\nTheodore Brown  \n\u003chttps://theodorejb.me\u003e\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheodorejb%2Fpolycast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftheodorejb%2Fpolycast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheodorejb%2Fpolycast/lists"}