5
I’m studying some concepts of OOP and I see a lot about the issue of SRP, "separating actions into classes etc". at the level of testing, I decided to create a small system for registering users in order to test the knowledge, only that I came across some questions
1 - I must separate the validation logic and the CRUD part into classes separated?
2 - It is the user’s responsibility to validate himself?
2.1 - User class may be responsible for "accumulating" validation errors?
3 - The user is responsible for saving himself?
4 - What would this example look like with thousands of attributes? use a loop in the setters methods?
What I concluded, based on the studies:
I must use 3 classes, User, Uservalidator and Usercrud, because in my understanding they are different "actions"
The User class would be the most "abstract", relating validation and integration with the database.
Should I create a second class called Uservalidator that would be responsible for the validation rules and can be coupled to some validation library? (Respect/rakit)
And finally, it would create a last class called Usercrud, which would inherit the methods of another class called "crudModel", responsible for integration with the database
Code example:
class User {
private $name;
private $age;
function __construct () {
$this->userValidator = new userValidator;
$this->userCRUD = new userCRUD;
}
function setName($name) {
if($this->userValidator->validateName($name))
$this->name = $name;
}
function setAge($age) {
if($this->userValidator->validateAge($age))
$this->age = $age;
}
function hasErrors () {
return $this->userValidator->hasErrors();
}
function Save () {
$user_data['name'] = $this->name;
$user_data['age'] = $this->age;
return $this->userCRUD->create($user_data);
// retorna true para salvo e falso para erro
}
}
-
class UserValidator {
private $error_bag;
public function validateName ($name) {
if(strlen($name) > 5)
return 1;
else
$this->setValidationError('name', 'Nome deve ser maior que 5');
}
public function validateAge ($age) {
if(is_numeric($age))
return 1;
else
$this->setValidationError('age', 'Idade inválida');
}
private function setValidationError ($error, $error_msg) {
$this->error_bag[$error] = $error_msg;
}
private function getValidationErrors() {
return $this->error_bag;
}
public function hasErrors () {
if( empty($this->getValidationErrors()) )
return false;
else
return $this->getValidationErrors();
}
}
Code call:
$User = new User;
$User->setName('Fulano de Tal');
$User->setAge(50);
if($User->hasErrors()) {
// exibe os erros na tela
print_r($User->hasErrors());
} else {
// salve o usuario
$saveUser = $User->Save();
var_dump($saveUser);
}