Created
June 4, 2013 13:45
-
-
Save rafi/5706033 to your computer and use it in GitHub Desktop.
Goofing around with DCI, inspired by https://gist.github.com/Ikke/4075281
This file contains hidden or 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 | |
Trait MethodDelegator | |
{ | |
private $roles = array(); | |
private $method_map = array(); | |
public function delegate($class) | |
{ | |
$role = new $class($this); | |
$this->roles[$class] = $role; | |
foreach(get_class_methods($class) as $method) | |
{ | |
$this->method_map[$method] = $role; | |
} | |
} | |
public function remove_delegation($class) | |
{ | |
$role = $this->roles[$class]; | |
unset($this->roles[$class]); | |
$this->method_map = array_filter( | |
$this->method_map, | |
function($stored_role) use($role) | |
{ | |
return $stored_role !== $role; | |
} | |
); | |
} | |
public function __call($method, $args) | |
{ | |
if ( ! array_key_exists($method, $this->method_map)) | |
throw new Exception("Method $method doesn't exist on this object"); | |
call_user_func_array( | |
array($this->method_map[$method], $method), $args | |
); | |
} | |
} | |
class Role { | |
private $entity; | |
public function __construct($entity) | |
{ | |
$this->entity = $entity; | |
} | |
public function __get($property) | |
{ | |
return $this->entity->$property; | |
} | |
public function __set($property, $value) | |
{ | |
$this->entity->$property = $value; | |
} | |
} | |
class Repository { | |
public function create() {} | |
} | |
// ------------------------------------------------------------ | |
class Role_Account_Guest extends Role { | |
public function assign_values(array $data) | |
{ | |
// echo 'Role Account Guest:<br />'; | |
// var_dump($this); | |
$this->title = $data['title']; | |
$this->active = (bool) $data['active']; | |
$this->created = time(); | |
$this->modified = NULL; | |
$this->deleted = NULL; | |
} | |
} | |
class Model_Account { | |
use MethodDelegator; | |
public $id; | |
public $title; | |
public $active; | |
public $created; | |
public $modified; | |
public $deleted; | |
} | |
// ------------------------------------------------------------ | |
class Context_Account_Add { | |
protected $_data; | |
protected $_account; | |
protected $_repo; | |
public function __construct(array $data, Model_Account $account, Repository $repo) | |
{ | |
$this->_data = $data; | |
$this->_account = $account; | |
$this->_repo = $repo; | |
} | |
public function execute() | |
{ | |
$this->_account->delegate('Role_Account_Guest'); | |
// Call Role_Account_Guest::assign_values() | |
$this->_account->assign_values($this->_data); | |
// Store | |
$this->_repo->create($this->_account); | |
echo 'Context Account Add:<br />'; | |
var_dump($this->_account); | |
} | |
} | |
// ------------------------------------------------------------ | |
$context = new Context_Account_Add( | |
[ 'title' => 'Test account', 'active' => 1 ], | |
new Model_Account, | |
new Repository | |
); | |
$context->execute(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment