Skip to content

Instantly share code, notes, and snippets.

@ahmedofali
Last active September 22, 2017 11:55
Show Gist options
  • Save ahmedofali/c70d569668858e29851f8963ae25b939 to your computer and use it in GitHub Desktop.
Save ahmedofali/c70d569668858e29851f8963ae25b939 to your computer and use it in GitHub Desktop.
Data Mapper Design Pattern Complete Example
<?php
namespace DPatterns;
use \DPatterns\PostMemoryStorage;
use \DPatterns\Post;
use \DPatterns\PostMapper;
use \PHPUnit\Framework\TestCase;
class DataMapperTest extends TestCase
{
public function testCanMapUserFromStorage()
{
// instantiating data in the database
$storage = new PostMemoryStorage([1 => ['post_name' => 'orm', 'post_body' => 'Learning Orm']]);
// Mapping Layer
$mapper = new PostMapper( $storage );
$post = $mapper->find_by_id(1);
$this->assertInstanceOf(Post::class, $post);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testWillNotMapInvalidData()
{
$storage = new PostMemoryStorage([]);
$mapper = new PostMapper( $storage );
$mapper->find_by_id( 5 );
}
}
<?php namespace DPatterns;
interface PersistenceStorage {
public function find( int $id );
}
<?php namespace DPatterns ;
/**
* Class Post
* Our Post Class Which we specify Post attributes
*
* @package DPatterns
*/
class Post{
protected $id ;
protected $post_name ;
protected $post_body ;
function __construct( $post_name , $post_body )
{
$this->post_name = $post_name ;
$this->post_body = $post_body ;
}
public static function create_from_state( $state ) : Post
{
return new self(
$state['post_name'],
$state['post_body']
);
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getPostName()
{
return $this->post_name;
}
/**
* @param mixed $post_name
*/
public function setPostName($post_name)
{
$this->post_name = $post_name;
}
/**
* @return mixed
*/
public function getPostBody()
{
return $this->post_body;
}
/**
* @param mixed $post_body
*/
public function setPostBody($post_body)
{
$this->post_body = $post_body;
}
}
<?php namespace DPatterns ;
use DPatterns\PersistenceStorage ;
use DPatterns\Post ;
class PostMapper{
protected $PersistenceStorage ;
/**
* PostMapper constructor.
* @Demonstration_for_learning
* * Whatever The Persistence Storage Method you use it must implement the Interface PersistenceStorage
* * which Ensures Consistency Across All Storage Methods Used .
*
* @param PersistenceStorage $PersistenceStorage
*/
function __construct( PersistenceStorage $PersistenceStorage )
{
$this->PersistenceStorage = $PersistenceStorage ;
}
public function find_by_id( int $id )
{
$result = $this->PersistenceStorage->find( $id );
if( is_null( $result ) ){
throw new \InvalidArgumentException("Post #$id not found");
}
return $this->mapRowToPost( $result );
}
private function mapRowToPost( $post ){
return Post::create_from_state( $post );
}
}
<?php namespace DPatterns ;
use DPatterns\PersistenceStorage ;
/**
* Class PostMemoryStorage
* For Simplicity We will make it in memory data store
*
* @package DPatterns
*/
class PostMemoryStorage implements PersistenceStorage{
private $data = [] ;
function __construct( array $data )
{
$this->data = $data;
}
/**
* @param int $id
* @return mixed|null
*/
public function find( int $id )
{
if ( isset( $this->data[$id] ) ) {
return $this->data[$id];
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment