estasi / validator
数据验证的验证类,以及将验证器链接起来创建复杂验证链的能力
1.2.0
2020-06-08 08:30 UTC
Requires
- php: ^7.4
- ext-intl: *
- ext-mbstring: *
- estasi/plugin-manager: ^1.0
- estasi/utility: ^1.1
- php-ds/php-ds: dev-master
Requires (Dev)
- phpunit/phpunit: ^7.0.1
Suggests
- estasi/translator: ^0 || ^1 translations of validator error messages
- laminas/laminas-validator: ^2.5 extends the data validation capabilities of the main validator using Laminas validators
- symfony/validator: ^5.0 extends the data validation capabilities of the main validator using Symfony validators
This package is auto-updated.
Last update: 2024-09-08 18:52:23 UTC
README
这是一组必要的不可变验证器。提供了一种简单的数据验证链机制,允许您以给定的顺序应用多个验证器。
安装
使用composer安装
composer require estasi/validator
要求
- PHP 7.4 或更高版本
- ext-mbstring
- ext-intl
- 数据结构:
composer require php-ds/php-ds
Polyfill 会与 estasi/validator 包一起安装。
用法
基本用法
<?php declare(strict_types=1); use Estasi\Validator\Email; $email = 'john@doe.com'; $validator = new Email(Email::ALLOW_UNICODE); if ($validator->isValid($email)) { // your code is here } else { // print "Email "$email" is not correct!" echo $validator->getLastErrorMessage(); }
自定义消息
<?php declare(strict_types=1); use Estasi\Validator\Email; $email = 'john@doe.com'; $validator = new Email(Email::ALLOW_UNICODE, [Email::E_INVALID_EMAIL => 'Custom error message.']); if ($validator->isValid($email)) { // your code is here } else { // print "Custom error message." echo $validator->getLastErrorMessage(); }
或
<?php declare(strict_types=1); use Estasi\Validator\Email; $email = 'john@doe.com'; $validator = new Email(Email::ALLOW_UNICODE); if ($validator->isValid($email)) { // your code is here } else { if ($validator->isLastError(Email::E_INVALID_EMAIL)) { echo "Custom error message."; // or your other code depending on the error } //... }
相同
只有相同的验证器可以在 isValid()、notValid() 和 __invoke() 方法中接受第二个参数
简单比较
<?php declare(strict_types=1); use Estasi\Validator\Identical; $token = 'string'; $value = 'string'; $validator = new Identical($token, Identical::STRICT_IDENTITY_VERIFICATION); if ($validator->isValid($value)) { // your code is here }
或
<?php declare(strict_types=1); use Estasi\Validator\Identical; $context = 'string'; $value = 'string'; $validator = new Identical(null, Identical::STRICT_IDENTITY_VERIFICATION); if ($validator->isValid($value, $context)) { // your code is here }
与数组中的值比较
<?php declare(strict_types=1); use Estasi\Validator\Identical; $token = 'email'; $value = 'john@doe.com'; $context = ['names' => ['firstname' => 'John', 'lastname' => 'Doe'], 'email' => 'john@doe.com']; $validator = new Identical($token, Identical::STRICT_IDENTITY_VERIFICATION); if ($validator->isValid($value, $context)) { // your code is here }
如果上下文是数组,您还可以检查不受限制的嵌套上下文的值。因此,嵌套分隔符将是符号 "."。
<?php declare(strict_types=1); use Estasi\Validator\Identical; $token = 'names.lastname'; $value = 'Doe'; $context = ['names' => ['firstname' => 'John', 'lastname' => 'Doe'], 'email' => 'john@doe.com']; $validator = new Identical($token, Identical::STRICT_IDENTITY_VERIFICATION); if ($validator->isValid($value, $context)) { // your code is here }
链
以下是链中的两个验证器任务:显式(通过类声明)和通过工厂(数组)
<?php declare(strict_types=1); use Estasi\Validator\{Chain,Identical,Regex}; $datum = [ 'password' => [ 'original' => 'password_25', 'confirm' => 'password_25' ] ]; $chain = new Chain(); $chain = $chain->attach( new Regex('[A-Za-z0-9_]{8,12}', Regex::OFFSET_ZERO, [Regex::OPT_ERROR_VALUE_OBSCURED => true]), Chain::WITH_BREAK_ON_FAILURE ) ->attach( [ Chain::VALIDATOR_NAME => 'identical', Chain::VALIDATOR_OPTIONS => [ Identical::OPT_TOKEN => 'password.original', Identical::OPT_ERROR_VALUE_OBSCURED => true ] ], Chain::WITH_BREAK_ON_FAILURE ); if($chain->isValid($datum['password']['original'], $datum)) { // your code is here }
许可证
本包所有内容均受BSD-3-Clause 许可证许可。