[add] Generic assers in employee application

This commit is contained in:
Jose Pabl Domingo Aramburo Sanchez 2018-08-09 22:47:19 -06:00
parent 21013cf6ac
commit 08702b2cdf

View File

@ -56,11 +56,11 @@ class EmployeeApplication{
* @return integer * @return integer
*/ */
function saveNewPerson($firstName, $middleName, $lastName, $birthDate, $email, $phone){ function saveNewPerson($firstName, $middleName, $lastName, $birthDate, $email, $phone){
$this->asserts->firstName($firstName); $this->asserts->isNotEmpty($firstName, "The first name can't be empty.");
$this->asserts->middleName($middleName); $this->asserts->isNotEmpty($middleName, "The middle name can't be empty.");
$this->asserts->birthDate($birthDate); $this->asserts->isNotEmpty($middleName, "The birth date can't be empty.");
$this->asserts->email($email); $this->asserts->isNotEmpty($email, "The email can't be empty.");
$this->asserts->phone($phone); $this->asserts->isNotEmpty($phone, "The phone number can't be empty.");
try { try {
$stmt = $this->pdo->prepare("INSERT INTO persons (firstName, middleName, lastName, birthDate, email, phone) $stmt = $this->pdo->prepare("INSERT INTO persons (firstName, middleName, lastName, birthDate, email, phone)
@ -115,21 +115,29 @@ class EmployeeApplication{
function saveNewEmployee($requestData){ function saveNewEmployee($requestData){
// Getting and validating the data // Getting and validating the data
$firstName = $requestData['firstName']; $firstName = $requestData['firstName'];
$this->asserts->firstName($firstName); $this->asserts->isNotEmpty($firstName, "The first name can't be empty.");
$this->asserts->isString($firstName, "The first name must be a string.");
$this->asserts->betweenLength($firstName, 1, 50, "The first name must have a length between 1 and 50 characters.");
$middleName = $requestData['middleName']; $middleName = $requestData['middleName'];
$this->asserts->middleName($middleName); $this->asserts->isNotEmpty($middleName, "The middle name can't be empty.");
$this->asserts->isString($middleName, "The middle name must be a string.");
$this->asserts->betweenLength($middleName, 1, 50, "The middle name must have a length between 1 and 50 characters.");
$lastName = isset($requestData['lastName']) ? $requestData['lastName'] : null; $lastName = isset($requestData['lastName'])
? $requestData['lastName']
: null;
$birthDate = $requestData['birthDate']; $birthDate = $requestData['birthDate'];
$this->asserts->birthDate($birthDate); $this->asserts->isNotEmpty($birthDate, "The birth date can't be empty.");
$email = $requestData['email']; $email = $requestData['email'];
$this->asserts->email($email); $this->asserts->isNotEmpty($email, "The email can't be empty.");
$this->asserts->betweenLength($email, 1, 100, "The middle name must have a length between 1 and 100 characters.");
$phone = $requestData['phone']; $phone = $requestData['phone'];
$this->asserts->phone($phone); $this->asserts->isNotEmpty($phone, "The phone number can't be empty.");
$this->asserts->betweenLength($phone, 10, 10, "The phone number must be 10 digits without special characters.");
$idEmployeeType = $requestData{'idEmployeeType'}; $idEmployeeType = $requestData{'idEmployeeType'};
$contractType = $requestData{'contractType'}; $contractType = $requestData{'contractType'};
@ -189,10 +197,12 @@ class EmployeeApplication{
} }
/** /**
* @param $code * @param $code string
* @return mixed * @return integer
*/ */
function getIdEmployeeTypeByCode($code){ function getIdEmployeeTypeByCode($code){
$this->asserts->isNotEmpty($code, "The code can't be empty.");
$stmt = $this->pdo->prepare("SELECT COALESCE((SELECT $stmt = $this->pdo->prepare("SELECT COALESCE((SELECT
et.id et.id
FROM FROM
@ -279,6 +289,8 @@ class EmployeeApplication{
* @return array * @return array
*/ */
function getEmployeeDataByCode($code){ function getEmployeeDataByCode($code){
$this->asserts->isNotEmpty($code, "The code can't be empty.");
$idEmployee = $this->getIdEmployeeTypeByCode($code); $idEmployee = $this->getIdEmployeeTypeByCode($code);
return $this->proxyGetEmployeeDataById($idEmployee); return $this->proxyGetEmployeeDataById($idEmployee);
@ -350,24 +362,32 @@ class EmployeeApplication{
// Getting and validating the data // Getting and validating the data
$idEmployee = $requestData['idEmployee']; $idEmployee = $requestData['idEmployee'];
$idPerson = $this->getIdPersonByIdEmployee($idEmployee); $idPerson = $this->getIdPersonByIdEmployee($idEmployee);
$code = $requestData['code']; $code = $requestData['code'];
$this->asserts->isNotEmpty($code, "The code can't be empty.");
$firstName = $requestData['firstName']; $firstName = $requestData['firstName'];
$this->asserts->firstName($firstName); $this->asserts->isNotEmpty($firstName, "The first name can't be empty.");
$this->asserts->isString($firstName, "The first name must be a string.");
$this->asserts->betweenLength($firstName, 1, 50, "The first name must have a length between 1 and 50 characters.");
$middleName = $requestData['middleName']; $middleName = $requestData['middleName'];
$this->asserts->middleName($middleName); $this->asserts->isNotEmpty($middleName, "The middle name can't be empty.");
$this->asserts->isString($middleName, "The middle name must be a string.");
$this->asserts->betweenLength($middleName, 1, 50, "The middle name must have a length between 1 and 50 characters.");
$lastName = isset($requestData['lastName']) ? $requestData['lastName'] : null; $lastName = isset($requestData['lastName']) ? $requestData['lastName'] : null;
$birthDate = $requestData['birthDate']; $birthDate = $requestData['birthDate'];
$this->asserts->birthDate($birthDate); $this->asserts->isNotEmpty($birthDate, "The birth date can't be empty.");
$email = $requestData['email']; $email = $requestData['email'];
$this->asserts->email($email); $this->asserts->isNotEmpty($email, "The email can't be empty.");
$this->asserts->betweenLength($email, 1, 100, "The middle name must have a length between 1 and 100 characters.");
$phone = $requestData['phone']; $phone = $requestData['phone'];
$this->asserts->phone($phone); $this->asserts->isNotEmpty($phone, "The phone number can't be empty.");
$this->asserts->betweenLength($phone, 10, 10, "The phone number must be 10 digits without special characters.");
$idEmployeeType = $requestData{'idEmployeeType'}; $idEmployeeType = $requestData{'idEmployeeType'};
$contractType = $requestData{'contractType'}; $contractType = $requestData{'contractType'};