jrfnl/php-cast-to-type
Composer 安装命令:
composer require jrfnl/php-cast-to-type
包简介
PHP Class to consistently cast variables to a specific type.
README 文档
README
PHP-cast-to-type
PHP Class to consistently cast variables to a specific type.
Returns either the value in the specified type or null.
Features:
- Consistent results across PHP versions.
- Compatible with PHP4, PHP5 and PHP7 which makes it extra useful if you're coding for open source software where you don't know the library user's PHP version and the
filter_var()functions may not be available. - Optionally recursively cast all values in an array to the choosen type (similar to
filter_var_array()behaviour). - Optionally allow/disallow empty strings/arrays.
- Will succesfully cast any of the following string values to their boolean counterpart (similar to
filter_var()behaviour, but less case-sensitive).- True:
'1', 'true', 'True', 'TRUE', 'y', 'Y', 'yes', 'Yes', 'YES', 'on', 'On', 'ON'. - False:
'0', 'false', 'False', 'FALSE', 'n', 'N', 'no', 'No', 'NO', 'off', 'Off', 'OFF'.
- True:
- Support for casting of
SplTypeobjects.
Some Usage examples:
$value = 'example'; $value = CastToType::_bool( $value ); // null $value = 'true'; $value = CastToType::_bool( $value ); // true $value = '123'; $value = CastToType::_int( $value ); // 123 $value = array( '123' ); $value = CastToType::_int( $value ); // null $value = CastToType::_int( $value, $array2null = false ); // array( 123 )
Available methods:
All methods are static.
-
CastToType::cast( $value, $type, $array2null = true, $allow_empty = true, $implode_array = false ); -
CastToType::_bool( $value, $array2null = true, $allow_empty = true ); -
CastToType::_int( $value, $array2null = true, $allow_empty = true ); -
CastToType::_float( $value, $array2null = true, $allow_empty = true ); -
CastToType::_string( $value, $array2null = true, $allow_empty = true ); -
CastToType::_array( $value, $allow_empty = true ); -
CastToType::_object( $value, $allow_empty = true ); -
CastToType::_null( $value );
Parameters:
| Param | Type | Description |
|---|---|---|
$value |
mixed | Value to cast. |
$type |
string | Type to cast to. Valid values: 'bool', 'boolean', 'int', 'integer', 'float', 'double', 'num', 'string', 'array', 'object'. |
$array2null |
bool | Optional. Whether to return null for arrays when casting to bool, int, float, num or string. If false, the individual values held in the array will recursively be cast to the specified type. Defaults to true. |
$allow_empty |
bool | Optional. Whether to allow empty strings, empty arrays, empty objects. If false, null will be returned instead of the empty string/array/object. Defaults to true. |
Installation
Requirements
This package requires the PHP native ctype extension.
Stand-alone
- Head to the Releases page and download the latest release zip.
- Extract the files and place them somewhere in your project hierarchy.
- Require the class loader using
require_once '/path/to/cast-to-type.php';.
Composer
If you are using PHP5+ (as you should), PHP-Cast-to_Type is also available as a package installable via Composer:
composer require jrfnl/PHP-cast-to-type
Changelog:
2.1.0 (Nov 2022)
- Allow for
doubleas an alias forfloatin theCastToType::cast()method. Thanks @nsrosenqvist for the contribution. - Bug fix: string
ONnot recognized as truthy when casting to boolean. - The requirement for the
ctypeextension has been made explicit. - The primary branch has been renamed from
mastertomain. - General housekeeping.
2.0.1 (Jan 2018)
- Bugfix for PHP cross-version compatibility. This affected use of this class on PHP < 5.2.7.
- General housekeeping.
2.0 (Jun 2015)
- Updated the object casting to be in line with the way this is done in PHP7 for cross-version compatibility.
Previously arrays with numerical keys cast to objects would be added to the object as a property called
arraywith the value being the complete array. Now - as in PHP7 - each numerical array key will be cast to an individual property. This breaks backward-compatibility with v1.0 for array to object cast results, so please review your code if you relied on the old behaviour. - Fixed a bug in the object casting which would return
nullfor non-objects cast to objects in PHP <= 5.1. - Fixed a bug in the object casting where an empty string would not return
nullwhile$allow_emptywas set tofalse.
1.0 (2006 / Sept 2013)
- Initial release.
jrfnl/php-cast-to-type 适用场景与选型建议
jrfnl/php-cast-to-type 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 260.43k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2015 年 06 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「type casting」 「type juggling」 「cross version」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jrfnl/php-cast-to-type 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jrfnl/php-cast-to-type 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jrfnl/php-cast-to-type 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A bundle providing fields for image upload with jquery upload and image cropping with jcrop for symfony2
The base model traits of Esensi
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.
统计信息
- 总下载量: 260.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 23
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2015-06-17