Skip to content

Instantly share code, notes, and snippets.

@jimboobrien
Forked from wpscholar/validate.php
Created September 20, 2017 23:19
Show Gist options
  • Save jimboobrien/9a33dd5d492d076c95a8414445697d74 to your computer and use it in GitHub Desktop.
Save jimboobrien/9a33dd5d492d076c95a8414445697d74 to your computer and use it in GitHub Desktop.
Validate a value by running it through one or more callbacks.
<?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