romagny13 / php-validator
此包最新版本(0.1.0)没有可用的许可证信息。
简单的PHP验证器
0.1.0
2017-05-07 16:49 UTC
Requires (Dev)
- phpunit/phpunit: ^5.7
This package is not auto-updated.
Last update: 2024-09-20 19:37:32 UTC
README
安装
composer require romagny13\php-validator
演示
验证类(扩展验证基类)=> 带消息(错误消息)和 __invoke(魔术方法)来验证接收到的值
- RequiredValidation
- MinLengthValidation
- MaxLengthValidation
- PatternValidation
- EmailValidation
- CustomValidation
服务:
- ValidationStrategy(实现 ValidationStrategyInterface)=> 返回验证类实例(RequiredValidation,MinLengthValidation等)
- ValidationService(实现 ValidationServiceInterface)=> 允许通过模型注册验证并验证模型值
辅助工具:
- Validations => 提供快捷方式(静态函数)来创建 Validations 实例(required,minLength,maxLength,pattern,email,custom)
- Validator => 允许轻松(静态函数)使用验证对模型进行验证(valide model 和 validateValue)
示例
<?php use PHPValidator\Helpers\Validations; use PHPValidator\Helpers\Validator; require __DIR__.'/../vendor/autoload.php'; $password = 'abc'; $model = [ 'username' => '', 'email' => 'abc', 'password' => $password, 'confirm_password' => 'xyz' ]; $validations = [ 'username' => [Validations::required(), Validations::minLength()], 'email' => [Validations::required(), Validations::email()], 'password' => [Validations::required('Please enter a password')], 'confirm_password' => [Validations::custom(function($value) use($password){ return $value === $password; },'Password and confirm password do not match.')] ]; $result = Validator::validateModel($validations, $model); var_dump($result->hasError, $result->errors);
扩展
创建一个继承自 'Validation' 的类。
class NumberValidation extends Validation { public function __construct($message) { $this->message = $message; } public function __invoke($value) { // if has no value => don't evaluate (this is the job of required validation) if(!$this->hasValue($value)){ return true; } return is_numeric($value); } }
扩展 Validators 类 辅助
class MyValidations extends Validations { public static function isNumeric($message='Please fix this field.'){ return new NumberValidation($message); } }
使用(以年龄为例)
$model = [ 'username' => 'abcdefg', 'age' => 'my age' ]; $validations = [ 'username' => [Validations::required(), Validations::minLength()], 'age' => [Validations::required(), MyValidations::isNumeric()], ]; $result = Validator::validateModel($validations, $model);
ValidationService
使用服务类更好地使用验证,例如,代码更易于测试。
示例
创建一个服务,该服务接收 ValidationService 以使用。
class MyApplicationValidationService { protected $validationService; protected $strategy; public function __construct(ValidationServiceInterface $validationService, ValidationStrategyInterface $strategy) { // allows to register validations by models and validate model values $this->validationService = $validationService; // returns Validation class instances (RequiredValidation, MinLengthValidation, etc.) $this->strategy = $strategy; // init $this->addPostValidations(); // post // other model Validations (example category, user, ....) } public function addPostValidations(){ $validations = [ 'title' => [$this->strategy->required('Please enter a title'),$this->strategy->minLength()], 'content' => [$this->strategy->required()] ]; $this->validationService->addValidations('post',$validations); } public function validatePost($post){ return $this->validationService->validate('post',$post); } // other models validation functions ... }
使用
// inject the services $service = new MyApplicationValidationService(new ValidationService(), new ValidationStrategy()); // the model to validate $post = [ 'title' => '', 'content' => 'My content' ]; // and finally validate with the service $result = $service->validatePost($post); // var_dump($result->hasError,$result->errors); // easy to inject the errors in view // for this example we have an error => $result->errors['title'] = 'This field is required.'