Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class User
- {
- private $id;
- private $email;
- private $password;
- private $name;
- private $birthDay
- public function __construct(UserBuilder $builder)
- {
- $this->email = $builder->getEmail();
- $this->password = $builder->getPassword();
- $this->name = $builder->getName();
- $this->birthDay = $builder->birthDay();
- }
- }
- class UserBuilder
- {
- private $email;
- private $password;
- private $name;
- private $birthDay;
- public function withEmail($email): User
- {
- $this->email = $email;
- return $this;
- }
- public function withPassword($password): User
- {
- $this->password = $password;
- return $this;
- }
- public function withName($name): User
- {
- $this->name = $name;
- return $this;
- }
- public function withBirthDay($birthDay): User
- {
- $this->birthDay = $birthDay;
- return $this;
- }
- public function getEmail()
- {
- return $this->email;
- }
- public function getPassword()
- {
- return $this->password;
- }
- public function getName()
- {
- return $this->name;
- }
- public function getBirthDay()
- {
- return $this->birthDay;
- }
- public function build(): User
- {
- if (empty($this->email)
- || empty($this->password)
- || empty($this->name) {
- throw \InvalidArgumentException('...');
- }
- return new User($this);
- }
- }
- // usage
- $user = (new UserBuilder()
- ->withEmail($email)
- ->withPassword($password)
- ->withName($name)
- ->withBirthDay($birthday))
- ->build();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement