Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Classe para tratamento de dados do usuário.
- *
- * Esta classe valida a entrada de dados e filtra a saída de dados.
- *
- * @author Leonardo Poletto <contato@leonardopoletto.com.br>
- */
- class User
- {
- /**
- * Id do usuário
- *
- * @var integer
- */
- private $id;
- /**
- * Username do usuário
- *
- * @var string
- */
- private $username;
- /**
- * Senha do usuário
- *
- * Usa um hash md5
- *
- * @var string
- */
- private $password;
- /**
- * Define o ID do usuário
- *
- * @throws InvalidArgumentException Se $value não for um ID válido.
- * @param integer|string $value
- * @return void
- */
- public function setId($value)
- {
- if(!is_numeric($value) or (is_numeric($value) and $value <= 0) ){
- throw new InvalidArgumentException('O valor deve ser inteiro e maior que zero');
- }
- $this->id = (int) $value;
- }
- /**
- * Retorna o ID do usuário.
- *
- * @return integer|null
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * Defines o nome do usuário
- *
- * @throws InvalidArgumentException Se $value não for uma string válida.
- * @param string $value
- * @return void
- */
- public function setUsername($value)
- {
- if(!is_string($value) || (is_string($value) && empty($value))) {
- throw new InvalidArgumentException('Username deve ser uma string válida.');
- }
- $this->username = $value;
- }
- /**
- * Retorna o nome do usuário.
- *
- * @throws UnexpectedValueException Se o nome de usuário não estiver definido.
- * @return string
- */
- public function getUsername()
- {
- if(!$this->username) {
- throw new UnexpectedValueException('É preciso definir o username primeiro');
- }
- return $this->username;
- }
- /**
- * Define a senha do usuário.
- *
- * Aplica um hash md5 caso $value não contiver 32 caracteres.
- *
- * @throws InvalidArgumentException Se $value não for uma string válida.
- * @param string $value
- * @return void
- */
- public function setPassword($value)
- {
- if(!is_string($value) or (is_string($value) and empty($value))){
- throw new InvalidArgumentException('Password deve se uma string.');
- }
- $this->password = strlen($value) === 32 ? $value : md5('SALT_KEY' . $value);
- }
- /**
- * Retorna a senha do usuário.
- *
- * @throws UnexpectedValueException Se não for definido a senha.
- * @return string
- */
- public function getPassword()
- {
- if(!$this->password) {
- throw new UnexpectedValueException('É preciso definir o password primeiro');
- }
- return $this->password;
- }
- /**
- * Define as propriedades do usuário, baseado em um Array.
- *
- * No array, procura um método correspondente ao nome
- * da chave. $data['nome'] --> setNome($data['nome'])
- *
- * @param array $data
- * @return void
- */
- public function populate(array $data)
- {
- foreach($data as $key=>$value) {
- $method_name = 'set' . ucfirst($key);
- if (method_exists($this, $method_name)) {
- $this->{$method_name}($value);
- }
- }
- }
- }
- try {
- $user = new User();
- $user->getUsername();
- var_dump($user);
- } catch (InvalidArgumentException $exception) {
- echo '<h1>Defina os valore corretamente</h1>';
- echo $exception->getMessage();
- echo '<br />';
- echo $exception->getTraceAsString();
- } catch (UnexpectedValueException $exception) {
- echo '<h1>Não esqueça de definir valores</h1>';
- echo $exception->getMessage();
- echo '<br />';
- echo $exception->getTraceAsString();
- } catch (Exception $exception) {
- echo '<h1>Exception</h1>';
- echo $exception->getMessage();
- echo '<br />';
- echo $exception->getTraceAsString();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement