mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-09-25 22:09:23 +02:00
PHP7 DataMapper
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user