[Ivan Diaz] - Validations - Create validation factory and initial validations [skip ci]

This commit is contained in:
Ivan Diaz 2016-06-18 20:08:53 -03:00
parent 220ff697f8
commit ee8c401e5f
5 changed files with 53 additions and 2 deletions

View File

@ -20,13 +20,13 @@ spl_autoload_register(function ($class) {
$classPath = "models/{$class}.php";
if(file_exists($classPath)) {
include $classPath;
include_once $classPath;
}
});
// LOAD CONTROLLERS
foreach (glob('controllers/*.php') as $controller) {
include $controller;
include_once $controller;
}
$app->run();

View File

@ -0,0 +1,17 @@
<?php
require_once 'validators/Validator.php';
require_once 'validators/EmailValidator.php';
require_once 'validators/PasswordValidator.php';
class ValidatorFactory {
public static function getValidator($validatorName) {
switch ($validatorName) {
case 'email':
return new EmailValidator();
case 'password':
return new PasswordValidator();
default:
return new Validator();
}
}
}

View File

@ -0,0 +1,11 @@
<?php
require_once 'Validator.php';
class EmailValidator extends Validator {
public function validate($value, $fullData) {
if(strpos($value, '@') === false) {
Validator::throwException();
}
}
}

View File

@ -0,0 +1,9 @@
<?php
require_once 'Validator.php';
class PasswordValidator extends Validator {
public function validate($value, $fullData) {
}
}

View File

@ -0,0 +1,14 @@
<?php
class ValidationException extends Exception {}
class Validator {
public static function throwException($message = '') {
throw new ValidationException($message);
}
public function validate($value) {
if (!$value) {
throw new ValidationException();
}
}
}