Skip to content

Commit

Permalink
Merge pull request #308 from moneyphp/doc
Browse files Browse the repository at this point in the history
Refactor documentation structure
  • Loading branch information
sagikazarmark committed Oct 21, 2016
2 parents e62c34f + 8a20262 commit 2f918f1
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 121 deletions.
36 changes: 0 additions & 36 deletions doc/Immutability.rst

This file was deleted.

29 changes: 0 additions & 29 deletions doc/IntegerLimit.rst

This file was deleted.

25 changes: 0 additions & 25 deletions doc/Introduction.rst

This file was deleted.

13 changes: 0 additions & 13 deletions doc/Json.rst

This file was deleted.

13 changes: 13 additions & 0 deletions doc/_static/custom.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
.row-even .line-block, .row-odd .line-block {
margin-left: 0;
}

.versionmodified {
font-weight: bold;
}

.wy-menu-vertical p.caption {
color: #b3b3b3;
margin-top: 16px;
margin-bottom: 0;
}
.wy-menu-vertical p.caption .caption-text {
font-size: 120%;
}
87 changes: 87 additions & 0 deletions doc/concept.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
Concept
=======

This section introduces the concept and basic features of the library

Immutability
------------

Jim and Hannah both want to buy a copy of book priced at EUR 25.

.. code-block:: php
use Money\Money;
$jimPrice = $hannahPrice = Money::EUR(2500);
Jim has a coupon for EUR 5.

.. code-block:: php
$coupon = Money::EUR(500);
$jimPrice->subtract($coupon);
Because ``$jimPrice`` and ``$hannahPrice`` are the same object, you'd expect Hannah to now have the reduced
price as well. To prevent this problem, Money objects are **immutable**. With the code above, both
``$jimPrice`` and ``$hannahPrice`` are still EUR 25:

.. code-block:: php
$jimPrice->equals($hannahPrice); // true
The correct way of doing operations is:

.. code-block:: php
$jimPrice = $jimPrice->subtract($coupon);
$jimPrice->lessThan($hannahPrice); // true
$jimPrice->equals(Money::EUR(2000)); // true
Integer Limit
-------------

Although in real life it is highly unprobable, you might have to deal with money values greater than
the integer limit of your system (``PHP_INT_MAX`` constant represents the maximum integer value).

In order to bypass this limit, we introduced `Calculators`. Based on your environment, Money automatically
picks the best internally and globally. The following implementations are available:

- BC Math (requires `bcmath` extension)
- GMP (requires `gmp` extension)
- Plain integer

Calculators are checked for availability in the order above. If no suitable Calculator is found
Money silently falls back to the integer implementation.

Because of PHP's integer limit, money values are stored as string internally and
``Money::getAmount`` also returns string.

.. code-block:: php
use Money\Currency;
use Money\Money;
$hugeAmount = new Money('12345678901234567890', new Currency('USD'));
.. note::
Remember, because of the integer limit in PHP, you should inject a string that represents your huge amount.


JSON
----

If you want to serialize a money object into a JSON, you can just use the PHP method ``json_encode`` for that.
Please find below example of how to achieve this.

.. code-block:: php
use Money\Money;
$money = Money::USD(350);
$json = json_encode($money);
echo $json; // outputs '{"amount":"350","currency":"USD"}'
2 changes: 1 addition & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

# General information about the project.
project = u'Money'
copyright = u'2011-2016, Mathias Verraes'
copyright = u'2011-2016, Mathias Verraes, 2016 The Money PHP Team'
author = u'The Money PHP Team'

# The version info for the project you're documenting, acts as replacement for
Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion doc/Bitcoin.rst → doc/features/bitcoin.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _bitcoin:

Bitcoin
=======

Expand Down Expand Up @@ -32,4 +34,4 @@ Please see the example below how to use the Bitcoin currency:
In most cases you probably don't know the exact currency you are going to format or parse.
For such scenarios, we have an aggregate formatter and a parser which lets you configure multiple parsers
and then choose the best based on the value. See more in :doc:`Formatting` and :doc:`Parsing` section.
and then choose the best based on the value. See more in :ref:`Formatting <formatting>` and :ref:`Parsing <parsing>` section.
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion doc/Formatting.rst → doc/features/formatting.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _formatting:

Formatting
==========

Expand Down Expand Up @@ -93,4 +95,4 @@ and want to support multiple currencies.
Bitcoin Formatter
-----------------

See :doc:`Bitcoin`.
See :ref:`Bitcoin <bitcoin>`.
4 changes: 3 additions & 1 deletion doc/Parsing.rst → doc/features/parsing.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _parsing:

Parsing
=======

Expand Down Expand Up @@ -87,4 +89,4 @@ This is very useful if you want to use one parser as a service in DI context.
Bitcoin Parser
--------------

See :doc:`Bitcoin`.
See :ref:`Bitcoin <bitcoin>`.
File renamed without changes.
64 changes: 50 additions & 14 deletions doc/index.rst
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
Money for PHP
=============

This library intends to provide tools for storing and using monetary values in an easy, yet powerful way.


Why a Money library for PHP?
----------------------------

Also see http://blog.verraes.net/2011/04/fowler-money-pattern-in-php/

This is a PHP implementation of the Money pattern, as described in [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.

.. [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
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.

.. toctree::
:hidden:

Money <self>
getting-started
concept
inspiration

.. toctree::
:maxdepth: 2

Introduction
GettingStarted
Immutability
Allocation
Currencies
CurrencyConversion
Json
IntegerLimit
Formatting
Parsing
Bitcoin
Inspiration
:hidden:
:caption: Features
:maxdepth: 3

features/allocation
features/bitcoin
features/currencies
features/currency-conversion
features/formatting
features/parsing

.. |clearfloat| raw:: html

<div style="clear:left"></div>
File renamed without changes.

0 comments on commit 2f918f1

Please sign in to comment.