twinkle/dto-validation

There is no license information available for the latest version (v1.0.0) of this package.

dto验证器

v1.0.0 2020-01-14 13:06 UTC

This package is auto-updated.

Last update: 2024-04-15 19:54:39 UTC


README

dto 较验

Build Status

日常工作中,跟第三方系统对接是常有的事,而这些系统可能并不是同一种语言实现的,因为语言特性不同,总会遇到意想不到的事情。
比如 php 是弱类型语言,'1' 与 1 是没有区别的,但是在 java 中,这两个就并不相同了。比如 null 与 '' 在 php 中也是相等的,但是在其他语言中,赋值就可能报错。

这个库主要就是满足这些需要。

使用

使用前必须将规则较验实例注入到容器

$container = new \Twinkle\DI\Container([
    'Validate:Required' => \twinkle\dto\validation\annotation\Required::class,
    'Validate:Type' => \twinkle\dto\validation\annotation\Type::class,
    'Validate:Enum' => \twinkle\dto\validation\annotation\Enum::class,
]);
\Twinkle\DI\Tools::setContainer($container);

示例

/**
 * @Validate # 是否需要较验
 * @var integer # 声明类型
 * @var integer autoConvert=integer # 声明类型,并且尝试自动转换
 * @Required() # 必须字段
 * @Required(default=1) # 字段必须,在字段为null的情况下设置默认值
 * @Enum(1,2,3) # 字段值必须为1,2,3中的一个
 */

Note

为了更好的开发体验,强列建议安装 annotations 插件
phpstorm(http://plugins.jetbrains.com/plugin/7320-php-annotations)