Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasverraes committed Mar 29, 2011
0 parents commit 9b0668b
Show file tree
Hide file tree
Showing 14 changed files with 792 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.project
.buildpath
.settings
build/
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2011 Mathias Verraes

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
Verraes\Money
=============

This is a PHP implementation of the Money pattern, as described in [Fowler2002].

The problem
-----------

From [Fowler2002]:

> A large proportion of the computers in this world manipulate money, so it's always puzzled me
> that money isn't actually a first class data type in any mainstream programming language. The
> lack of a type causes problems, the most obvious surrounding currencies. If all your calculations
> are done in a single currency, this isn't a huge problem, but once you involve multiple currencies
> you want to avoid adding your dollars to your yen without taking the currency differences into
> account. The more subtle problem is with rounding. Monetary calculations are often rounded to the
> smallest currency unit. When you do this it's easy to lose pennies (or your local equivalent)
> because of rounding errors.
The goal
--------

Implement a reusable Money class in PHP, using all the best practices and taking care of all the
subtle intricacies of handling money.

Usage
=====

<?php
use Verraes\Money\Money,
Verraes\Money\Usd,
Verraes\Money\Euro;
// One EURO, expressed in cents
$eur1 = new Money(100, new Euro);
// Shortcut
$eur2 = Money::euro(200);
Money::euro(300)->equals(
$eur1->add($eur2)
);

Inspiration
===========

* https://github.com/RubyMoney/money
* http://css.dzone.com/books/practical-php-patterns/basic/practical-php-patterns-value

* http://www.codeproject.com/KB/recipes/MoneyTypeForCLR.aspx
* http://www.michaelbrumm.com/money.html
* http://stackoverflow.com/questions/1679292/proof-that-fowlers-money-allocation-algorithm-is-correct
* http://timeandmoney.sourceforge.net/
* https://github.com/lucamarrocco/timeandmoney/blob/master/lib/money.rb

Bibliography
============

[Fowler2002]
Fowler, M., D. Rice, M. Foemmel, E. Hieatt, R. Mee, and R. Stafford, Patterns of Enterprise Application Architecture, Addison-Wesley, 2002.
http://martinfowler.com/books.html#eaa

http://en.wikipedia.org/wiki/ISO_4217

Todo
====

* https://github.com/RubyMoney/eu_central_bank
92 changes: 92 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated by PHP Project Wizard (PPW) 1.0.4 on Tue Mar 22 10:10:56 CET 2011 -->

<project name="fowler-money" default="build" basedir=".">
<property name="source" value="lib"/>

<target name="clean" description="Clean up and create artifact directories">
<delete dir="${basedir}/build/api"/>
<delete dir="${basedir}/build/code-browser"/>
<delete dir="${basedir}/build/coverage"/>
<delete dir="${basedir}/build/logs"/>
<delete dir="${basedir}/build/pdepend"/>

<mkdir dir="${basedir}/build/api"/>
<mkdir dir="${basedir}/build/code-browser"/>
<mkdir dir="${basedir}/build/coverage"/>
<mkdir dir="${basedir}/build/logs"/>
<mkdir dir="${basedir}/build/pdepend"/>
</target>

<target name="phpunit" description="Run unit tests using PHPUnit and generates junit.xml and clover.xml">
<exec executable="phpunit" failonerror="true"/>
</target>

<target name="parallelTasks" description="Run the pdepend, phpmd, phpcpd, phpcs, phpdoc and phploc tasks in parallel using a maximum of 2 threads.">
<parallel threadCount="2">
<sequential>
<antcall target="pdepend"/>
<antcall target="phpmd"/>
</sequential>
<antcall target="phpcpd"/>
<antcall target="phpcs"/>
<antcall target="phpdoc"/>
<antcall target="phploc"/>
</parallel>
</target>

<target name="pdepend" description="Generate jdepend.xml and software metrics charts using PHP_Depend">
<exec executable="pdepend">
<arg line="--jdepend-xml=${basedir}/build/logs/jdepend.xml
--jdepend-chart=${basedir}/build/pdepend/dependencies.svg
--overview-pyramid=${basedir}/build/pdepend/overview-pyramid.svg
${source}" />
</exec>
</target>

<target name="phpmd" description="Generate pmd.xml using PHPMD">
<exec executable="phpmd">
<arg line="${source}
xml
codesize,design,naming,unusedcode
--reportfile ${basedir}/build/logs/pmd.xml" />
</exec>
</target>

<target name="phpcpd" description="Generate pmd-cpd.xml using PHPCPD">
<exec executable="phpcpd">
<arg line="--log-pmd ${basedir}/build/logs/pmd-cpd.xml ${source}" />
</exec>
</target>

<target name="phploc" description="Generate phploc.csv">
<exec executable="phploc">
<arg line="--log-csv ${basedir}/build/logs/phploc.csv ${source}" />
</exec>
</target>

<target name="phpcs" description="Generate checkstyle.xml using PHP_CodeSniffer">
<exec executable="phpcs" output="/dev/null">
<arg line="--report=checkstyle
--report-file=${basedir}/build/logs/checkstyle.xml
--standard=PEAR
${source}" />
</exec>
</target>

<target name="phpdoc" description="Generate API documentation using PHPDocumentor">
<exec executable="phpdoc">
<arg line="-d ${source} -t ${basedir}/build/api" />
</exec>
</target>

<target name="phpcb" description="Aggregate tool output with PHP_CodeBrowser">
<exec executable="phpcb">
<arg line="--log ${basedir}/build/logs
--source ${source}
--output ${basedir}/build/code-browser" />
</exec>
</target>

<target name="build" depends="clean,parallelTasks,phpunit,phpcb"/>
</project>
24 changes: 24 additions & 0 deletions lib/MathiasVerraes/FowlerMoney/Currency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* This file is part of the Verraes\Money library
*
* Copyright (c) 2011 Mathias Verraes
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Verraes\Money;

interface Currency
{
/**
* @return string
*/
public function getName();

/**
* @return bool
*/
public function equals(Currency $currency);
}
30 changes: 30 additions & 0 deletions lib/MathiasVerraes/FowlerMoney/Euro.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* This file is part of the Verraes\Money library
*
* Copyright (c) 2011 Mathias Verraes
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Verraes\Money;

final class Euro implements Currency
{
/**
* @return string
*/
public function getName()
{
return 'EUR';
}

/**
* @return bool
*/
public function equals(Currency $currency)
{
return $this->getName() == $currency->getName();
}
}
18 changes: 18 additions & 0 deletions lib/MathiasVerraes/FowlerMoney/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* This file is part of the Verraes\Money library
*
* Copyright (c) 2011 Mathias Verraes
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Verraes\Money;

/**
* @see http://www.phpkode.com/tips/item/exception-best-practices-in-php-5-3/
*/
interface Exception
{
}
15 changes: 15 additions & 0 deletions lib/MathiasVerraes/FowlerMoney/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* This file is part of the Verraes\Money library
*
* Copyright (c) 2011 Mathias Verraes
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Verraes\Money;

class InvalidArgumentException extends \InvalidArgumentException implements Exception
{
}
Loading

0 comments on commit 9b0668b

Please sign in to comment.