Skip to content

Instantly share code, notes, and snippets.

@fesor
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save fesor/6a857a57baa400285fa5 to your computer and use it in GitHub Desktop.

Select an option

Save fesor/6a857a57baa400285fa5 to your computer and use it in GitHub Desktop.
Domain-model: User
<?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;
}
}
<?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);
}
}
<?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