alexpts/php-typecast
Composer 安装命令:
composer require alexpts/php-typecast
包简介
Simple type cast request data
README 文档
README
Cast types
Install
composer require alexpts/php-typecast
Example
$body = [ 'title' => 'Some', 'user' => [ 'name' => 'Alex' 'age' => '29', 'isAdmin' = 'false', ], 'friendsIds' => ['1', '2', '3', 4], 'date' => '11-12-2017', 'date2' => '11-12-2017', ]; $caster = new TypeCast(new DeepArray); // shot format $data = $caster->cast($body, [ 'friendsIds' => ['array', ['each' => ['int']]], 'title' => ['string'], 'user' => ['array'], 'user.name' => ['string'], 'user.age' => ['int'], 'user.isAdmin' => ['bool'], 'date' => ['datetime'], 'date2' => ['datetime', ['datetimeFormat' => ['Y-m-d']]], // full format ]); /* $data ==== [ 'title' => 'Some', 'user' => [ 'name' => 'Alex' 'age' => 29, 'isAdmin' = false, ], 'friendsIds' => [1, 2, 3, 4], 'date' => new \DateTime('11-12-2017'), 'date2' => '2017-12-11', ]; */
Types:
string
Set type to string
bool
Set type to boolean
int
Set type to int
array
Set type to array
float
Set type to float
object
Set type to float
null
Set type to null
numbers
Remove all not number symbols from string
each
Convert each array item to describe types (see example above)
datetime
Convert value to \DateTime object with default timezone
datetimeFormat
Convert \DateTime object to string via format method
Custom convert type
You can add custom convert via method on TypeCast service
public function registerType(string $name, callable $handler): self
统计信息
- 总下载量: 126
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-09-17