Merge pull request #19 from stakisko/master

Data Mapper Pattern Implementation
This commit is contained in:
Dominik Liebler
2013-06-27 01:23:38 -07:00
3 changed files with 186 additions and 0 deletions

27
DataMapper/Test.php Normal file
View File

@@ -0,0 +1,27 @@
<?php
namespace DesignPatterns;
/**
* DataMapper pattern
*
* Testing the DataMapper Pattern
*/
$userMapper = new UserMapper();
/* fetch a record from Database */
$user = $userMapper->findById(1);
if ($user !== null) {
echo "Hello " . $user->getUsername() . ". Your email is " . $user->getEmail();
}
/* save a new record on Database */
var $newUser = new User('', 'Odysseus', 'Odysseus@ithaca.gr');
$userMapper->save($newUser);
/* fetch all from a table on Database */
var $usersArray = $userMapper->findAll();
?>

48
DataMapper/User.php Normal file
View File

@@ -0,0 +1,48 @@
<?php
namespace DesignPatterns;
/**
* DataMapper pattern
*
* This is our representation of a DataBase record in the memory
*
*/
class User
{
protected $userId;
protected $username;
protected $email;
public function __construct($id, $username, $email)
{
$this->id = $id;
$this->username = $username;
$this->email = $email;
}
public function getUserId() {
return $this->userId;
}
public function setUserID($userId) {
$this->userId = $userId;
}
public function getUsername() {
return $this->username;
}
public function setUsername($username) {
$this->username = $username;
}
public function getEmail() {
return $this->email;
}
public function setEmail($email) {
$this->email = $email;
}
}

111
DataMapper/UserMapper.php Normal file
View File

@@ -0,0 +1,111 @@
<?php
namespace DesignPatterns;
/**
* DataMapper pattern
*
* Purpose:
* A Data Mapper, is a Data Access Layer that performs bidirectional transfer of
* data between a persistent data store (often a relational database) and an in
* memory data representation (the domain layer). The goal of the pattern is to
* keep the in memory representation and the persistent data store independent of
* each other and the data mapper itself. The layer is composed of one or more
* mappers (or Data Access Objects), performing the data transfer. Mapper
* implementations vary in scope. Generic mappers will handle many different domain
* entity types, dedicated mappers will handle one or a few.
* (FROM http://en.wikipedia.org/wiki/Data_mapper_pattern)
*
*
* Examples:
* - DB Object Relational Mapper (ORM)
*
*/
class UserMapper
{
protected $_adapter;
public function __construct(array $options = null)
{
/**
* create new Database connector on $_adapter using specific table
*
* $_adapter var could be a specific to a table class or a generic
* interface for connecting to Database and do certain jobs
*/
}
/**
* saves a user object from memory to Database
*
* @return boolean
*/
public function save(User $user)
{
/* $data keys shoulds correspond to valid Table columns on the Database */
$data = array(
'userid' => $user->getUserId(),
'username' => $user->getUsername(),
'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);
return true;
} else {
$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
*
* @return User
*/
public function findById($id)
{
$result = $this->_adapter->find($id);
if (0 == count($result)) {
return;
}
$row = $result->current();
var user = new User();
$user->setUserID($row['userid']);
$user->setUsername($row['username']);
$user->setEmail($row['email']);
return user;
}
/**
* fetches an array from Database and returns an array of User objects
* located in memory
*
* @return array
*/
public function findAll()
{
$resultSet = $this->_adapter->findAll();
$entries = array();
foreach ($resultSet as $row) {
$entry = new User();
$user->setUserID($row['userid']);
$user->setUsername($row['username']);
$user->setEmail($row['email']);
$entries[] = $entry;
}
return $entries;
}
}