mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-05-23 14:59:48 +02:00
Merge pull request #30 from eddiejaoude/master
Data mapper improvements inc. docblocs, unused code etc
This commit is contained in:
commit
85f1ae42e6
@ -1,6 +1,8 @@
|
||||
language: php
|
||||
php:
|
||||
- 5.3
|
||||
- 5.4
|
||||
- 5.5
|
||||
|
||||
branches:
|
||||
only:
|
||||
|
@ -5,43 +5,79 @@ namespace DesignPatterns\DataMapper;
|
||||
/**
|
||||
* DataMapper pattern
|
||||
*
|
||||
* This is our representation of a DataBase record in the memory
|
||||
* This is our representation of a DataBase record in the memory (Entity)
|
||||
*
|
||||
* Validation would also go in this object
|
||||
*
|
||||
*/
|
||||
class User
|
||||
{
|
||||
protected $userId;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $userId;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $username;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $email;
|
||||
|
||||
public function __construct($id = null, $username = null, $email = null)
|
||||
/**
|
||||
* @param null $id
|
||||
* @param null $username
|
||||
* @param null $email
|
||||
*/
|
||||
public function __construct($id = null, $username = null, $email = null)
|
||||
{
|
||||
$this->userId = $id;
|
||||
$this->username = $username;
|
||||
$this->email = $email;
|
||||
}
|
||||
|
||||
public function getUserId() {
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getUserId() {
|
||||
return $this->userId;
|
||||
}
|
||||
|
||||
public function setUserID($userId) {
|
||||
/**
|
||||
* @param int $userId
|
||||
*/
|
||||
public function setUserID($userId) {
|
||||
$this->userId = $userId;
|
||||
}
|
||||
|
||||
public function getUsername() {
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUsername() {
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
public function setUsername($username) {
|
||||
/**
|
||||
* @param string $username
|
||||
*/
|
||||
public function setUsername($username) {
|
||||
$this->username = $username;
|
||||
}
|
||||
|
||||
public function getEmail() {
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getEmail() {
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
public function setEmail($email) {
|
||||
/**
|
||||
* @param string $email
|
||||
*/
|
||||
public function setEmail($email) {
|
||||
$this->email = $email;
|
||||
}
|
||||
|
||||
|
@ -26,49 +26,53 @@ namespace DesignPatterns\DataMapper;
|
||||
class UserMapper
|
||||
{
|
||||
|
||||
protected $_adapter;
|
||||
/**
|
||||
* @var DBAL
|
||||
*/
|
||||
protected $adapter;
|
||||
|
||||
public function __construct(DBAL $dbLayer)
|
||||
{
|
||||
$this->_adapter = $dbLayer;
|
||||
$this->adapter = $dbLayer;
|
||||
}
|
||||
|
||||
/**
|
||||
* saves a user object from memory to Database
|
||||
*
|
||||
* @param User $user
|
||||
* @return boolean
|
||||
*/
|
||||
public function save(User $user)
|
||||
{
|
||||
/* $data keys shoulds correspond to valid Table columns on the Database */
|
||||
/* $data keys should correspond to valid Table columns on the Database */
|
||||
$data = array(
|
||||
'userid' => $user->getUserId(),
|
||||
'username' => $user->getUsername(),
|
||||
'email' => $user->getEmail(),
|
||||
'email' => $user->getEmail(),
|
||||
);
|
||||
|
||||
/* if no ID specified create new user else update the one in the Database */
|
||||
if (null === ($id = $user->getUserId())) {
|
||||
unset($data['userid']);
|
||||
$this->_adapter->insert($data);
|
||||
$this->adapter->insert($data);
|
||||
return true;
|
||||
} else {
|
||||
$this->_adapter->update($data, array('userid = ?' => $id));
|
||||
$this->adapter->update($data, array('userid = ?' => $id));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* finds a user from Database based on ID and returns a User object located
|
||||
* in memory
|
||||
*
|
||||
* @param $id
|
||||
* @throws \InvalidArgumentException
|
||||
* @return User
|
||||
*/
|
||||
public function findById($id)
|
||||
{
|
||||
$result = $this->_adapter->find($id);
|
||||
$result = $this->adapter->find($id);
|
||||
if (0 == count($result)) {
|
||||
throw new \InvalidArgumentException("User #$id not found");
|
||||
}
|
||||
@ -85,7 +89,7 @@ class UserMapper
|
||||
*/
|
||||
public function findAll()
|
||||
{
|
||||
$resultSet = $this->_adapter->findAll();
|
||||
$resultSet = $this->adapter->findAll();
|
||||
$entries = array();
|
||||
|
||||
foreach ($resultSet as $row) {
|
||||
|
@ -10,12 +10,18 @@ use DesignPatterns\DataMapper\UserMapper;
|
||||
use DesignPatterns\DataMapper\User;
|
||||
|
||||
/**
|
||||
* UserMapperTest tests the datamappe pattern
|
||||
* UserMapperTest tests the datamapper pattern
|
||||
*/
|
||||
class UserMapperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var UserMapper
|
||||
*/
|
||||
protected $mapper;
|
||||
|
||||
/**
|
||||
* @var DBAL
|
||||
*/
|
||||
protected $dbal;
|
||||
|
||||
protected function setUp()
|
||||
|
Loading…
x
Reference in New Issue
Block a user