From 407200878dc01a28c189b1b87612c433dd693713 Mon Sep 17 00:00:00 2001 From: Ivan Diaz Date: Wed, 22 Jun 2016 00:21:03 -0300 Subject: [PATCH] [Ivan] - Validations - Implement decorator pattern [skip ci] --- server/libs/validators/EmailValidator.php | 4 +++- server/libs/validators/PasswordValidator.php | 4 ++-- server/libs/validators/Validator.php | 12 +++++++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/server/libs/validators/EmailValidator.php b/server/libs/validators/EmailValidator.php index a76f6cba..be4da69c 100644 --- a/server/libs/validators/EmailValidator.php +++ b/server/libs/validators/EmailValidator.php @@ -4,7 +4,9 @@ require_once 'Validator.php'; class EmailValidator extends Validator { public function validate($value, $fullData) { - if(strpos($value, '@') === false) { + parent::validate($value, $fullData); + + if (strpos($value, '@') === false) { Validator::throwException(); } } diff --git a/server/libs/validators/PasswordValidator.php b/server/libs/validators/PasswordValidator.php index 046f2590..3b706095 100644 --- a/server/libs/validators/PasswordValidator.php +++ b/server/libs/validators/PasswordValidator.php @@ -3,7 +3,7 @@ require_once 'Validator.php'; class PasswordValidator extends Validator { - public function validate($value, $fullData) { - + public function validate($value, $fullData = []) { + parent::validate($value, $fullData); } } \ No newline at end of file diff --git a/server/libs/validators/Validator.php b/server/libs/validators/Validator.php index 2cdc402e..6cdb6c9c 100644 --- a/server/libs/validators/Validator.php +++ b/server/libs/validators/Validator.php @@ -6,9 +6,15 @@ class Validator { throw new ValidationException($message); } - public function validate($value) { - if (!$value) { - throw new ValidationException(); + protected $baseValidator; + + public function __construct(Validator $baseValidator = null) { + $this->baseValidator = $baseValidator; + } + + public function validate($value, $fullData = []) { + if ($this->baseValidator) { + $this->baseValidator->validate($value, $fullData); } } }