Skip to content

Instantly share code, notes, and snippets.

@agoalofalife
Created December 28, 2016 09:12
Show Gist options
  • Save agoalofalife/422bf945f8aaa9e9091f3189d04734c0 to your computer and use it in GitHub Desktop.
Save agoalofalife/422bf945f8aaa9e9091f3189d04734c0 to your computer and use it in GitHub Desktop.
Data Mapper
<?php
abstract class DomainObjectAbstract
{
protected $_id = null;
/**
* Get the ID of this object (unique to the
* object type)
*
* @return int
*/
public function getId()
{
return $this->_id;
}
/**
* Set the id for this object.
*
* @param int $id
* @return int
* @throws Exception If the id on the object is already set
*/
public function setId($id)
{
if (!is_null($this->_id)) {
throw new Exception('ID is immutable');
}
return $this->_id = $id;
}
}
<?php
abstract class MapperAbstract
{
/**
* Create a new instance of the DomainObject that this
* mapper is responsible for. Optionally populating it
* from a data array.
*
* @param array $data
* @return DomainObjectAbstract
*/
public function create(array $data = null) {
$obj = $this->_create();
if ($data) {
$obj = $this->populate($obj, $data);
}
return $obj;
}
/**
* Save the DomainObject
*
* Store the DomainObject in persistent storage. Either insert
* or update the store as required.
*
* @param DomainObjectAbstract $obj
*/
public function save(DomainObjectAbstract $obj)
{
if (is_null($obj->getId())) {
$this->_insert($obj);
} else {
$this->_update($obj);
}
}
/**
* Delete the DomainObject
*
* Delete the DomainObject from persistent storage.
*
* @param DomainObjectAbstract $obj
*/
public function delete(DomainObjectAbstract $obj)
{
$this->_delete($obj);
}
/**
* Populate the DomainObject with the values
* from the data array.
*
* To be implemented by the concrete mapper class
*
* @param DomainObjectAbstract $obj
* @param array $data
* @return DomainObjectAbstract
*/
abstract public function populate(DomainObjectAbstract $obj, array $data);
/**
* Create a new instance of a DomainObject
*
* @return DomainObjectAbstract
*/
abstract protected function _create();
/**
* Insert the DomainObject to persistent storage
*
* @param DomainObjectAbstract $obj
*/
abstract protected function _insert(DomainObjectAbstract $obj);
/**
* Update the DomainObject in persistent storage
*
* @param DomainObjectAbstract $obj
*/
abstract protected function _update(DomainObjectAbstract $obj);
/**
* Delete the DomainObject from peristent Storage
*
* @param DomainObjectAbstract $obj
*/
abstract protected function _delete(DomainObjectAbstract $obj);
}
<?php
class User extends DomainObjectAbstract
{
public $firstname;
public $lastname;
public $username;
/**
* Get the full name of the User
*
* Demonstrates how other functions can be
* added to the DomainObject
*
* @return string
*/
public function getName()
{
return $this->firstname . ' ' . $this->lastname;
}
}
<?php
class UserMapper extends MapperAbstract
{
/**
* Fetch a user object by ID
*
* An example skeleton of a "Fetch" function showing
* how the database data ($dataFromDb) is used to
* create a new User instance via the create function.
*
* @param string $id
* @return User
*/
public function findById($id)
{
// Query database for User with $id
// ...
$dataFromDb = array(
'id' => $id,
'firstname' => 'Brenton',
'lastname' => '',
'username' => 'Tekerson',
);
return $this->create($dataFromDb);
}
/**
* Poplate the User (DomainObject) with
* the data array.
*
* This is a very simple example, but the mapping
* can be as complex as required.
*
* @param DomainObjectAbstract $obj
* @param array $data
* @return User
*/
public function populate(DomainObjectAbstract $obj, array $data)
{
$obj->setId($data['id']);
$obj->firstname = $data['firstname'];
$obj->lastname = $data['lastname'];
$obj->username = $data['username'];
return $obj;
}
/**
* Create a new User DomainObject
*
* @return User
*/
protected function _create()
{
return new User();
}
/**
* Insert the DomainObject in persistent storage
*
* This may include connecting to the database
* and running an insert statement.
*
* @param DomainObjectAbstract $obj
*/
protected function _insert(DomainObjectAbstract $obj)
{
// ...
}
/**
* Update the DomainObject in persistent storage
*
* This may include connecting to the database
* and running an update statement.
*
* @param DomainObjectAbstract $obj
*/
protected function _update(DomainObjectAbstract $obj)
{
// ...
}
/**
* Delete the DomainObject from persistent storage
*
* This may include connecting to the database
* and running a delete statement.
*
* @param DomainObjectAbstract $obj
*/
protected function _delete(DomainObjectAbstract $obj)
{
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment