[add] Generic asserts with custom errors

This commit is contained in:
2018-08-09 21:55:39 -06:00
parent 7cf083a612
commit 21013cf6ac
2 changed files with 58 additions and 3 deletions

View File

@@ -5,6 +5,47 @@ use Exception;
use Respect\Validation\Validator as v;
class Asserts{
/**
* @param $string string
* @param $errorMessage string
* @throws Exception
*/
function isString($string, $errorMessage){
$validation = v::stringType()->validate($string);
if(!$validation){
throw new Exception($errorMessage);
}
}
/**
* @param $string string
* @param $errorMessage string
* @throws Exception
*/
function isNotEmpty($string, $errorMessage){
$validation = v::notEmpty()->validate($string);
if(!$validation){
throw new Exception($errorMessage);
}
}
/**
* @param $string string
* @param $min integer
* @param $max integer
* @param $errorMessage string
* @throws Exception
*/
function betweenLength($string, $min, $max, $errorMessage){
$validation = v::length($min, $max)->validate($string);
if(!$validation){
throw new Exception($errorMessage);
}
}
/**
* @param $string
* @throws Exception