andresmeireles / respectannotation
使用Respect Validation验证作为doctrine实体的注解
2.0
2024-04-11 14:15 UTC
Requires
- php: ^8.1
- doctrine/annotations: ^1.6
- respect/validation: ^1.1
Requires (Dev)
- phpunit/phpunit: ^11.1
- squizlabs/php_codesniffer: ^3
README
RESPECT ANNOTATIONS
阅读开发者路线图时,其中一步提到,学习的好方法是创建和分发你正在学习的语言的包。但我从未觉得有什么好的东西可以提供。然后在工作中遇到的一个项目中,有使用Respect验证器在Doctrine实体中的需求,完成后觉得将其作为包发布是个不错的想法。
安装
通过Composer
$ composer require andresmeireles/respectannotation
使用方法
在具有公共属性的实体中
<?php declare(strict_types = 1); [..] use Andresmeireles\RespectAnnotation\ValidationAnnotation as Respect; class EntityX { /** * @Respect(rules={"noBlank"}) * Outras anotações do DOCTRINE */ public $name }
在具有private或protected属性的情况下。为了验证变量值,需要提供一个getter
<?php declare(strict_types = 1); [..] use Andresmeireles\RespectAnnotation\ValidationAnnotation as Respect; class EntityX { /** * @Respect(rules={"noBlank"}) * Outras anotações do DOCTRINE */ private $name public getName() { return $this->name; } }
将注解放置在适当的位置后,要验证并返回错误,请创建一个RespectValidationAnnotation
实例,并作为参数发送一个包含有效注解的对象。
$entity = new EntityX();
$entity->setName('andre');
$validator = new RespectValidationAnnotation()
$validator->executeClassValidation($entity)
可选验证器和非验证器
要使用optional和非验证器,需要创建包含相应规则的键。
Optival参数
<?php declare(strict_types = 1); [..] use Andresmeireles\RespectAnnotation\ValidationAnnotation as Respect; class EntityX { /** * @Respect(optrules={"noWhitespace"}) * Outras anotações do DOCTRINE */ private $name public getName() { return $this->name; } }
Not参数
<?php declare(strict_types = 1); [..] use Andresmeireles\RespectAnnotation\ValidationAnnotation as Respect; class EntityX { /** * @Respect(notrules={"noWhitespace"}) * Outras anotações do DOCTRINE */ private $name public getName() { return $this->name; } }
验证实体的过程保持不变。
测试
$ composer test
限制
- 目前无法使用一些更复杂的验证器类型,如sf或zend。
- 无法在同一个键中同时使用optional和非验证器。例如:
not(optional("noWhitespace"))
贡献
TBD
行为准则
行为准则.
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。