-
-
Save jimboobrien/9a33dd5d492d076c95a8414445697d74 to your computer and use it in GitHub Desktop.
Validate a value by running it through one or more callbacks.
This file contains hidden or 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
<?php | |
/** | |
* Validate a value by running it through one or more callbacks. | |
* | |
* @param mixed $value | |
* @param callable $validation | |
* @return bool | |
*/ | |
function validate( $value, $validation ) { | |
$valid = true; | |
$callbacks = is_callable( $validation ) ? array( $validation ) : (array) $validation; | |
foreach ( $callbacks as $callback ) { | |
if ( is_callable( $callback ) ) { | |
$valid = call_user_func( $callback, $value ); | |
} | |
if ( true !== $valid ) { | |
break; | |
} | |
} | |
return true === $valid; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment