Skip to content

Instantly share code, notes, and snippets.

@mageekguy
Last active August 29, 2015 14:13
Show Gist options
  • Save mageekguy/91912fccd7aaaae4141d to your computer and use it in GitHub Desktop.
Save mageekguy/91912fccd7aaaae4141d to your computer and use it in GitHub Desktop.
East oriented account transfert
<?php
namespace east;
class amount
{
private
$value
;
function __construct($value)
{
$this->value = $value;
}
function __get($property)
{
return $property === 'value' ? $this->value : null;
}
}
class balance
{
private
$amount
;
function __construct(amount $amount)
{
$this->amount = $amount;
}
function add(amount $amount)
{
return new self(new amount($this->amount->value + $amount->value));
}
function sub(amount $amount)
{
return new self(new amount($this->amount->value - $amount->value));
}
}
class account
{
private
$balance
;
function __construct(balance $balance)
{
$this->balance = $balance;
}
function add(amount $amount)
{
$this->balance = $this->balance->add($amount);
return $this;
}
function remove(amount $amount)
{
$this->balance = $this->balance->sub($amount);
return $this;
}
}
class transaction
{
function __construct(amount $amount, account $from, account $to)
{
$this->amount = $amount;
$this->to = $to;
$this->from = $from;
}
function execute()
{
$this->from->remove($this->amount);
$this->to->add($this->amount);
return $this;
}
}
$to = new account(new balance(new amount(10)));
$from = new account(new balance(new amount(5)));
(new transaction(new amount(2), $from, $to))->execute();
var_dump($from, $to);
(new transaction(new amount(2), $to, $from))->execute();
var_dump($from, $to);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment