[add] Generic asserts with custom errors

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

View File

@ -43,7 +43,9 @@ class SessionApplication{
* @return mixed * @return mixed
*/ */
function getPassword($userName){ function getPassword($userName){
$this->asserts->userName($userName); $this->asserts->isNotEmpty($userName, "The username can't be empty");
$this->asserts->isString($userName, "The username must be a string.");
$this->asserts->betweenLength($userName, 1, 50, "The username must have a length between 1 and 50 characters.");
$stmt = $this->pdo->prepare("SELECT password FROM users WHERE name = :userName"); $stmt = $this->pdo->prepare("SELECT password FROM users WHERE name = :userName");
$stmt->execute(array(':userName' => $userName)); $stmt->execute(array(':userName' => $userName));
@ -62,8 +64,12 @@ class SessionApplication{
* @throws Exception * @throws Exception
*/ */
function newSession($userName, $password){ function newSession($userName, $password){
$this->asserts->userName($userName); $this->asserts->isNotEmpty($userName, "The username can't be empty");
$this->asserts->password($password); $this->asserts->isString($userName, "The username must be a string.");
$this->asserts->betweenLength($userName, 1, 50, "The username must have a length between 1 and 50 characters.");
$this->asserts->isNotEmpty($password, "The password can't be empty");
$this->asserts->isString($password, "The password must be a string.");
$this->asserts->betweenLength($password, 1, 50, "The password must have a length between 1 and 50 characters.");
$storedPassword = $this->getPassword($userName); $storedPassword = $this->getPassword($userName);
@ -93,6 +99,14 @@ class SessionApplication{
* @throws Exception * @throws Exception
*/ */
function login($userName, $password){ function login($userName, $password){
$this->asserts->isNotEmpty($userName, "The username can't be empty");
$this->asserts->isString($userName, "The username must be a string.");
$this->asserts->betweenLength($userName, 1, 50, "The username must have a length between 1 and 50 characters.");
$this->asserts->isNotEmpty($password, "The password can't be empty");
$this->asserts->isString($password, "The password must be a string.");
$this->asserts->betweenLength($password, 1, 50, "The password must have a length between 1 and 50 characters.");
if($this->newSession($userName, $password)){ if($this->newSession($userName, $password)){
return array('status' => 'success', 'message' => 'Logged in successfully.'); return array('status' => 'success', 'message' => 'Logged in successfully.');
} }

View File

@ -5,6 +5,47 @@ use Exception;
use Respect\Validation\Validator as v; use Respect\Validation\Validator as v;
class Asserts{ 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 * @param $string
* @throws Exception * @throws Exception