Skip to content

Commit

Permalink
Add callable validator rule #22
Browse files Browse the repository at this point in the history
  • Loading branch information
elie29 committed Oct 10, 2019
1 parent 5b27698 commit 4f3f0a8
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ All notable changes to this project will be documented in this file, in reverse

### Added

- Nothing.
- [#22](https://github.com/elie29/validator/issues/22) Add callable validator rule.

### Changed

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ $validator->validate(); // bool depends on $_POST content
1. [ArrayRule](https://github.com/elie29/validator/blob/master/src/Rule/ArrayRule.php) accepts `min` and `max` options. Empty value is cast to empty array [].
1. [BicRule](https://github.com/elie29/validator/blob/master/src/Rule/BicRule.php)
1. [BooleanRule](https://github.com/elie29/validator/blob/master/src/Rule/BooleanRule.php) accepts `cast` option.
1. [CallableRule](https://github.com/elie29/validator/blob/master/src/Rule/CallableRule.php) accepts `callable` function.
1. [ChoicesRule](https://github.com/elie29/validator/blob/master/src/Rule/ChoicesRule.php) accepts `list` option.
1. [CompareRule](https://github.com/elie29/validator/blob/master/src/Rule/CompareRule.php) accepts `sign` and `expected` options. `sign` is [CompareRule::EQ](https://github.com/elie29/validator/blob/master/src/Rule/CompareConstants.php) by default, `expected` is null by default.
1. [DateRule](https://github.com/elie29/validator/blob/master/src/Rule/DateRule.php) accepts `format` and `separator` options.
Expand Down
64 changes: 64 additions & 0 deletions src/Rule/CallableRule.php
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;
}
}
46 changes: 46 additions & 0 deletions tests/Rule/CallableRuleTest.php
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'
];
}
}

0 comments on commit 4f3f0a8

Please sign in to comment.