Last active
December 15, 2015 17:29
-
-
Save edinella/5296977 to your computer and use it in GitHub Desktop.
Using php magic setters and getters to validate object properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| date_default_timezone_set('America/Sao_Paulo'); | |
| /** | |
| * Classe com filtros mágicos sobre as propriedades | |
| */ | |
| abstract class Magic { | |
| /** @var array Propriedades */ | |
| private $_properties; | |
| /** | |
| * Construtor, armazena e remove de si as propriedades | |
| */ | |
| public function __construct() { | |
| foreach($this as $key => $value) { | |
| if($key != '_properties') { | |
| unset($this->$key); | |
| $this->_properties[$key] = $value; | |
| } | |
| } | |
| } | |
| /** | |
| * Obtém uma propriedade | |
| * @param string $key | |
| * @return unknown_type | |
| */ | |
| public function __get($key) { | |
| return $this->_properties[$key]; | |
| } | |
| /** | |
| * Armazena uma propriedade | |
| * @param string $key | |
| * @param unknown_type $value | |
| */ | |
| public function __set($key, $value) { | |
| $this->_properties[$key] = $value; | |
| } | |
| } | |
| /** | |
| * Representa um usuário | |
| */ | |
| class User extends Magic { | |
| public $name; | |
| public $role; | |
| public $email; | |
| public $created; | |
| /** | |
| * Roda ao construir | |
| */ | |
| public function __construct() { | |
| parent::__construct(); | |
| // preenche campos padrão | |
| $this->created = new DateTime; | |
| $this->role = 'user'; | |
| } | |
| /** | |
| * Roda ao armazenar | |
| * @param string $key | |
| * @param unknown_type $value | |
| */ | |
| public function __set($key, $value) { | |
| // datetime | |
| if($key == 'created' && (!$value instanceof DateTime)) | |
| throw new Exception('Field '.$key.' should contain instance of DateTime'); | |
| if($key == 'email' && !strpos($value, '@')) | |
| throw new Exception('Field '.$key.' should contain a valid email'); | |
| // prossegue | |
| parent::__set($key, $value); | |
| } | |
| /** | |
| * Roda ao obter | |
| * @param string $key | |
| * @return unknown_type $value | |
| */ | |
| public function __get($key) { | |
| $value = parent::__get($key); | |
| // required | |
| if(in_array($key, array('name', 'email', 'role')) && empty($value)) | |
| throw new Exception('Required field '.$key.' can not be empty'); | |
| // format | |
| if($key == 'created') | |
| $value = $value->format('Y-m-d H:i:s'); | |
| // prossegue | |
| return $value; | |
| } | |
| } | |
| $u = new User(); | |
| $u->name = 'Manolo'; | |
| $u->email = 'manolo@server'; | |
| echo '<pre>User:'."\n"; | |
| echo ' name: '.$u->name."\n"; | |
| echo ' role: '.$u->role."\n"; | |
| echo ' email: '.$u->email."\n"; | |
| echo ' created: '.$u->created."\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment