forked from jbowens/jBBCode
-
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.
Ability to pass a callable to CodeDefinitionBuilder::set*Validator() [c…
…loses jbowens#60] From this commit, it is possible to: - pass either InputValidor or a callable to both: - CodeDefinitionBuilder::setOptionValidator() - CodeDefinitionBuilder::setBodyValidator() - wrap any method or function into JBBCode\validators\CallableValidatorAdapter without need to manually implement InputValidator (this is also what internally CodeDefinitionBuilder does)
- Loading branch information
Showing
4 changed files
with
85 additions
and
8 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
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,40 @@ | ||
<?php | ||
|
||
namespace JBBCode\validators; | ||
|
||
use JBBCode, | ||
JBBCode\InputValidator; | ||
|
||
require_once __DIR__ . '/../InputValidator.php'; | ||
|
||
/** | ||
* An adapter for callable input validator. | ||
* | ||
* This class is most often created by {@link CodeDefinitionBuilder} | ||
* and used as wrapper for different types of `callable` validators. | ||
* | ||
* @author Kubo2 | ||
* @since August 2015 | ||
*/ | ||
final class CallableValidatorAdapter implements InputValidator { | ||
|
||
/** @var callable */ | ||
private $validator; | ||
|
||
/** | ||
* Constructs adapter's instance, taking `callable` validator as parameter. | ||
* | ||
* @param callable $validator The callable validator | ||
*/ | ||
public function __construct(callable $validator) { | ||
$this->validator = $validator; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* @return boolean Anything returned by the callable validator, coerced to boolean | ||
*/ | ||
public function validate($input) { | ||
return (boolean) call_user_func($this->validator, $input); | ||
} | ||
} |