Advertisement
Guest User

Untitled

a guest
Apr 14th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.73 KB | None | 0 0
  1. <?php
  2.  
  3. class User
  4. {
  5.     private $id;
  6.     private $email;
  7.     private $password;
  8.     private $name;
  9.     private $birthDay
  10.  
  11.     public function __construct(UserBuilder $builder)
  12.     {
  13.         $this->email = $builder->getEmail();
  14.         $this->password = $builder->getPassword();
  15.         $this->name = $builder->getName();
  16.         $this->birthDay = $builder->birthDay();
  17.     }
  18. }
  19.  
  20. class UserBuilder
  21. {
  22.     private $email;
  23.     private $password;
  24.     private $name;
  25.     private $birthDay;
  26.  
  27.     public function withEmail($email): User
  28.     {
  29.         $this->email = $email;
  30.         return $this;
  31.     }
  32.  
  33.     public function withPassword($password): User
  34.     {
  35.         $this->password = $password;
  36.         return $this;
  37.     }
  38.  
  39.     public function withName($name): User
  40.     {
  41.         $this->name = $name;
  42.         return $this;
  43.     }
  44.  
  45.     public function withBirthDay($birthDay): User
  46.     {
  47.         $this->birthDay = $birthDay;
  48.         return $this;
  49.     }
  50.  
  51.     public function getEmail()
  52.     {
  53.         return $this->email;
  54.     }
  55.  
  56.     public function getPassword()
  57.     {
  58.         return $this->password;
  59.     }
  60.  
  61.     public function getName()
  62.     {
  63.         return $this->name;
  64.     }
  65.  
  66.     public function getBirthDay()
  67.     {
  68.         return $this->birthDay;
  69.     }    
  70.  
  71.     public function build(): User
  72.     {
  73.         if (empty($this->email)
  74.             || empty($this->password)
  75.             || empty($this->name) {
  76.             throw \InvalidArgumentException('...');
  77.         }
  78.        
  79.         return new User($this);
  80.     }
  81. }
  82.  
  83. // usage
  84. $user = (new UserBuilder()
  85.        ->withEmail($email)
  86.        ->withPassword($password)
  87.        ->withName($name)
  88.        ->withBirthDay($birthday))
  89.        ->build();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement