Skip to content

Commit

Permalink
feat(IsCallback): 增加 getValidator 方法,可用于回调中获取当前校验器的信息
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Dec 9, 2020
1 parent 0202591 commit c3718cb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/IsCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class IsCallback extends BaseValidator
* Check if the input is valid by specified callback
*
* @param mixed $input The input value
* @param callable|null $fn The callback to validate the input
* @param callable|null $fn The callback to validate the input
* @param string|null $message The custom invalid message
* @return bool
*/
Expand All @@ -52,6 +52,16 @@ public function setMessage($message)
return $this;
}

/**
* Returns the validator instance
*
* @return Validate|null
*/
public function getValidator()
{
return $this->validator;
}

/**
* {@inheritdoc}
*/
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/IsCallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace WeiTest;

use Wei\IsCallback;
use Wei\V;
use Wei\Validate;

/**
* @internal
*/
Expand Down Expand Up @@ -49,6 +53,26 @@ public function testArrayCallback()
$this->assertFalse($result);
}

public function testGetValidator()
{
$validator = wei()->isCallback->getValidator();
$this->assertNull($validator);
}

public function testGetValidatorFromV()
{
$validator = null;
V
::key('test')->callback(function ($input, IsCallback $callback) use (&$validator) {
$validator = $callback->getValidator();
return false;
})
->check([
'test' => 1,
]);
$this->assertInstanceOf(Validate::class, $validator);
}

public function arrayCallback()
{
return false;
Expand Down

0 comments on commit c3718cb

Please sign in to comment.