emma / validation
PHP 8.0+ 的基于属性的验证,适用于对象/实体/DTO 属性。包含内置的数据类型转换器,可以实施以便在不损害对象的情况下简化验证。您可以通过实现一个简单的接口来创建自己的自定义验证器或转换器。
v1.0.5
2024-01-12 21:46 UTC
Requires
- php: >=8.2
- emma/common: ^1.0.2
Requires (Dev)
- emma/common: ^1.0.0
README
A PHP 8.2+ Attribute Based Valdiation For Properties of Object/Entity/DTO. Comes with in-built data-type converter that can be implmented to make validation easy without compromising your object. You can create your own custom Validators and/or Converter by implmenting a simple interface.
#示例
<?php
class LoginAttempt
{
/**
* @var int|null
*/
#[Min(1)]
protected ?int $id = null;
/**
* @var string|null
*/
#[Required]
#[AlphaNumeric]
protected ?string $username = null;
/**
* @var string|null
*/
#[Required]
#[Email]
protected ?string $email = null;
/**
* @var string|null
*/
#[Required]
#[AlphaNumeric]
protected ?string $computername = null;
/**
* @var string|null
*/
#[Required]
#[IpAddress]
protected ?string $ipaddress = null;
/**
* @var \DateTime|null
*/
#[Required]
#[DateTimeFormat]
protected ?\DateTime $time_created = null;
}
//Let's Validate this object
$loginAttempt = new LoginAttempt();
$violations = ValidationFactory::validate($loginAttempt);
if ($violations->valid()) {
var_dump($violations->getArrayCopy());
}
这很简单吧!没有复杂性,没有压力,只需使用 PHP 8 属性轻松验证对象,没有任何麻烦。享受吧。