Skip to content

Add money pattern #326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions More/Money/Money.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace DesignPatterns\More\Money;

use Exception;

class Money
{
/**
* Money constructor.
* @param $currency
* @param $amount
*/
private function __construct($currency, $amount)
{
$this->currency = $currency;
$this->amount = $amount;
}

/**
* Create money from string input
*
* @param $currency
* @param $amount
* @return Money
*/
public static function fromString($currency, $amount): Money
{
return new Money($currency, $amount);
}

/**
* Return money represented as currency + amount
*
* @return string
*/
public function __toString(): String
{
return $this->amount . "" . $this->currency;
}

/**
* Get only amount
*
* @return mixed
*/
private function getAmount()
{
return $this->amount;
}

/**
* Get currency
*
* @return mixed
*/
private function getCurrency()
{
return $this->currency;
}

/**
* Subtract money instances (as value objects)
*
* @param Money $money
* @return Money
* @throws Exception
*/
public function add(Money $money)
{
if ($money->getCurrency() != $this->getCurrency()) {
throw new Exception('Money should have equal currencies');
}
return new Money($money->getCurrency(), $money->getAmount() + $this->getAmount());
}

/**
* Compare by value
*
* @param Money $money
* @return bool
*/
public function equals(Money $money): bool
{
return $this->getCurrency() . "" . $this->getAmount() == $money->getCurrency() . "" . $money->getAmount();
}

/** mo
* More user friendly interface for creating money
*
* @param $method
* @param $args
* @return Money
*/
public static function __callStatic($method, $args): Money
{
return Money::fromString($method, $args[0]);
}
}
43 changes: 43 additions & 0 deletions More/Money/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
`Money`__
==============

Purpose
-------
Represents money as value object (immutable, compared by value and protects currency invariant in most basic form)


Examples
--------

Please take a look at provided test (Money/Tests/MoneyTest.php) to see how this pattern can be used.

UML Diagram
-----------

.. image:: uml/money.png
:alt: Alt Money UML Diagram
:align: center

Code
----

You can also find this code on `GitHub`_

Money.php

.. literalinclude:: Money.php
:language: php
:linenos:


Test
----

Tests/MoneyTest.php

.. literalinclude:: Tests/MoneyTest.php
:language: php
:linenos:

.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/More/Money
.. __: https://martinfowler.com/eaaCatalog/money.html
72 changes: 72 additions & 0 deletions More/Money/Tests/MoneyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

use DesignPatterns\More\Money\Money;
use PHPUnit\Framework\TestCase;

class MoneyTest extends TestCase
{
/**
* Can we crete money from input strings?
*
* @throws \ReflectionException
*/
public function testMoneyCanBeCreatedFromString()
{
$money = Money::fromString('EUR', 200);

$reflector = new ReflectionClass($money);
$method = $reflector->getMethod('getAmount');
$method->setAccessible(true);
$result = $method->invokeArgs($money, []);

$this->assertEquals($result, 200);
}

/**
* Test two money objects have equals values
*/
public function testMoneyAmountIsEqual()
{
$money = Money::fromString('EUR', 300);
$money1 = Money::fromString('EUR', 300);

$this->assertTrue($money->equals($money1));
}

/**
* Can we subtract two money objects?
*
* @throws \ReflectionException
*/
public function testMoneyCanBeSubtracted()
{
$money = Money::fromString('EUR', 200);
$money1 = Money::fromString('EUR', 882);
$newAmount = $money->add($money1);

$reflector = new ReflectionClass($newAmount);
$method = $reflector->getMethod('getAmount');
$method->setAccessible(true);
$result = $method->invokeArgs($newAmount, []);

$this->assertEquals($result, 1082);
}

/**
* Can we create money instance using more friendly interface (static)
*
* @throws \ReflectionException
*/
public function testMoneyAndCurrencyCanBeCreatedStatically()
{
$money = Money::fromString('EUR', 200);
$newAmount = Money::EUR(150)->add($money);

$reflector = new ReflectionClass($newAmount);
$method = $reflector->getMethod('getAmount');
$method->setAccessible(true);
$result = $method->invokeArgs($newAmount, []);

$this->assertEquals($result, 350);
}
}
Binary file added More/Money/uml/money.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added More/Money/uml/moneySketch.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.