healthengine / coerce
尝试将混合值强制转换为所需类型,或者尝试失败
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
关于
将混合值强制转换为所需的标量类型,或者尝试失败。
安装
composer require healthengine/coerce
用法
<?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