yarco / type-detector
TypeDetector 是一个 PHP 库,通过名称和值检测变量类型。
Requires
- php: ^7.0
Requires (Dev)
- phpunit/phpunit: ^5.6
This package is auto-updated.
Last update: 2024-03-01 08:08:35 UTC
README
TypeDetector 是一个 PHP 库,通过键名和值检测变量类型。其思路继承自 superunit 和 php-schema(都是我的实验性项目)。
用法
使用 composer 安装此库。
composer require yarco/type-detector
使用类的静态方法 array TypeDetector::detect(array $array)
获取类型的关键字值对。
示例
use Yarco\TypeDetector; $result = TypeDetector::detect(['name' => 'yarco', 'age' => 36]); print_r($result); /* it should return Array ( [name] => string [age] => smallint ) */
类型
以下字段参考自 w3schools.com。
数字
MySQL 数据类型 | SQL Server 数据类型 | HTML 输入类型 | Doctrine 类型 | 类型检测器支持 |
---|---|---|---|---|
TINYINT(size) | tinyint | |||
SMALLINT(size) | smallint | smallint | smallint | |
MEDIUMINT(size) | ||||
INT(size) | int | range, number | integer | int |
BIGINT(size) | bigint | bigint | bigint | |
FLOAT(size,d) | real | |||
DOUBLE(size,d) | float(n) | float | float | |
DECIMAL(size,d) | decimal(p,s) | decimal | decimal | |
numeric(p,s) | ||||
smallmoney | ||||
money | ||||
bit | boolean |
文本
MySQL 数据类型 | SQL Server 数据类型 | HTML 输入类型 | Doctrine 类型 | 类型检测器支持 |
---|---|---|---|---|
CHAR(size) | char(n), nchar(n) | |||
VARCHAR(size) | varchar(n), nvarchar(n) | text | string | string |
TINYTEXT | ||||
TEXT | text, ntext | textarea | text | text |
BLOB | binary, varbinary | binary | ||
MEDIUMTEXT | ||||
MEDIUMBLOB | ||||
LONGTEXT | ||||
LONGBLOB | image | blob | ||
ENUM(x,y,z,etc.) | select | |||
SET | ||||
password | password | |||
color | color | |||
search | ||||
tel | ||||
url | url |
日期时间
MySQL 数据类型 | SQL Server 数据类型 | HTML 输入类型 | Doctrine 类型 | 类型检测器支持 |
---|---|---|---|---|
DATE | date | date | date | date |
DATETIME | datetime, datetime2, smalldatetime | datetime, datetime-local | datetime | datetime |
TIMESTAMP | timestamp | |||
TIME | time | time | time | time |
YEAR | month,week | |||
datetimeoffset | datetimetz |
其他
MySQL 数据类型 | SQL Server 数据类型 | HTML 输入类型 | Doctrine 类型 | 类型检测器支持 |
---|
| uniqueidentifier | | guid
JSON | | checkbox |
有效的类型定义
类型检测器支持类型 | 定义 | 优先级(越大越优先) ----|----|----|---- smallint | 类型为整数且值小于 100 | 120 bigint | 类型为整数且值大于 10000 | 120 int | 类型为整数 | 115 decimal | 类型为浮点数且小数位数为 2 或 3 | 112 float | 类型为浮点数 | 110 password | 类型为字符串且值为 *** | 100 color | 类型为字符串且值匹配 ^#[0-9a-fA-F]{6}$ | 90 email | 类型为字符串且值为 email | 80 url | 类型为字符串且值为 url | 70 date | 类型为字符串且值匹配 ^\d{2}/\d{2}/\d{4}$ 或 ^\d{4}-\d{2}-\d{2}$ | 60 time | 类型为字符串且值匹配 ^\d{2}:\d{2}:\d{2}$ 或 ^\d{2}:\d{2}$ | 60 datetime | 类型为 tring 且值匹配 ^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}(?::\d{2})?$ 或可以转换为 datetime | 55 string | 类型为字符串且长度小于 27 | 20 text | 类型为 tring | 0