Skip to content

Instantly share code, notes, and snippets.

@mbrowne
Last active December 31, 2015 21:59
Show Gist options
  • Save mbrowne/8050338 to your computer and use it in GitHub Desktop.
Save mbrowne/8050338 to your computer and use it in GitHub Desktop.
DCI for PHP - example usage
<?php
namespace UseCases
{
class TransferMoney extends \DCI\Context
{
//These would ideally be private but they need to be public so that the roles can access them,
//since PHP doesn't support inner classes
public $sourceAccount;
public $destinationAccount;
public $amount;
function __construct($sourceAccount, $destinationAccount, $amount) {
$this->sourceAccount = $sourceAccount->addRole('SourceAccount', $this);
$this->destinationAccount = $destinationAccount->addRole('DestinationAccount', $this);
$this->amount = $amount;
}
/**
* Transfer the amount from the source account to the destination account
*/
function transfer() {
$this->sourceAccount->transferOut($this->amount);
}
}
}
/**
* Roles are defined in a sub-namespace of the context as a workaround for the fact that
* PHP doesn't support inner classes
*/
namespace UseCases\TransferMoney\Roles
{
use DCI\Role;
class SourceAccount extends Role
{
/**
* Withdraw the given amount from this account
* @param float $amount
*
* Note that we could alternatively have retrieved the amount using $this->context->amount
* rather than having an $amount parameter here.
*/
function withdraw($amount) {
$this->decreaseBalance($amount);
//update transaction log...
}
/**
* Transfer the given amount from ("out" of) this account to the destination account
* @param float $amount
*/
function transferOut($amount) {
$this->context->destinationAccount->deposit($amount);
$this->withdraw($amount);
}
}
class DestinationAccount extends Role
{
function deposit($amount) {
$this->increaseBalance($amount);
//update transaction log...
}
}
}
namespace DataObjects
{
class Account extends \DCI\RolePlayer
{
protected $balance = 0;
function __construct($initialBalance) {
$this->balance = $initialBalance;
}
function getBalance() {
return $this->balance;
}
function increaseBalance($amount) {
$this->balance += $amount;
}
function decreaseBalance($amount) {
$this->balance -= $amount;
}
}
}
<?php
namespace UseCases
{
class TransferMoney extends \DCI\Context
{
//These would ideally be private but they need to be public so that the roles can access them,
//since PHP doesn't support inner classes
public $sourceAccount;
public $destinationAccount;
public $amount;
function __construct($sourceAccount, $destinationAccount, $amount) {
$this->sourceAccount = $sourceAccount->addRole('SourceAccount', $this);
$this->destinationAccount = $destinationAccount->addRole('DestinationAccount', $this);
$this->amount = $amount;
}
/**
* Transfer the amount from the source account to the destination account
*/
function transfer() {
$this->sourceAccount->transferOut($this->amount);
}
}
}
/**
* Roles are defined in a sub-namespace of the context as a workaround for the fact that
* PHP doesn't support inner classes
*/
namespace UseCases\TransferMoney\Roles
{
trait SourceAccount
{
/**
* Withdraw the given amount from this account
* @param float $amount
*
* Note that we could alternatively have retrieved the amount using $this->context->amount
* rather than having an $amount parameter here.
*/
function withdraw($amount) {
$this->decreaseBalance($amount);
//update transaction log...
}
/**
* Transfer the given amount from ("out" of) this account to the destination account
* @param float $amount
*/
function transferOut($amount) {
$this->context->destinationAccount->deposit($amount);
$this->withdraw($amount);
}
}
trait DestinationAccount
{
function deposit($amount) {
$this->increaseBalance($amount);
//update transaction log...
}
}
}
namespace DataObjects
{
class Account
{
use \DCI\RolePlayer;
protected $balance = 0;
function __construct($initialBalance) {
$this->balance = $initialBalance;
}
function getBalance() {
return $this->balance;
}
function increaseBalance($amount) {
$this->balance += $amount;
}
function decreaseBalance($amount) {
$this->balance -= $amount;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment