Advertisement
TeeZ0NE

User

Nov 3rd, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.63 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Vadim\User;
  4.  
  5. use \Datetime;
  6.  
  7. class User
  8. {
  9.    
  10.     private $name;
  11.     private $srName;
  12.     private $dbo;
  13.     private $roles;
  14.     private $password;
  15.    
  16.     /**
  17.      * Class constructor
  18.      *
  19.      * @param string $name
  20.      * @param string $srName
  21.      * @param array $roles User roles
  22.      * @param dateTime $dbo date of birth
  23.      * @param string $password not hashed password
  24.      *
  25.      */
  26.     public function __construct(string $name, string $srName, array $roles, dateTime $dbo, string $password)
  27.     {
  28.         $this->setName($name);
  29.         $this->setSrName($srName);
  30.         $this->setPassword($password);
  31.         $this->setRoles($roles);
  32.         $this->dbo = $dbo;
  33.     }
  34.    
  35.     /**
  36.      * Destruct User Class
  37.      */
  38.     public function __destruct()
  39.     {
  40.         unset($this->name);
  41.         unset($this->srName);
  42.         unset($this->dbo);
  43.         unset($this->roles);
  44.         unset($this->password);
  45.     }
  46.    
  47.     /**
  48.      * Set user name
  49.      *
  50.      * @param string $name
  51.      * @return void
  52.      */
  53.     private function setName(string $name)
  54.     {
  55.         $this->name = $name;
  56.     }
  57.    
  58.     /**
  59.      * Set user surname
  60.      *
  61.      * @param string $srName
  62.      * @return void
  63.      */
  64.     private function setSrName(string $srName)
  65.     {
  66.         $this->srName = $srName;
  67.     }
  68.    
  69.     /**
  70.      * Get user name
  71.      *
  72.      * If need to do some operations or use in nav menu as a User Auth name
  73.      *
  74.      * @return string
  75.      */
  76.      public function getName() : string
  77.      {
  78.          return $this->name;
  79.      }
  80.      
  81.      /**
  82.       * Get user surname
  83.       *
  84.       * Using if need in a User info board
  85.       *
  86.       * @return string
  87.       */
  88.       public function getSrName() : string
  89.       {
  90.           return $this->srName;
  91.       }
  92.    
  93.     /**
  94.      * Get user name and surname
  95.      * __toString
  96.      *
  97.      * @return string
  98.      */
  99.     public function GetUserName() : string
  100.     {
  101.         return sprintf('%s %s', $this->getName(), $this->getSrName());
  102.     }
  103.    
  104.     /**
  105.      * Set password
  106.      *
  107.      * Set user password
  108.      * @return void
  109.      */
  110.     private function setPassword(string $password)
  111.     {
  112.         $this->password = md5($password);
  113.     }
  114.    
  115.     /**
  116.      * Get user hashed password
  117.      *
  118.      * @return string
  119.      */
  120.     public function getPassword() : string
  121.     {
  122.         return $this->password;
  123.     }
  124.    
  125.     /**
  126.      * Set user roles
  127.      *
  128.      * @param array $roles
  129.      * @return void
  130.      */
  131.     private function setRoles(array $roles)
  132.     {
  133.         $this->roles = $roles;
  134.     }
  135.    
  136.     /**
  137.      * Get user roles
  138.      *
  139.      * @return array
  140.      */
  141.     public function getRoles() : array
  142.     {
  143.         return $this->roles;
  144.     }
  145.    
  146.     /**
  147.      * Check is user adult
  148.      *
  149.      * @return bool
  150.      */
  151.     public function isAdult() : bool
  152.     {
  153.         $now = new DateTime();
  154.         return (($now->diff($this->dbo))->y > 17) ? true : false;
  155.     }
  156.    
  157. }
  158.  
  159. $dbo = new DateTime('2015-04-08');
  160. $user = new User('Bill','Johnson',['USER','ADMIN'],$dbo,'123wert');
  161.  
  162. if ($user->getUserName() == 'Bill Johnson') {
  163.     echo "User name is correct\n";
  164. } else {
  165.     echo "User name is incorrect\n";
  166. }
  167.  
  168. if ($user->getPassword() ==  md5('123wert')) {
  169.     echo "User password is correct\n";
  170. } else {
  171.     echo "User password is incorrect\n";
  172. }
  173.  
  174. if ($user->getRoles() == ['USER','ADMIN']) {
  175.     echo "User has roles\n";
  176. } else {
  177.     echo "User hasn't roles\n";
  178. }
  179.  
  180. if ($user->isAdult()) {
  181.     echo "User is adult\n";
  182. } else {
  183.     echo "User is young (teen)\n";
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement