Merge pull request #30 from eddiejaoude/master

Data mapper improvements inc. docblocs, unused code etc
This commit is contained in:
Dominik Liebler
2013-09-02 06:54:44 -07:00
4 changed files with 69 additions and 21 deletions

View File

@@ -1,6 +1,8 @@
language: php language: php
php: php:
- 5.3
- 5.4 - 5.4
- 5.5
branches: branches:
only: only:

View File

@@ -5,43 +5,79 @@ namespace DesignPatterns\DataMapper;
/** /**
* DataMapper pattern * 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 class User
{ {
protected $userId; /**
* @var int
*/
protected $userId;
/**
* @var string
*/
protected $username; protected $username;
/**
* @var string
*/
protected $email; 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->userId = $id;
$this->username = $username; $this->username = $username;
$this->email = $email; $this->email = $email;
} }
public function getUserId() { /**
* @return int
*/
public function getUserId() {
return $this->userId; return $this->userId;
} }
public function setUserID($userId) { /**
* @param int $userId
*/
public function setUserID($userId) {
$this->userId = $userId; $this->userId = $userId;
} }
public function getUsername() { /**
* @return string
*/
public function getUsername() {
return $this->username; return $this->username;
} }
public function setUsername($username) { /**
* @param string $username
*/
public function setUsername($username) {
$this->username = $username; $this->username = $username;
} }
public function getEmail() { /**
* @return string
*/
public function getEmail() {
return $this->email; return $this->email;
} }
public function setEmail($email) { /**
* @param string $email
*/
public function setEmail($email) {
$this->email = $email; $this->email = $email;
} }

View File

@@ -26,49 +26,53 @@ namespace DesignPatterns\DataMapper;
class UserMapper class UserMapper
{ {
protected $_adapter; /**
* @var DBAL
*/
protected $adapter;
public function __construct(DBAL $dbLayer) public function __construct(DBAL $dbLayer)
{ {
$this->_adapter = $dbLayer; $this->adapter = $dbLayer;
} }
/** /**
* saves a user object from memory to Database * saves a user object from memory to Database
* *
* @param User $user
* @return boolean * @return boolean
*/ */
public function save(User $user) 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( $data = array(
'userid' => $user->getUserId(), 'userid' => $user->getUserId(),
'username' => $user->getUsername(), 'username' => $user->getUsername(),
'email' => $user->getEmail(), 'email' => $user->getEmail(),
); );
/* if no ID specified create new user else update the one in the Database */ /* if no ID specified create new user else update the one in the Database */
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;
} }
return false;
} }
/** /**
* 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
* @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");
} }
@@ -85,7 +89,7 @@ class UserMapper
*/ */
public function findAll() public function findAll()
{ {
$resultSet = $this->_adapter->findAll(); $resultSet = $this->adapter->findAll();
$entries = array(); $entries = array();
foreach ($resultSet as $row) { foreach ($resultSet as $row) {

View File

@@ -10,12 +10,18 @@ use DesignPatterns\DataMapper\UserMapper;
use DesignPatterns\DataMapper\User; use DesignPatterns\DataMapper\User;
/** /**
* UserMapperTest tests the datamappe pattern * UserMapperTest tests the datamapper pattern
*/ */
class UserMapperTest extends \PHPUnit_Framework_TestCase class UserMapperTest extends \PHPUnit_Framework_TestCase
{ {
/**
* @var UserMapper
*/
protected $mapper; protected $mapper;
/**
* @var DBAL
*/
protected $dbal; protected $dbal;
protected function setUp() protected function setUp()