<?php
class ClientController {
private $collection;
public function __construct($collection) {
$this->collection = $collection;
$this->validate();
}
protected function validate() {
foreach ($this->collection as $model) {
$props = get_object_vars($model);
foreach ($props as $key => $val) {
$method = 'validate' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
$valid = $model->$method();
if (!$valid) {
$model->addError($key);
}
}
if (length($model->getErrors())) {
$model->setValid(false);
}
}
}
public function getCollection() {
return $this->collection;
}
}
class ClientModel {
private $name;
private $cpf;
private $birth_date;
private $email;
private static $is_valid = true;
private static $errors = [];
public function __construct($name, $cpf, $birth_date, $email) {
$this->setName($name);
$this->setCpf($cpf);
$this->setBirthDate($birth_date);
$this->setEmail($email);
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function validateName() {
return (!empty($this->name) && !preg_match("/^[a-zA-Z'-]+$/", $this->name));
}
public function getCpf() {
return $this->cpf;
}
public function setCpf($cpf) {
$this->cpf = $cpf;
}
public function validateCpf() {
return (!empty($this->cpf) && ctype_digit($this->cpf));
}
public function getBirthDate() {
return $this->birth_date;
}
public function setBirthDate($birth_date) {
$this->birth_date = $birth_date;
}
public function validateBirthDate() {
return (!empty($this->birth_date) && preg_match("/^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/", $this->birth_date));
}
public function getEmail() {
return $this->email;
}
public function setEmail($email) {
$this->email = $email;
}
public function validateEmail() {
return (filter_var($this->email, FILTER_VALIDATE_EMAIL));
}
public function isValid() {
return $this->is_valid;
}
public function setValid($valid) {
$this->is_valid = $valid;
}
public function addError($field) {
$this->errors[] = $field;
}
public function getErrors() {
return $this->errors;
}
}
class ClientCollection {
private $_clients = [];
public function add(ClientModel $client) {
$this->_clients[] = $client;
end($this->_clients);
return key($this->_clients);
}
public function get($key) {
if (isset($this->_clients[$key])) {
return $this->_clients[$key];
} else {
throw new Exception("Client does not exist");
}
}
public function delete($key) {
if (isset($this->_clients[$key])) {
unset($this->_clients[$key]);
} else {
throw new Exception("Client does not exist");
}
}
}
$clients = array(
array("name" => "Hubert Adams", "cpf" => "86383860011", "birth_date" => "20/07/2015", "email" => "carlee_fadel@block.name"),
array("name" => "Isabelle Mann", "cpf" => "58958058684", "birth_date" => "27/07/2015", "email" => "dortha.pacocha@ebert.biz"),
array("name" => "Jayda Reichert", "cpf" => "01075384664", "birth_date" => "24/07/2015", "email" => "kaylie@stehrgaylord.biz"),
array("name" => "Brionna Reinger", "cpf" => "48991299636", "birth_date" => "22/07/2015", "email" => "ansley.orn@herzog.org"),
array("name" => "Diego", "cpf" => "55483741684aaaa", "birth_date" => "", "email" => "raoul mills com")
);
$collection = new ClientCollection();
foreach ($clients as $client) {
$model = new ClientModel($client['name'], $client['cpf'], $client['birth_date'], $client['email']);
$collection->add($model);
}
$ctrl = new ClientController($collection);
var_dump($ctrl->getCollection());
preferences:
45.82 ms | 402 KiB | 5 Q