jasny/typecast
最新稳定版本:v2.1.1
Composer 安装命令:
composer require jasny/typecast
包简介
Type casting with basic logic
关键字:
README 文档
README
This library does type casting in PHP.
Type casting is natively supported in PHP. This library adds some basic logic to the process, like triggering a warning
when casting a string like "FOO" to an integer.
In contrary to PHP's internal type casting, casting null always results in null.
Installation
The Jasny TypeCast package is available on packagist. Install it using composer:
composer require jasny/typecast
Usage
use Jasny\TypeCast; $typecast = new TypeCast(); $typecast->to('string')->cast(null); // null $typecast->to('integer')->cast('987'); // 987 $typecast->to(DateTime::class)->cast('2015-01-01'); // new DateTime('2015-01-01) $typecast->to(FooBar::class)->cast($data); // FooBar::__set_state($data) // Unable to cast $typecast->to('float')->cast('red'); // 'red' + triggers a notice $typecast->to('int')->cast(new stdClass()); // stdClass object + triggers a notice
Alias
You can set aliases in cases where you might need to cast to an interface or abstract class or when you want to cast to a child class.
$typecast = new TypeCast(); $typecast->alias(FooBarInterface::class, FooBar::class); $typecast->to(FooBarInterface::class)->cast($data); // FooBar::__set_state($data)
Errors
By default an E_NOTICE is triggered if a value can't be casted to desired type. Jasny\TypeCast follows stricter
rules than PHP for casting values.
Instead of a notice an error of any severity can be triggered. Alternatively any Throwable like an exception or
error can be thrown.
$typecast = new TypeCast(); $typecast->failWith(E_USER_WARNING); $typecast->failWith(TypeError::class); $typecast->failWith(UnexpectedValueException::class);
Variable name in error
You can use the setName() method to set the property or variable name that is casted. This name will be included in
any error triggered when type casting. This can be useful when determining an issue.
$foo = 'red'; $typecast->value($foo)->setName('foo')->to('float');
Dependency injection
If your application supports dependency injection through containers, create a new TypeCast object and add it to the
container as a service.
The value() method will clone the TypeCast object. Settings like any aliases or custom handlers will propagate.
use Jasny\TypeCast; use Jasny\TypeCastInterface; $container = new Container([ TypeCastInterface::class => function() { $typecast = new TypeCast(); $typecast->alias(FooBarInterface::class, FooBar::class); return $typecast; } ]); $container->get(TypeCastInterface::class)->value('987')->to('integer');
Assume that Container is any PSR-11 compatible container.
Handlers
The Typecast object uses handlers to cast a value. Each handler can cast a value to a specific type. The following
handlers are defined:
- array (includes typed arrays as
string[]andDateTime[]) - boolean
- float
- integer
- number (
int|float) - mixed
- object (includes casting to a specific class)
- resource
- string
- multiple (e.g.
int|stringandstring|string[])
You may overwrite the handlers when creating the TypeCast object.
Desire
The desire method will return the handler. This is an alternative approach of using the value method. If you need to
cast multiple values to the same type, it's recommendable to get the handler once using desire.
use Jasny\TypeCast; $typecast = new TypeCast(); $typecast->desire('integer')->cast('10'); $arrayHandler = $typecast->desire('array'); foreach ($items as &$item) { $item = $arrayHandler->cast($item); }
Multiple handler
In cast multiple types are specified, the handler will try to guess the type the value should be cast in. This might
hurt performance. You may use NoTypeGuess to have the handler give an error if the type can't be determined.
use Jasny\TypeCast; $multipleHandler = new TypeCast\Handlers\MultipleHandler(new TypeCast\NoTypeGuess()); $typecast = new TypeCast(null, ['multiple' => $multipleHandler] + TypeCast::getDefaultHandlers());
jasny/typecast 适用场景与选型建议
jasny/typecast 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.17k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2015 年 08 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「type casting」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jasny/typecast 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jasny/typecast 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jasny/typecast 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A bundle providing fields for image upload with jquery upload and image cropping with jcrop for symfony2
Data Transfer Objects with Attribute Casting and Date Mutators.
A simple Symfony bundle that adds boolean form field type.
Strictly typed object hydration and value casting.
Cast currency attributes, stored as integer, to floats automatically.
A PHP Abstract Enum Class
统计信息
- 总下载量: 23.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 21
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-08-30