mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 20:20:15 +02:00
cs DataMapper
This commit is contained in:
@@ -42,43 +42,48 @@ class User
|
|||||||
/**
|
/**
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getUserId() {
|
public function getUserId()
|
||||||
return $this->userId;
|
{
|
||||||
}
|
return $this->userId;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $userId
|
* @param int $userId
|
||||||
*/
|
*/
|
||||||
public function setUserID($userId) {
|
public function setUserID($userId)
|
||||||
$this->userId = $userId;
|
{
|
||||||
}
|
$this->userId = $userId;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getUsername() {
|
public function getUsername()
|
||||||
return $this->username;
|
{
|
||||||
}
|
return $this->username;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $username
|
* @param string $username
|
||||||
*/
|
*/
|
||||||
public function setUsername($username) {
|
public function setUsername($username)
|
||||||
$this->username = $username;
|
{
|
||||||
}
|
$this->username = $username;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getEmail() {
|
public function getEmail()
|
||||||
return $this->email;
|
{
|
||||||
}
|
return $this->email;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $email
|
* @param string $email
|
||||||
*/
|
*/
|
||||||
public function setEmail($email) {
|
public function setEmail($email)
|
||||||
$this->email = $email;
|
{
|
||||||
}
|
$this->email = $email;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -25,12 +25,14 @@ namespace DesignPatterns\DataMapper;
|
|||||||
*/
|
*/
|
||||||
class UserMapper
|
class UserMapper
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var DBAL
|
* @var DBAL
|
||||||
*/
|
*/
|
||||||
protected $adapter;
|
protected $adapter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param DBAL $dbLayer
|
||||||
|
*/
|
||||||
public function __construct(DBAL $dbLayer)
|
public function __construct(DBAL $dbLayer)
|
||||||
{
|
{
|
||||||
$this->adapter = $dbLayer;
|
$this->adapter = $dbLayer;
|
||||||
@@ -40,6 +42,7 @@ class UserMapper
|
|||||||
* saves a user object from memory to Database
|
* saves a user object from memory to Database
|
||||||
*
|
*
|
||||||
* @param User $user
|
* @param User $user
|
||||||
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function save(User $user)
|
public function save(User $user)
|
||||||
@@ -55,9 +58,11 @@ class UserMapper
|
|||||||
if (null === ($id = $user->getUserId())) {
|
if (null === ($id = $user->getUserId())) {
|
||||||
unset($data['userid']);
|
unset($data['userid']);
|
||||||
$this->adapter->insert($data);
|
$this->adapter->insert($data);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
$this->adapter->update($data, array('userid = ?' => $id));
|
$this->adapter->update($data, array('userid = ?' => $id));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,13 +71,15 @@ class UserMapper
|
|||||||
* finds a user from Database based on ID and returns a User object located
|
* finds a user from Database based on ID and returns a User object located
|
||||||
* in memory
|
* in memory
|
||||||
*
|
*
|
||||||
* @param $id
|
* @param int $id
|
||||||
|
*
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
* @return User
|
* @return User
|
||||||
*/
|
*/
|
||||||
public function findById($id)
|
public function findById($id)
|
||||||
{
|
{
|
||||||
$result = $this->adapter->find($id);
|
$result = $this->adapter->find($id);
|
||||||
|
|
||||||
if (0 == count($result)) {
|
if (0 == count($result)) {
|
||||||
throw new \InvalidArgumentException("User #$id not found");
|
throw new \InvalidArgumentException("User #$id not found");
|
||||||
}
|
}
|
||||||
@@ -104,7 +111,7 @@ class UserMapper
|
|||||||
*
|
*
|
||||||
* @param array $row
|
* @param array $row
|
||||||
*
|
*
|
||||||
* @return \DesignPatterns\DataMapper\User
|
* @return User
|
||||||
*/
|
*/
|
||||||
protected function mapObject(array $row)
|
protected function mapObject(array $row)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user