<?php
class ClientController {
function __construct($clients) {
foreach ($clients as $key => $value) {
$clients[$key]['name_valid'] = $this->validName($value['name']);
$clients[$key]['cpf_valid'] = $this->validCpf($value['cpf']);
$clients[$key]['birth_date_valid'] = $this->validBirthdate($value['birth_date']);
$clients[$key]['email_valid'] = $this->validEmail($value['email']);
}
print_r($clients);
}
function validName($name) {
return (!empty($name) && !preg_match("/^[a-zA-Z'-]+$/", $name));
}
function validCpf($cpf) {
return (!empty($cpf) && ctype_digit($cpf));
}
function validBirthdate($date) {
return (!empty($birthdate) && preg_match("/^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/", $birthdate));
}
function validEmail($email) {
return (filter_var($email, FILTER_VALIDATE_EMAIL));
}
}
class ClientModel {
private $name;
private $cpf;
private $birth_date;
private $email;
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 getCpf() {
return $this->cpf;
}
public function setCpf($cpf) {
$this->cpf = $cpf;
}
public function getBirthDate() {
return $this->birth_date;
}
public function setBirthDate($birth_date) {
$this->birth_date = $birth_date;
}
public function getEmail() {
return $this->email;
}
public function setEmail($email) {
$this->email = $email;
}
}
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);
}
var_dump($collection);
preferences:
60.53 ms | 402 KiB | 5 Q