-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Elie\Validator\Rule; | ||
|
||
/** | ||
* This class verifies that a value through a callable function. | ||
*/ | ||
class CallableRule extends AbstractRule | ||
{ | ||
|
||
/** | ||
* Specific message error code | ||
*/ | ||
public const INVALID_CALLABLE_CHECK = 'invalidCallableCheck'; | ||
|
||
/** | ||
* Specific option for CallableRule | ||
*/ | ||
public const CALLABLE = 'callable'; | ||
|
||
/** | ||
* @var callable _invoke|function($key, $value): bool | ||
*/ | ||
protected $callable; | ||
|
||
/** | ||
* Params could have the following structure: | ||
* [ | ||
* 'required' => {bool:optional:false by default}, | ||
* 'trim' => {bool:optional:true by default:only if value is string}, | ||
* 'messages' => {array:optional:key/value message patterns}, | ||
* 'callable' => {callable:required:should receive key/value and return boolean} | ||
* ] | ||
*/ | ||
public function __construct(string $key, $value, array $params = []) | ||
{ | ||
parent::__construct($key, $value, $params); | ||
|
||
$this->callable = $params[self::CALLABLE]; | ||
|
||
$this->messages = $this->messages + [ | ||
self::INVALID_CALLABLE_CHECK => '%key%: %value% did not pass the callable check', | ||
]; | ||
} | ||
|
||
public function validate(): int | ||
{ | ||
$run = parent::validate(); | ||
|
||
if ($run !== $this::CHECK) { | ||
return $run; | ||
} | ||
|
||
$callable = $this->callable; | ||
|
||
if (! $callable($this->key, $this->value)) { | ||
return $this->setAndReturnError($this::INVALID_CALLABLE_CHECK); | ||
} | ||
|
||
return $this::VALID; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Elie\Validator\Rule; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class CallableRuleTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @dataProvider getCallableRuleValueProvider | ||
*/ | ||
public function testValidate($value, array $params, int $expectedResult, string $expectedError): void | ||
{ | ||
$rule = new CallableRule('name', $value, $params); | ||
|
||
$res = $rule->validate(); | ||
|
||
assertThat($res, identicalTo($expectedResult)); | ||
|
||
assertThat($rule->getError(), identicalTo($expectedError)); | ||
} | ||
|
||
public function getCallableRuleValueProvider(): \Generator | ||
{ | ||
yield 'Given value could be empty when not required' => [ | ||
null, [CallableRule::CALLABLE => function ($key, $value): bool { | ||
return $value === '102'; | ||
}], CallableRule::VALID, '' | ||
]; | ||
|
||
yield 'Given value should be validated through a callable' => [ | ||
'102', [CallableRule::CALLABLE => function ($key, $value): bool { | ||
return $value === '102'; | ||
}], CallableRule::VALID, '' | ||
]; | ||
|
||
yield 'Given value should not be validated through a callable' => [ | ||
false, [CallableRule::CALLABLE => function ($key, $value): bool { | ||
return $value === null; | ||
}], CallableRule::ERROR, 'name: <FALSE> did not pass the callable check' | ||
]; | ||
} | ||
} |