healthengine / coerce
Try to coerce mixed values into desired types or die trying
v1.0.2
2024-06-28 02:03 UTC
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.26
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/phpunit: ^10.3
README
About
Coerce mixed
value into a desired scalar type or die trying.
Install
composer require healthengine/coerce
Usage
<?php declare(strict_types=1); use Healthengine\Coerce\Coerce; use Healthengine\Coerce\CouldNotCoerceException; use stdClass; Coerce::toBool(1); // true Coerce::toBoolOrNull(null) // null Coerce::toInt('1'); // 1 Coerce::toInt(new stdClass()); // CouldNotCoerceException Coerce::toIntOrNull(null) // null Coerce::toIntOrNull(new stdClass()); // CouldNotCoerceException Coerce::toNonEmptyString('123'); // '123' Coerce::toNonEmptyString(''); // CouldNotCoerceException Coerce::toString(1); // '1' Coerce::toString([]) // CouldNotCoerceException Coerce::toStringOrNull(null); // null Coerce::toStringOrNull([]) // CouldNotCoerceException