Advertisement
Guest User

Untitled

a guest
May 26th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.08 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Classe para tratamento de dados do usuário.
  4.  *
  5.  * Esta classe valida a entrada de dados e filtra a saída de dados.
  6.  *
  7.  * @author Leonardo Poletto <contato@leonardopoletto.com.br>
  8.  */
  9. class User
  10. {
  11.  
  12.     /**
  13.      * Id do usuário
  14.      *
  15.      * @var integer
  16.      */
  17.     private $id;
  18.  
  19.     /**
  20.      * Username do usuário
  21.      *
  22.      * @var string
  23.      */
  24.     private $username;
  25.  
  26.     /**
  27.      * Senha do usuário
  28.      *
  29.      * Usa um hash md5
  30.      *
  31.      * @var string
  32.      */
  33.     private $password;
  34.  
  35.     /**
  36.      * Define o ID do usuário
  37.      *
  38.      * @throws InvalidArgumentException Se $value não for um ID válido.
  39.      * @param integer|string $value
  40.      * @return void
  41.      */
  42.     public function setId($value)
  43.     {
  44.         if(!is_numeric($value) or (is_numeric($value) and $value <= 0) ){
  45.             throw new InvalidArgumentException('O valor deve ser inteiro e maior que zero');
  46.         }
  47.         $this->id = (int) $value;
  48.     }
  49.  
  50.     /**
  51.      * Retorna o ID do usuário.
  52.      *
  53.      * @return integer|null
  54.      */
  55.     public function getId()
  56.     {
  57.         return $this->id;
  58.     }
  59.  
  60.     /**
  61.      * Defines o nome do usuário
  62.      *
  63.      * @throws InvalidArgumentException Se $value não for uma string válida.
  64.      * @param string $value
  65.      * @return void
  66.      */
  67.     public function setUsername($value)
  68.     {
  69.         if(!is_string($value) || (is_string($value) && empty($value))) {
  70.             throw new InvalidArgumentException('Username deve ser uma string válida.');
  71.         }
  72.         $this->username = $value;
  73.     }
  74.  
  75.     /**
  76.      * Retorna o nome do usuário.
  77.      *
  78.      * @throws UnexpectedValueException Se o nome de usuário não estiver definido.
  79.      * @return string
  80.      */
  81.     public function getUsername()
  82.     {
  83.         if(!$this->username) {
  84.             throw new UnexpectedValueException('É preciso definir o username primeiro');
  85.         }
  86.         return $this->username;
  87.     }
  88.  
  89.     /**
  90.      * Define a senha do usuário.
  91.      *
  92.      * Aplica um hash md5 caso $value não contiver 32 caracteres.
  93.      *
  94.      * @throws InvalidArgumentException Se $value não for uma string válida.
  95.      * @param string $value
  96.      * @return void
  97.      */
  98.     public  function setPassword($value)
  99.     {
  100.         if(!is_string($value) or (is_string($value) and empty($value))){
  101.             throw new InvalidArgumentException('Password deve se uma string.');
  102.         }
  103.         $this->password = strlen($value) === 32 ? $value : md5('SALT_KEY' . $value);
  104.     }
  105.  
  106.     /**
  107.      * Retorna a senha do usuário.
  108.      *
  109.      * @throws UnexpectedValueException Se não for definido a senha.
  110.      * @return string
  111.      */
  112.     public function getPassword()
  113.     {
  114.  
  115.         if(!$this->password) {
  116.             throw new UnexpectedValueException('É preciso definir o password primeiro');
  117.         }
  118.         return $this->password;
  119.     }
  120.  
  121.     /**
  122.      * Define as propriedades do usuário, baseado em um Array.
  123.      *
  124.      * No array, procura um método correspondente ao nome
  125.      * da chave. $data['nome'] --> setNome($data['nome'])
  126.      *
  127.      * @param array $data
  128.      * @return void
  129.      */
  130.     public function  populate(array $data)
  131.     {
  132.         foreach($data as $key=>$value) {
  133.             $method_name = 'set' . ucfirst($key);
  134.             if (method_exists($this, $method_name)) {
  135.                 $this->{$method_name}($value);
  136.             }
  137.         }
  138.     }
  139.  
  140. }
  141.  
  142. try {
  143.  
  144.     $user = new User();
  145.     $user->getUsername();
  146.     var_dump($user);
  147.  
  148. } catch (InvalidArgumentException $exception) {
  149.  
  150.     echo '<h1>Defina os valore corretamente</h1>';
  151.     echo $exception->getMessage();
  152.     echo '<br />';
  153.     echo $exception->getTraceAsString();
  154.  
  155. } catch (UnexpectedValueException $exception) {
  156.  
  157.     echo '<h1>Não esqueça de definir valores</h1>';
  158.     echo $exception->getMessage();
  159.     echo '<br />';
  160.     echo $exception->getTraceAsString();
  161.  
  162. } catch (Exception $exception) {
  163.  
  164.     echo '<h1>Exception</h1>';
  165.     echo $exception->getMessage();
  166.     echo '<br />';
  167.     echo $exception->getTraceAsString();
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement