<?php
/**
* WeEngine System
*
* (c) We7Team 2022 <https://www.w7.cc>
*
* This is not a free software
* Using it under the license terms
* visited https://www.w7.cc for more details
*/
namespace W7\Validate;
use Closure;
use Itwmw\Validation\Factory;
use Itwmw\Validation\Support\Str;
use Itwmw\Validation\ValidationData;
use Itwmw\Validation\Support\ValidationException;
use W7\Validate\Exception\ValidateException;
use W7\Validate\Exception\ValidateRuntimeException;
use W7\Validate\Support\Common;
use W7\Validate\Support\Concerns\DefaultInterface;
use W7\Validate\Support\Concerns\FilterInterface;
use W7\Validate\Support\Concerns\MessageProviderInterface;
use W7\Validate\Support\DataAttribute;
use W7\Validate\Support\Event\ValidateEventAbstract;
use W7\Validate\Support\MessageProvider;
use Itwmw\Validation\Support\Collection\Collection;
use W7\Validate\Support\Storage\ValidateConfig;
use W7\Validate\Support\ValidateScene;
class Validate extends RuleManager
{
/**
* Global Event Handler
*
* @link https://v.neww7.com/en/4/Validate.html#event
* @var array
*/
protected $event = [];
/**
* All validated fields cannot be empty when present
*
* @link https://v.neww7.com/en/4/Validate.html#filled
* @var bool
*/
protected $filled = true;
/**
* The filter. This can be a global function name, anonymous function, etc.
*
* @link https://v.neww7.com/en/4/Validate.html#filter
* @var array
*/
protected $filter = [];
/**
* Sets the specified property to the specified default value.
*
* @link https://v.neww7.com/en/4/Validate.html#default
* @var array
*/
protected $default = [];
/**
* Event Priority
*
* @var bool
*/
private $eventPriority = true;
/**
* Events to be processed for this validate
*
* @var array
*/
private $events = [];
/**
* Methods to be executed before this validate
*
* @var array
*/
private $befores = [];
/**
* Methods to be executed after this validate
*
* @var array
*/
private $afters = [];
/**
* This validation requires a default value for the value
*
* @var array
*/
private $defaults = [];
/**
* Filters to be passed for this validation
*
* @var array
*/
private $filters = [];
/**
* Error Message Provider
*
* @var MessageProviderInterface
*/
private $messageProvider = null;
/**
* Data to be validated
*
* @var array
*/
private $checkData = [];
/**
* Data validated this time
*
* @var array
*/
private $validatedData = [];
/**
* Fields validated this time
*
* @var array
*/
private $validateFields = [];
/**
* {@inheritdoc }
*/
protected $sceneProvider = ValidateScene::class;
/**
* Create a validator
*
* @param array $rules Validation rules
* @param array $messages Error message
* @param array $customAttributes Field Name
* @return Validate
*/
public static function make(array $rules = [], array $messages = [], array $customAttributes = []): Validate
{
return (new static())->setRules($rules)->setMessages($messages)->setCustomAttributes($customAttributes);
}
/**
* Get Validator Factory
*
* @return Factory
*/
private static function getValidationFactory(): Factory
{
return ValidateConfig::instance()->getFactory();
}
/**
* Auto validate
*
* @param array $data Data to be verified
* @return array
* @throws ValidateException
*/
public function check(array $data): array
{
try {
$this->init();
$this->checkData = $data;
$this->addEvent('event', $this->event);
$this->handleEvent($data, 'beforeValidate');
$events = $this->events;
$this->events = [];
$rule = $this->getSceneRules();
$data = $this->pass($data, $rule);
$this->events = $events;
$this->handleEvent($data, 'afterValidate');
return $data;
} catch (ValidationException $e) {
$messageProvider = $this->getMessageProvider();
$messageProvider->setAttribute($e->getAttribute());
$error = $messageProvider->handleMessage($e->getMessage());
throw new ValidateException($error, 403, $e->getAttribute(), $e);
}
}
/**
* Perform data validation and processing
*
* @param array $data Data to be verified
* @param array $rules Rules for validation
* @return array
* @throws ValidateException
* @throws ValidationException
*/
private function pass(array $data, array $rules): array
{
$this->defaults = array_merge($this->default, $this->defaults);
$this->filters = array_merge($this->filter, $this->filters);
// Validated fields are not re-validated
$checkFields = array_diff(array_keys($rules), $this->validateFields);
$checkRules = array_intersect_key($rules, array_flip($checkFields));
$checkRules = $this->getCheckRules($checkRules);
$this->validateFields = array_merge($this->validateFields, $checkFields);
if ($this->filled) {
$checkRules = $this->addFilledRule($checkRules);
}
// Defaults and filters only handle the fields that are being validated now
$fields = array_keys($checkRules);
$data = $this->handleDefault($data, $fields);
if ($this->eventPriority) {
$this->handleEvent($data, 'beforeValidate');
$this->handleEventCallback($data, 'before');
} else {
$this->handleEventCallback($data, 'before');
$this->handleEvent($data, 'beforeValidate');
}
$validatedData = $this->validatedData;
if (!empty($checkRules)) {
$validatedData = $this->getValidationFactory()->make($data, $checkRules, $this->message, $this->customAttributes)->validate();
$validatedData = array_merge($this->validatedData, $validatedData);
}
if ($this->eventPriority) {
$this->handleEventCallback($validatedData, 'after');
$this->handleEvent($validatedData, 'afterValidate');
} else {
$this->handleEvent($validatedData, 'afterValidate');
$this->handleEventCallback($validatedData, 'after');
}
$validatedData = $this->handlerFilter($validatedData, $fields);
$this->initScene();
$this->scene(null);
return $validatedData;
}
/**
* Add a custom implicit validation rule
*
* @param string $rule Rule Name
* @param Closure|string|array $extension Closure rules, providing four parameters:$attribute, $value, $parameters, $validator
* @param string|null $message Error Message
* @return Validate
*/
protected function extendImplicitRule(string $rule, $extension, ?string $message = null): Validate
{
self::validatorExtend('Implicit', $rule, $extension, $message, true);
return $this;
}
/**
* Add a custom dependent validation rule.
*
* @param string $rule Rule Name
* @param Closure|string|array $extension Closure rules, providing four parameters:$attribute, $value, $parameters, $validator
* @param string|null $message Error Message
* @return Validate
*/
protected function extendDependentRule(string $rule, $extension, ?string $message = null): Validate
{
self::validatorExtend('Dependent', $rule, $extension, $message, true);
return $this;
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
w7-engine-validate-4.x.zip (62个子文件)
w7-engine-validate-4.x
composer.json 1KB
phpunit.xml 1KB
.php-cs-fixer.php 3KB
tests
Test
TestInvokeSceneCheck.php 7KB
TestValidateSceneNextAndEvent.php 7KB
TestRegexRule.php 3KB
TestValidateSceneNext.php 5KB
TestRuleManagerScene.php 3KB
Fixer
TestScene.php 5KB
TestDataDefault.php 6KB
TestRuleGroup.php 2KB
TestHandlerEvent.php 9KB
TestRuleManagerGet.php 5KB
TestDataFilter.php 6KB
TestArrayRule.php 3KB
TestHandlerFunction.php 2KB
TestCustomRuleAndMessage.php 13KB
TestCustomMessageProvider.php 2KB
TestValidateCollection.php 4KB
TestValidateMessage.php 6KB
TestValidateScene.php 10KB
Material
Count.php 1KB
UserRulesManager.php 2KB
Event
CheckIsChs.php 672B
ArticleValidate.php 2KB
Queue.php 898B
Rules
Chs.php 736B
AlphaNum.php 544B
Length.php 724B
Contain.php 611B
ChsAlphaNum.php 568B
LengthBetween.php 851B
BaseTestValidate.php 685B
TestValidate.php 929B
LICENSE 11KB
src
RuleManager.php 22KB
Providers
Laravel
ValidateProvider.php 670B
PresenceVerifier.php 2KB
Rangine
ValidateProvider.php 881B
Think
PresenceVerifier.php 2KB
ValidateService.php 658B
Support
ValidateScene.php 9KB
RuleManagerScene.php 3KB
DataAttribute.php 369B
Storage
ValidateConfig.php 4KB
Rule
RuleInterface.php 653B
BaseRule.php 2KB
Event
ValidateEventInterface.php 534B
ValidateEventAbstract.php 763B
MessageProvider.php 4KB
helpers.php 193B
Concerns
SceneInterface.php 2KB
FilterInterface.php 301B
MessageProviderInterface.php 2KB
DefaultInterface.php 342B
Common.php 2KB
Validate.php 23KB
Exception
ValidateException.php 639B
ValidateRuntimeException.php 311B
CollectionException.php 292B
.phpstorm.meta.php 401B
.gitignore 146B
共 62 条
- 1
资源评论
m0_72731342
- 粉丝: 2
- 资源: 1832
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功