yarco / type-detector
TypeDetector is php library to detect variable types by the name and value
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 is php library to detect variable types by the key name and the value. The thinkings inherit from superunit and php-schema ( both are my experimental projects ).
Usage
Use composer to install this library.
composer require yarco/type-detector
Use the class's static method array TypeDetector::detect(array $array)
to get key-value pair of the types.
Example:
use Yarco\TypeDetector; $result = TypeDetector::detect(['name' => 'yarco', 'age' => 36]); print_r($result); /* it should return Array ( [name] => string [age] => smallint ) */
Types
Below fields are referenced from w3schools.com.
Number
MySQL Data Types | SQL Server Data Types | HTML Input Types | Doctrine Types | Type Detector Supported |
---|---|---|---|---|
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 |
Text
MySQL Data Types | SQL Server Data Types | HTML Input Types | Doctrine Types | Type Detector Supported |
---|---|---|---|---|
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 |
Datetime
MySQL Data Types | SQL Server Data Types | HTML Input Types | Doctrine Types | Type Detector Supported |
---|---|---|---|---|
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 |
Other
MySQL Data Types | SQL Server Data Types | HTML Input Types | Doctrine Types | Type Detector Supported |
---|
| uniqueidentifier | | guid
JSON | | checkbox |
Valid Type Definitions
Type Detector Supported Types | Definition | Priority (large first) ----|----|----|---- smallint | type is integer and value less than 100 | 120 bigint | type is integer and value large than 10000 | 120 int | type is integer | 115 decimal | type is float and number of the fraction is either 2 or 3 | 112 float | type is float | 110 password | type is string and value is *** | 100 color | type is string and value match ^#[0-9a-fA-F]{6}$ | 90 email | type is string and value is email | 80 url | type is string and value is url | 70 date | type is string and value match ^\d{2}/\d{2}/\d{4}$ or ^\d{4}-\d{2}-\d{2}$ | 60 time | type is string and value match ^\d{2}:\d{2}:\d{2}$ or ^\d{2}:\d{2}$ | 60 datetime | type is tring and value match ^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}(?::\d{2})?$ or can be convert to datetime | 55 string | type is string and length less than 27 | 20 text | type is tring | 0