Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Created February 11, 2014 17:32
Show Gist options
  • Save wpscholar/8939780 to your computer and use it in GitHub Desktop.
Save wpscholar/8939780 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