diff --git a/.gitignore b/.gitignore index 76a40980b440a32e3c74f66460702a20a0b54c96..ae2b966f76002225bf0b817504201e7a2a99bd0b 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ runtime/ /composer.lock /coverage/ /cache/ +.phpunit.result.cache \ No newline at end of file diff --git a/src/Validate.php b/src/Validate.php index f52772035f3c305c747d7c990e1fe2798b50fe1b..6bfe99cb6096f7d76aa5e96f9e004406d94d1cfd 100644 --- a/src/Validate.php +++ b/src/Validate.php @@ -174,7 +174,7 @@ class Validate extends RuleManager try { $this->init(); $this->checkData = $data; - $this->addEvent($this->event); + $this->addEvent('event', $this->event); $this->handleEvent($data, 'beforeValidate'); $events = $this->events; $this->events = []; @@ -286,7 +286,7 @@ class Validate extends RuleManager try { $this->init(); $this->checkData = $data; - $this->addEvent($this->event); + $this->addEvent('event', $this->event); $this->handleEvent($data, 'beforeValidate'); $events = $this->events; $this->events = []; @@ -357,25 +357,12 @@ class Validate extends RuleManager if (isset($this->scene[$sceneName])) { $sceneRule = $this->scene[$sceneName]; - // Determine if an event is defined - if (isset($sceneRule['event'])) { - $events = $sceneRule['event']; - $this->addEvent($events); - unset($sceneRule['event']); - } - - // Methods to be executed before determining the presence or absence of authentication - if (isset($sceneRule['before'])) { - $callback = $sceneRule['before']; - $this->addBefore($callback); - unset($sceneRule['before']); - } - - // Methods to be executed after determining the existence of validation - if (isset($sceneRule['after'])) { - $callback = $sceneRule['after']; - $this->addAfter($callback); - unset($sceneRule['after']); + foreach (['event', 'before', 'after'] as $eventType) { + if (isset($sceneRule[$eventType])) { + $callback = $sceneRule[$eventType]; + $this->addEvent($eventType, $callback); + unset($sceneRule[$eventType]); + } } if (isset($sceneRule['next']) && !empty($sceneRule['next'])) { @@ -707,67 +694,30 @@ class Validate extends RuleManager /** * Add Event * - * @param $handlers - */ - private function addEvent($handlers) - { - $this->addCallback(0, $handlers); - } - - /** - * Methods to be executed before adding validation - * - * @param $callback - */ - private function addBefore($callback) - { - $this->addCallback(1, $callback); - } - - /** - * Add the method that needs to be executed after verification - * - * @param $callback + * @param string $type 'event','before','after + * @param string|array $callback */ - private function addAfter($callback) + private function addEvent(string $type, $callback) { - $this->addCallback(2, $callback); - } - - /** - * Add method - * - * @param int $intType 0 event 1 before 2 after - * @param $callback - */ - private function addCallback(int $intType, $callback) - { - switch ($intType) { - case 0: - $type = 'events'; - break; - case 1: - $type = 'befores'; - break; - case 2: - $type = 'afters'; - break; - } + $type .= 's'; + $callbacks = $this->$type; if (is_string($callback)) { - $this->$type[] = [$callback, []]; + $callbacks[] = [$callback, []]; } else { foreach ($callback as $classOrMethod => $param) { if (is_int($classOrMethod)) { - $this->$type[] = [$param, []]; + $callbacks[] = [$param, []]; } elseif (is_string($classOrMethod)) { if (is_array($param)) { - $this->$type[] = [$classOrMethod, $param]; + $callbacks[] = [$classOrMethod, $param]; } else { - $this->$type[] = [$classOrMethod, [$param]]; + $callbacks[] = [$classOrMethod, [$param]]; } } } } + + $this->$type = $callbacks; } }