I'm still learning about domain driven design, and I have created my first entity class User
.
Is this fully compliant with the DDD principles? Particularly my setters because I have business rules in them. Any other suggestions would be highly appreciated as well.
namespace Models\Entities;
class User
{
private $id;
private $firstName;
private $lastName;
private $email;
private $password;
private $registerDate;
public function setId($id)
{
if (!$this->id) {
if (is_int($id) && $id >= 1) {
$this->id = $id;
return $this;
}
throw new \InvalidArgumentException('ID must be a positive integer.');
}
throw new \InvalidArgumentException('ID has already been set.');
}
public function getId()
{
return $this->id;
}
public function setFirstName($firstName)
{
if (is_string($firstName) && !empty($firstName) && ctype_alpha($firstName)) {
$length = $this->stringLength($firstName);
if ($length >= 1 && $length <= 35) {
$this->firstName = $firstName;
return $this;
}
}
throw new \InvalidArgumentException('First name must be an alpha string inclusively between 1 and 35 characters.');
}
public function getFirstName()
{
return $this->firstName;
}
public function setLastName($lastName)
{
if (is_string($lastName) && !empty($lastName) && ctype_alpha($lastName)) {
$length = $this->stringLength($lastName);
if ($length >= 1 && $length <= 35) {
$this->lastName = $lastName;
return $this;
}
}
throw new \InvalidArgumentException('Last name must be an alpha string inclusively between 1 and 35 characters.');
}
public function getLastName()
{
return $this->lastName;
}
public function getFullName()
{
return $this->firstName . ' ' . $this->lastName;
}
public function setEmail($email)
{
if (is_string($email) && !empty($email)) {
$sanitized = filter_var($email, FILTER_SANITIZE_EMAIL);
$isValid = filter_var($sanitized, FILTER_VALIDATE_EMAIL);
$isValid = $isValid ? $this->stringLength($email) <= 254 : false;
if ($email === $sanitized && $isValid) {
$this->email = $email;
return $this;
}
}
throw new \InvalidArgumentException('Email must be a string in this format: "[local]@[domain].[ext]".');
}
public function getEmail()
{
return $this->email;
}
public function setPassword($password)
{
if (is_string($password) && !empty($password)) {
$length = $this->stringLength($password);
if ($length >= 6 && $length <= 60) {
$this->password = $password;
return $this;
}
}
throw new \InvalidArgumentException('Password must be a string inclusively between 6 and 60 characters.');
}
public function getPassword()
{
return $this->password;
}
public function setRegisterDate($date)
{
if (is_int($date) && !empty($date)) {
// TO DO: date validation unix timestamp is_int
$this->registerDate = $date;
return $this;
}
throw new \InvalidArgumentException('Register date must be an integer and valid unix timestamp.');
}
public function getRegisterDate()
{
return $this->registerDate;
}
private function stringLength($input)
{
$encoding = mb_detect_encoding($input);
$length = mb_strlen($input, $encoding);
return $length;
}
}