Last active
August 29, 2015 14:20
-
-
Save fesor/6a857a57baa400285fa5 to your computer and use it in GitHub Desktop.
Domain-model: User
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Email | |
| { | |
| private $email; | |
| public function __construct($email) | |
| { | |
| if (filter_var($email, FILTER_VALIDATE_EMAIL)) { | |
| throw new \InvalidArgumentException('Invalid Email'); | |
| } | |
| $this->email = $email; | |
| } | |
| public function __toString() | |
| { | |
| return $this->email; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class User | |
| { | |
| private $id; | |
| private $email; | |
| private $password; | |
| /** todo: password should be Value Object too? */ | |
| public function __construct(Email $email, $password) | |
| { | |
| $this->email = $email; | |
| $this->setPassword($password); | |
| } | |
| public function setPassword($password) | |
| { | |
| if (6 < strlen($password)) { | |
| throw new \InvalidArgumentException('Password is too short'); | |
| } | |
| // and some other specific buisness rules | |
| // todo: User should'n know how to encode password... | |
| $this->password = password_hash($password, PASSWORD_BCRYPT); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class UserController extends Controller { | |
| public function registerUser(Request $request) | |
| { | |
| $user = new User( | |
| new Email($request->get('email')), | |
| $request->get('password') | |
| ); | |
| $this->get('app.repository.user')->add($user); | |
| return $user; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment