-
-
Save codenamegary/10996302 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyProject; | |
class Person | |
{ | |
private $data; | |
/** | |
* | |
* [ | |
* 'id' => <id>, | |
* 'name' => 'name', | |
* 'insertDate' => '2014-04-17 15:53:00' | |
* ] | |
* | |
* @param array $data | |
**/ | |
public function __construct(array $data) | |
{ | |
$this->data = $data; | |
} | |
/** | |
* @return string | |
**/ | |
public function getInsertTime() | |
{ | |
return strtotime($this->data['insertDate']); | |
} | |
/** | |
* __get | |
* | |
* @param mixed string | |
* @access public | |
* @return mixed | |
*/ | |
public function __get($name) | |
{ | |
static $forbidden = array('insertDate'); | |
if (array_key_exists($name, $this->data) && !in_array($name, $forbidden)) { | |
switch ($name) { | |
case 'insertTime': | |
$ret = $this->getInsertTime(); | |
break; | |
default: | |
$ret = $this->data[$name]; | |
break; | |
} | |
} else { | |
throw new \DomainException("No such key: `$name`"); | |
} | |
return $ret; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyProject; | |
class PersonController { | |
/** | |
* @var MyProject\PersonRepository | |
*/ | |
protected $repository; | |
public function __construct(PersonRepository $repository) | |
{ | |
$this->repository = $repository; | |
} | |
public function show($id) | |
{ | |
if(!$person = $this->repository->findById($id)) doSome404Function(); | |
var_dump($person); | |
} | |
public function create() | |
{ | |
// DO NOT ACTUALLY DO THIS, JUST AN EXAMPLE | |
$input = $_POST; | |
$person = $this->repository->create($input); | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyProject; | |
use Person as Entity; | |
class PersonRepository { | |
public function make(array $data = array()) | |
{ | |
return new Entity($data); | |
} | |
public function findById($id) | |
{ | |
// Do pdo / sql query to find a version of a person based on $id | |
$data = $resultOfQuery; | |
return $data ? $this->make($data) : false; | |
} | |
public function save(Entity $entity) | |
{ | |
// Do pdo / sql query to persist $entity to the database | |
// You should throw an exception here if it fails | |
return $entity; | |
} | |
public function create(array $data = array()) | |
{ | |
return $this->save($this->make($data)); | |
} | |
public function delete(Entity $entity) | |
{ | |
// Do pdo / sql query to delete $entity | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment