Last active
December 31, 2015 21:59
-
-
Save mbrowne/8050338 to your computer and use it in GitHub Desktop.
Revisions
-
mbrowne revised this gist
Dec 20, 2013 . 1 changed file with 2 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -30,9 +30,7 @@ function transfer() { */ namespace UseCases\TransferMoney\Roles { trait SourceAccount { /** * Withdraw the given amount from this account @@ -56,7 +54,7 @@ function transferOut($amount) { } } trait DestinationAccount { function deposit($amount) { $this->increaseBalance($amount); -
mbrowne revised this gist
Dec 20, 2013 . 1 changed file with 0 additions and 15 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -62,21 +62,6 @@ function deposit($amount) { $this->increaseBalance($amount); //update transaction log... } } } -
mbrowne created this gist
Dec 20, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,91 @@ <?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; } } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,107 @@ <?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... } /* These methods are here for illustrative purposes only. They're commented out because they're not used in the context as currently written, so there's no reason for them to exist in this example (since roles are context-specific) function transferIn($amount) { $this->withdraw($amount); $this->context->sourceAccount->deposit($amount); } function withdraw($amount) { $this->decreaseBalance($amount); } */ } } 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; } } }