From abd8168dbfd0af99c87f23676ce7d10c1b98534a Mon Sep 17 00:00:00 2001 From: Jose Pabl Domingo Aramburo Sanchez Date: Sun, 12 Aug 2018 17:15:39 -0600 Subject: [PATCH] [add] Asserting email type --- api-payroll/src/application/EmployeeApplication.php | 2 ++ api-payroll/src/service/Asserts.php | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/api-payroll/src/application/EmployeeApplication.php b/api-payroll/src/application/EmployeeApplication.php index d17e9e1..988be4f 100644 --- a/api-payroll/src/application/EmployeeApplication.php +++ b/api-payroll/src/application/EmployeeApplication.php @@ -138,6 +138,7 @@ class EmployeeApplication{ $email = $requestData['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."); + $this->asserts->isEmail($email, "The email isn't in a correct format"); $phone = $requestData['phone']; $this->asserts->isNotEmpty($phone, "The phone number can't be empty."); @@ -409,6 +410,7 @@ class EmployeeApplication{ $email = $requestData['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."); + $this->asserts->isEmail($email, "The email isn't in a correct format"); $phone = $requestData['phone']; $this->asserts->isNotEmpty($phone, "The phone number can't be empty."); diff --git a/api-payroll/src/service/Asserts.php b/api-payroll/src/service/Asserts.php index 9e1f1d3..3976a26 100644 --- a/api-payroll/src/service/Asserts.php +++ b/api-payroll/src/service/Asserts.php @@ -56,5 +56,18 @@ class Asserts{ throw new Exception($errorMessage); } } + + /** + * Compares a string against a regex to determine if it's an email + * + * @param $string string + * @param $errorMessage string + * @throws Exception + */ + function isEmail($string, $errorMessage){ + if(!filter_var($string, FILTER_VALIDATE_EMAIL)){ + throw new Exception($errorMessage); + } + } } ?>