squirrelphp/types
Composer 安装命令:
composer require squirrelphp/types
包简介
Explicit handling of type coercions and enforcement in PHP in order to deal with unknown data in a more predictable and safe way.
README 文档
README
Explicit handling of type coercions and enforcement in PHP in order to deal with unknown data in a more predictable and safe way.
Installation
composer require squirrelphp/types
Table of contents
- Introduction
- Coercion behavior overview
- Test if value can be coerced
- Coerce a value
- Enforce a type for a value
Introduction
When getting values from a database or an API (or even just another PHP component you do not know) you have an expectation of what type of value you will get, but you might get something more or less different.
A database might give you the string "1" for a boolean, or the integer 1, and that would often be fine (many databases have no boolean type, so some coercion is often necessary). But getting the string "hello" or the integer -30 should not be fine for a boolean and most likely points to a mistake - maybe the expected type is wrong, or the wrong field in the database was retrieved.
This small component coerces and enforces types and is fully tested to behave in a predictable way. This should often be better than using other coercion methods like explicit casts (bool) / boolval(), as these will coerce any value, while this component will reject unreasonable values and throw a TypeError. It coerces less values than PHP does in coercive typing mode, as PHP accepts quite a few questionable values for legacy/BC reasons.
Coercion behavior overview
All argument flags mentioned below are set to false by default for a more conservative coercion behavior. Enable them only if it is necessary for your use case.
toBool
- Always accepts bools
- Accepts "0" and "1" strings
- Accepts 0 and 1 ints
- Only allows "" string if the argument
$allowEmptyStringis set to true - Only allows 0.0 and 1.0 floats if the argument
$allowFloatis set to true
toInt
- Always accepts ints
- Accepts floats and numeric strings without a fractional part
- Only allows bools if the argument
$allowBoolis set to true
toFloat
- Always accepts ints and floats
- Accepts numeric strings
- Only allows bools if the argument
$allowBoolis set to true
toString
- Always accepts strings
- Accepts any ints and floats
- Only allows bools if the argument
$allowBoolis set to true - Only allows Stringable objects if the argument
$allowStringableis set to true
Test if value can be coerced
All these functions have the mixed $value as their first argument and return true or false:
Coerceable::toBool
Returns true if $value is one of the following:
- A boolean
- A string with value '0' or '1'
- An int with value 0 or 1
- An empty string - only if the argument
$allowEmptyStringis set to true - A float with value 0 or 1 - only if the
$allowFloatargument is set to true
For any other values it returns false.
Coerceable::toInt
Returns true if $value is one of the following:
- An integer
- A float without fractional part
- A numeric string without fractional part
- A boolean - only if the
$allowBoolargument is set to true
For any other values it returns false.
Coerceable::toFloat
Returns true if $value is one of the following:
- An integer or float
- A numeric string
- A boolean - only if the
$allowBoolargument is set to true
For any other values it returns false.
Coerceable::toString
Returns true if $value is one of the following:
- A string
- An integer or float
- A boolean - only if the
$allowBoolargument is set to true - A Stringable object - only if the
$allowStringableargument is set to true
For any other values it returns false.
Coerceable::stringToBool
Specifically tests a string if it can be coerced to a boolean. $value must be '0' or '1'.
Coerceable::intToBool
Specifically tests an integer if it can be coerced to a boolean. $value must be 0 or 1.
Coerceable::floatToBool
Specifically tests a float if it can be coerced to a boolean. $value must be 0 or 1.
Coerceable::floatToInt
Specifically tests a float if it can be coerced to an integer. $value must not have a fractional part.
Coerceable::stringToInt
Specifically tests a string if it can be coerced to an integer. $value must be a numeric string and not have a fractional part.
Coerceable::stringToFloat
Specifically tests a string if it can be coerced to a float. $value must be a numeric string.
Coerce a value
All these functions have the mixed $value as their first argument and return the type they are coercing (or throw a TypeError).
Coerce::toBool
Returns a boolean if the given value is coerceable, see Coerceable::toBool for valid values, or a TypeError if the value is not coerceable.
Coerce::toInt
Returns an integer if the given value is coerceable, see Coerceable::toInt for valid values, or a TypeError if the value is not coerceable.
Coerce::toFloat
Returns a float if the given value is coerceable, see Coerceable::toFloat for valid values, or a TypeError if the value is not coerceable.
Coerce::toString
Returns a string if the given value is coerceable, see Coerceable::toString for valid values, or a TypeError if the value is not coerceable.
Coerce::stringToBool
Returns a boolean if the given string is coerceable, see Coerceable::stringToBool for valid values, or a TypeError if the value is not coerceable.
Coerce::intToBool
Returns a boolean if the given int is coerceable, see Coerceable::intToBool for valid values, or a TypeError if the value is not coerceable.
Coerce::floatToBool
Returns a boolean if the given float is coerceable, see Coerceable::floatToBool for valid values, or a TypeError if the value is not coerceable.
Coerce::floatToInt
Returns an integer if the given float is coerceable, see Coerceable::floatToInt for valid values, or a TypeError if the value is not coerceable.
Coerce::stringToInt
Returns an integer if the given string is coerceable, see Coerceable::stringToInt for valid values, or a TypeError if the value is not coerceable.
Coerce::stringToFloat
Returns a float if the given string is coerceable, see Coerceable::stringToFloat for valid values, or a TypeError if the value is not coerceable.
Coerce::boolToString
Coerces a boolean to a string, returning either '0' or '1'.
Coerce::intToString
Coerces an integer to a string, returning a numeric string.
Coerce::floatToString
Coerces a float to a string, returning a numeric string.
Enforce a type for a value
All these functions have the mixed $value as their only argument and return the type they are enforcing, according to the same logic as strict mode in PHP.
Enforce::asBool
Returns $value as a boolean if it is a boolean. Throws a TypeError otherwise.
Enforce::asInt
Returns $value as an integer if it is an integer. Throws a TypeError otherwise.
Enforce::asFloat
Returns $value as a float if it is an integer or a float. Throws a TypeError otherwise.
Enforce::asString
Returns $value as a string if it is a string. Throws a TypeError otherwise.
squirrelphp/types 适用场景与选型建议
squirrelphp/types 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.21k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 06 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「input」 「types」 「data processing」 「Enforcement」 「coercions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 squirrelphp/types 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 squirrelphp/types 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 squirrelphp/types 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Yii2 map input widget. Allows you to select geographcal coordinates via a human-friendly inteface.
Widget to add a remaining character counter to text inputs and textareas
Data type classes for PHP
Easily switch between entry types in entries section
Native (browser) color input field for SilverStripe
Integration bundle for Aura.Input with Aura.Filter
统计信息
- 总下载量: 1.21k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 17
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-10