DesignPatternsPHP/DataMapper/UserMapper.php

109 lines
2.8 KiB
PHP
Raw Normal View History

2013-05-11 20:07:59 +03:00
<?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)
*
2013-05-13 20:40:21 +03:00
*
2013-05-11 20:07:59 +03:00
* Examples:
* - DB Object Relational Mapper (ORM)
*
*/
class UserMapper
{
2013-05-13 20:40:21 +03:00
protected $_adapter;
2013-05-11 20:07:59 +03:00
public function __construct(array $options = null)
{
2013-05-13 20:40:21 +03:00
/**
* 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
*/
2013-05-11 20:07:59 +03:00
}
2013-05-13 20:40:21 +03:00
/**
* saves a user object from memory to Database
*
* @return boolean
*/
2013-05-11 20:07:59 +03:00
public function save(User $user)
{
$data = array(
'userid' => $user->getUserId(),
'username' => $user->getUsername(),
'email' => $user->getEmail(),
);
if (null === ($id = $user->getUserId())) {
unset($data['userid']);
2013-05-13 20:40:21 +03:00
$this->_adapter->insert($data);
return true;
2013-05-11 20:07:59 +03:00
} else {
2013-05-13 20:40:21 +03:00
$this->_adapter->update($data, array('userid = ?' => $id));
return true;
2013-05-11 20:07:59 +03:00
}
2013-05-13 20:40:21 +03:00
return false;
2013-05-11 20:07:59 +03:00
}
2013-05-13 20:40:21 +03:00
/**
* finds a user from Database based on ID and returns a User object located
* in memory
*
* @return User
*/
public function findById($id)
2013-05-11 20:07:59 +03:00
{
2013-05-13 20:40:21 +03:00
$result = $this->_adapter->find($id);
2013-05-11 20:07:59 +03:00
if (0 == count($result)) {
return;
}
$row = $result->current();
2013-05-13 20:40:21 +03:00
var user = new User();
$user->setUserID($row['userid']);
$user->setUsername($row['username']);
$user->setEmail($row['email']);
return user;
2013-05-11 20:07:59 +03:00
}
2013-05-13 20:40:21 +03:00
/**
* fetches an array from Database and returns an array of User objects
* located in memory
*
* @return array
*/
public function findAll()
2013-05-11 20:07:59 +03:00
{
2013-05-13 20:40:21 +03:00
$resultSet = $this->_adapter->findAll();
2013-05-11 20:07:59 +03:00
$entries = array();
foreach ($resultSet as $row) {
2013-05-13 20:40:21 +03:00
2013-05-11 20:07:59 +03:00
$entry = new User();
2013-05-13 20:40:21 +03:00
$user->setUserID($row['userid']);
$user->setUsername($row['username']);
$user->setEmail($row['email']);
2013-05-11 20:07:59 +03:00
$entries[] = $entry;
}
return $entries;
}
}