Skip to content

Instantly share code, notes, and snippets.

@mbrowne
Last active December 31, 2015 21:59
Show Gist options
  • Select an option

  • Save mbrowne/8050338 to your computer and use it in GitHub Desktop.

Select an option

Save mbrowne/8050338 to your computer and use it in GitHub Desktop.

Revisions

  1. mbrowne revised this gist Dec 20, 2013. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions php5.4.php
    Original file line number Diff line number Diff line change
    @@ -30,9 +30,7 @@ function transfer() {
    */
    namespace UseCases\TransferMoney\Roles
    {
    use DCI\Role;

    class SourceAccount extends Role
    trait SourceAccount
    {
    /**
    * Withdraw the given amount from this account
    @@ -56,7 +54,7 @@ function transferOut($amount) {
    }
    }

    class DestinationAccount extends Role
    trait DestinationAccount
    {
    function deposit($amount) {
    $this->increaseBalance($amount);
  2. mbrowne revised this gist Dec 20, 2013. 1 changed file with 0 additions and 15 deletions.
    15 changes: 0 additions & 15 deletions php5.4.php
    Original file line number Diff line number Diff line change
    @@ -62,21 +62,6 @@ 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);
    }
    */
    }
    }

  3. mbrowne created this gist Dec 20, 2013.
    91 changes: 91 additions & 0 deletions php5.3.php
    Original 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;
    }
    }
    }
    107 changes: 107 additions & 0 deletions php5.4.php
    Original 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;
    }
    }
    }