jrfnl/php-cast-to-type 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

GitHub license CS Lint

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'.
  • Support for casting of SplType objects.

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

  1. Head to the Releases page and download the latest release zip.
  2. Extract the files and place them somewhere in your project hierarchy.
  3. 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 double as an alias for float in the CastToType::cast() method. Thanks @nsrosenqvist for the contribution.
  • Bug fix: string ON not recognized as truthy when casting to boolean.
  • The requirement for the ctype extension has been made explicit.
  • The primary branch has been renamed from master to main.
  • 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 array with 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 null for non-objects cast to objects in PHP <= 5.1.
  • Fixed a bug in the object casting where an empty string would not return null while $allow_empty was set to false.

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 jrfnl/php-cast-to-type 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 260.43k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 23
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 13
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2015-06-17