PHPackages                             shin1x1/safe-cast - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. shin1x1/safe-cast

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

shin1x1/safe-cast
=================

v1.0.0(4mo ago)06.0kMITPHPPHP &gt;=8.0CI passing

Since Jun 3Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/shin1x1/safe-cast)[ Packagist](https://packagist.org/packages/shin1x1/safe-cast)[ RSS](/packages/shin1x1-safe-cast/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (4)Versions (4)Used By (0)

SafeCast PHP Library
====================

[](#safecast-php-library)

SafeCast is a library designed to simplify and safeguard typecasting in PHP. It offers an efficient way to convert mixed values to any given type, following the conversion rules of the built-in PHP functions `intval()`, `strval()`, `floatval()`, and `boolval()` without causing PHP errors.

Motivation
----------

[](#motivation)

The primary motivations behind SafeCast are:

- **Avoid PHP errors**: Built-in PHP casting functions, such as `intval()`, can sometimes cause errors depending on the value being converted. SafeCast provides an alternative that consistently returns either the safely cast value or `null`, instead of producing a PHP error.
- **Consistent error handling**: With SafeCast, errors during casting are uniformly handled either by throwing an `InvalidArgumentException` or returning `null`, making error handling more predictable and manageable.
- **Safe casting of mixed types**: It provides a safe and consistent way to cast values of mixed types to the desired type.
- **Use of PHP's built-in conversion logic**: Despite its focus on providing safe typecasting, SafeCast sticks to the conversion rules of the built-in PHP casting functions. It does not introduce any conversion behavior that is inconsistent with PHP's built-in type casting.
- **Avoiding errors with PHPStan**: SafeCast helps to prevent errors when mixed values are provided to (int|float|str|bool)val() functions.

Features
--------

[](#features)

SafeCast offers simple wrappers around the PHP's native casting functions `(int|float|str|bool)val()`, with the following available functions:

- `try_(mixed $v): ?` - These functions are intended to be used when you're working with a value that might cause a casting error. They attempt to cast the given value to the specified type and return `null` if the cast is not possible.
- `to_(mixed $v): ` - These functions are intended to be used when the value is expected to be of the specific type. For example, when dealing with a setting or a programmer-determined value, use these functions. If a casting error occurs, it is treated as a programmer error and an `InvalidArgumentException` is thrown.

These functions are available for `int`, `string`, `float`, and `bool` types.

Usage
-----

[](#usage)

Here's an example of how you can use SafeCast:

```
