-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
8540f3d
commit 735446d
Showing
2 changed files
with
79 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace ipl\Validator; | ||
|
||
class DeferredInArrayValidator extends InArrayValidator | ||
{ | ||
/** @var callable Callback to create the haystack array */ | ||
protected $callback; | ||
|
||
/** | ||
* Validates whether the value exists in the haystack create by the callback | ||
* | ||
* Required parameter: | ||
* | ||
* - callback: (callable) The callback to create haystack | ||
* | ||
* Optional parameter: | ||
* | ||
* - options: (array) Following option can be defined: | ||
* | ||
* `strict` => true|false, default `false` | ||
* | ||
* @param callable $callback Validation callback | ||
* @param array $options | ||
*/ | ||
public function __construct(callable $callback, array $options = []) | ||
{ | ||
$this->callback = $callback; | ||
|
||
parent::__construct($options); | ||
} | ||
|
||
public function getHaystack(): array | ||
{ | ||
return call_user_func($this->callback); | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
namespace ipl\Tests\Validator; | ||
|
||
use ipl\I18n\NoopTranslator; | ||
use ipl\I18n\StaticTranslator; | ||
use ipl\Validator\DeferredInArrayValidator; | ||
|
||
class DeferredInArrayValidatorTest extends TestCase | ||
{ | ||
public function testWithValidAndInvalidValue() | ||
{ | ||
StaticTranslator::$instance = new NoopTranslator(); | ||
|
||
$validator = new DeferredInArrayValidator(function () { | ||
return ['test', 'foo', 'bar', 5, [], 0.008]; | ||
}); | ||
|
||
$this->assertTrue($validator->isValid('foo'), 'foo was found in the haystack'); | ||
$this->assertTrue($validator->isValid(5), '5 was found in the haystack'); | ||
|
||
$this->assertFalse($validator->isValid('bear'), 'bear was found in the haystack'); | ||
$this->assertFalse($validator->isValid(60), '60 was found in the haystack'); | ||
} | ||
|
||
public function testOptions() | ||
{ | ||
StaticTranslator::$instance = new NoopTranslator(); | ||
|
||
$validator = new DeferredInArrayValidator( | ||
function () { | ||
return ['test', 'foo', 'bar', 5, [], 0.008]; | ||
}, | ||
['strict' => true] | ||
); | ||
|
||
$this->assertTrue($validator->isStrict()); | ||
|
||
$validator->setStrict(false); | ||
$this->assertFalse($validator->isStrict()); | ||
} | ||
} |