mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-06 15:06:31 +02:00
PHP7 DataMapper
This commit is contained in:
@@ -2,107 +2,44 @@
|
||||
|
||||
namespace DesignPatterns\Structural\DataMapper;
|
||||
|
||||
/**
|
||||
* class UserMapper.
|
||||
*/
|
||||
class UserMapper
|
||||
{
|
||||
/**
|
||||
* @var DBAL
|
||||
* @var StorageAdapter
|
||||
*/
|
||||
protected $adapter;
|
||||
private $adapter;
|
||||
|
||||
/**
|
||||
* @param DBAL $dbLayer
|
||||
* @param StorageAdapter $storage
|
||||
*/
|
||||
public function __construct(DBAL $dbLayer)
|
||||
public function __construct(StorageAdapter $storage)
|
||||
{
|
||||
$this->adapter = $dbLayer;
|
||||
$this->adapter = $storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* saves a user object from memory to Database.
|
||||
*
|
||||
* @param User $user
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function save(User $user)
|
||||
{
|
||||
/* $data keys should 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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* finds a user from Database based on ID and returns a User object located
|
||||
* in memory.
|
||||
* finds a user from storage based on ID and returns a User object located
|
||||
* in memory. Normally this kind of logic will be implemented using the Repository pattern.
|
||||
* However the important part is in mapRowToUser() below, that will create a business object from the
|
||||
* data fetched from storage
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public function findById($id)
|
||||
public function findById(int $id): User
|
||||
{
|
||||
$result = $this->adapter->find($id);
|
||||
|
||||
if (0 == count($result)) {
|
||||
if ($result === null) {
|
||||
throw new \InvalidArgumentException("User #$id not found");
|
||||
}
|
||||
$row = $result->current();
|
||||
|
||||
return $this->mapObject($row);
|
||||
return $this->mapRowToUser($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* fetches an array from Database and returns an array of User objects
|
||||
* located in memory.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function findAll()
|
||||
private function mapRowToUser(array $row): User
|
||||
{
|
||||
$resultSet = $this->adapter->findAll();
|
||||
$entries = array();
|
||||
|
||||
foreach ($resultSet as $row) {
|
||||
$entries[] = $this->mapObject($row);
|
||||
}
|
||||
|
||||
return $entries;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps a table row to an object.
|
||||
*
|
||||
* @param array $row
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
protected function mapObject(array $row)
|
||||
{
|
||||
$entry = new User();
|
||||
$entry->setUserID($row['userid']);
|
||||
$entry->setUsername($row['username']);
|
||||
$entry->setEmail($row['email']);
|
||||
|
||||
return $entry;
|
||||
return User::fromState($row);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user