PHP7 DataMapper

This commit is contained in:
Dominik Liebler
2016-09-22 21:04:16 +02:00
parent 320dc3c6bf
commit 243456b2da
8 changed files with 777 additions and 645 deletions

View File

@@ -2,56 +2,34 @@
namespace DesignPatterns\Structural\DataMapper;
/**
* DataMapper pattern.
*
* This is our representation of a DataBase record in the memory (Entity)
*
* Validation would also go in this object
*/
class User
{
/**
* @var int
* @var string
*/
protected $userId;
private $username;
/**
* @var string
*/
protected $username;
private $email;
/**
* @var string
*/
protected $email;
/**
* @param null $id
* @param null $username
* @param null $email
*/
public function __construct($id = null, $username = null, $email = null)
public static function fromState(array $state): User
{
$this->setUserID($id);
$this->setUsername($username);
$this->setEmail($email);
// validate state before accessing keys!
return new self(
$state['username'],
$state['email']
);
}
/**
* @return int
*/
public function getUserId()
public function __construct(string $username, string $email)
{
return $this->userId;
}
// validate parameters before setting them!
/**
* @param int $userId
*/
public function setUserID($userId)
{
$this->userId = $userId;
$this->username = $username;
$this->email = $email;
}
/**
@@ -62,14 +40,6 @@ class User
return $this->username;
}
/**
* @param string $username
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* @return string
*/
@@ -77,12 +47,4 @@ class User
{
return $this->email;
}
/**
* @param string $email
*/
public function setEmail($email)
{
$this->email = $email;
}
}