star / php-type
包装基本 PHP 类型并在通用接口后面进行封装的包
2.0.0
2023-12-29 19:41 UTC
Requires
- php: ^7.0|^8.0
- ext-mbstring: *
Requires (Dev)
- phpstan/phpstan: ^1.5
- phpstan/phpstan-phpunit: ^1.1
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: *
README
基本的面向对象类,用于将 PHP 基本类型转换封装在通用接口后面。
安装
composer require star/php-type
值转换
您可以使用 ValueGuesser 将混合值转换为对象类型。
use Star\Component\Type\ValueGuesser; $int = ValueGuesser::fromMixed(12); $int->isEmpty(); // false $int->toBool(); // true $int->toDate(); // throws Exception $int->toFloat(); // 12.0 $int->toInteger(); // 12 $int->toString(); // "12"
类型钩子
当您不知道将返回哪种类型时,ValueVisitor 允许您为每种类型定义操作(如 switch 案例)。
use Star\Component\Type\BooleanValue; use Star\Component\Type\ValueVisitor; $true = BooleanValue::asTrue(); $true->acceptValueVisitor( new class implements ValueVisitor { ... public function visitFloatValue(float $value): void { // do your custom operation when the value is float. } ... } );