@ 2015-07-22T18:50:58Z <?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());
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.4.0 - 5.4.45 , 5.5.24 - 5.5.35 , 5.6.8 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 object(ClientCollection)#1 (1) {
["_clients":"ClientCollection":private]=>
array(5) {
[0]=>
object(ClientModel)#2 (4) {
["name":"ClientModel":private]=>
string(12) "Hubert Adams"
["cpf":"ClientModel":private]=>
string(11) "86383860011"
["birth_date":"ClientModel":private]=>
string(10) "20/07/2015"
["email":"ClientModel":private]=>
string(23) "carlee_fadel@block.name"
}
[1]=>
object(ClientModel)#3 (4) {
["name":"ClientModel":private]=>
string(13) "Isabelle Mann"
["cpf":"ClientModel":private]=>
string(11) "58958058684"
["birth_date":"ClientModel":private]=>
string(10) "27/07/2015"
["email":"ClientModel":private]=>
string(24) "dortha.pacocha@ebert.biz"
}
[2]=>
object(ClientModel)#4 (4) {
["name":"ClientModel":private]=>
string(14) "Jayda Reichert"
["cpf":"ClientModel":private]=>
string(11) "01075384664"
["birth_date":"ClientModel":private]=>
string(10) "24/07/2015"
["email":"ClientModel":private]=>
string(23) "kaylie@stehrgaylord.biz"
}
[3]=>
object(ClientModel)#5 (4) {
["name":"ClientModel":private]=>
string(15) "Brionna Reinger"
["cpf":"ClientModel":private]=>
string(11) "48991299636"
["birth_date":"ClientModel":private]=>
string(10) "22/07/2015"
["email":"ClientModel":private]=>
string(21) "ansley.orn@herzog.org"
}
[4]=>
object(ClientModel)#6 (4) {
["name":"ClientModel":private]=>
string(5) "Diego"
["cpf":"ClientModel":private]=>
string(15) "55483741684aaaa"
["birth_date":"ClientModel":private]=>
string(0) ""
["email":"ClientModel":private]=>
string(15) "raoul mills com"
}
}
}
Output for 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 Parse error: syntax error, unexpected '[' in /in/DbUDj on line 40
Process exited with code 255 . Output for 5.0.0 - 5.0.5 Parse error: parse error, unexpected '[' in /in/DbUDj on line 40
Process exited with code 255 . Output for 4.4.2 - 4.4.9 Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/DbUDj on line 6
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/DbUDj on line 6
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/DbUDj on line 6
Process exited with code 255 . preferences:dark mode live preview
260.99 ms | 401 KiB | 354 Q