Skip to content

Commit 93d67b4

Browse files
committed
merge conflicts
2 parents 4519b10 + f30fd18 commit 93d67b4

File tree

6 files changed

+2488
-189
lines changed

6 files changed

+2488
-189
lines changed

README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
Money
22
=====
33

4-
[![Build Status](https://api.travis-ci.org/mathiasverraes/money.png?branch=master)](http://travis-ci.org/mathiasverraes/money)
4+
Fork from [Mathias Verraes implementation of Fowley's money pattern](https://github.com/mathiasverraes/money).
5+
6+
The current version of the fork divverges from Fowley's pattern by using floats as parameters on the Money constructor. This was introduced in order to cater to the issue of large monetary entities overflowing PHP int size on 32 bits platforms. This will be dealt with on later releases of this fork, so expect API breakage on minor releases.
57

68
PHP 5.3+ library to make working with money safer, easier, and fun!
79

@@ -15,7 +17,7 @@ you need to represent money, use this Money value object.
1517

1618
use Money\Money;
1719

18-
$fiveEur = Money::EUR(500);
20+
$fiveEur = Money::EUR(5);
1921
$tenEur = $fiveEur->add($fiveEur);
2022

2123
list($part1, $part2, $part3) = $tenEur->allocate(array(1, 1, 1));
@@ -35,7 +37,7 @@ Install the library using [composer][1]. Add the following to your `composer.jso
3537
```json
3638
{
3739
"require": {
38-
"mathiasverraes/money": "dev-master"
40+
"hemeragestao/money": "dev-master"
3941
},
4042
"minimum-stability": "dev"
4143
}
@@ -47,6 +49,26 @@ Now run the `install` command.
4749
$ composer.phar install
4850
```
4951

52+
53+
Added Features
54+
--------------
55+
This fork replaces the maping of currencies from a simple ISO 4217 map from openexchangerates.org with the data contained in [Ruby's Money](https://github.com/RubyMoney) mapping. This allows for a few additional features, including generating formatted output string for the Money objects.
56+
Also, the Money object constructor takes a float parameter, using it as the amount instead of units. This will be changed on later releases.
57+
58+
```php
59+
<?php
60+
61+
use Money\Money;
62+
63+
echo Money::EUR(5.32)->formattedString();
64+
```
65+
66+
Output:
67+
```
68+
€ 5.32
69+
```
70+
71+
5072
Integration
5173
-----------
5274

composer.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "mathiasverraes/money",
2+
"name": "money/money",
33
"description": "PHP implementation of Fowler's Money pattern",
44
"type": "library",
55
"keywords": [ "Money", "Value Object", "Generic Sub-domain" ],
@@ -9,6 +9,14 @@
99
{
1010
"name": "Mathias Verraes",
1111
"email": "[email protected]"
12+
},
13+
{
14+
"name": "Gustavo Santos",
15+
"email": "[email protected]"
16+
},
17+
{
18+
"name": "Hassan Amouhzi",
19+
"email": "[email protected]"
1220
}
1321
],
1422
"require": {
@@ -19,7 +27,7 @@
1927
},
2028
"extra": {
2129
"branch-alias": {
22-
"dev-master": "1.3.x-dev"
30+
"dev-master": "1.4.x-dev"
2331
}
2432
},
2533
"autoload": {

lib/Money/Currency.php

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,51 @@ class Currency
1414
{
1515
/** @var string */
1616
private $name;
17-
17+
1818
/** @var array */
19-
private static $currencies;
19+
private $map;
20+
21+
/** @var string */
22+
private $symbol;
23+
24+
/** @var float */
25+
private $multiplier;
26+
27+
/** @var string */
28+
private $decimals;
29+
30+
/** @var string */
31+
private $thousands;
32+
33+
/** @var bool */
34+
private $symbolFirst;
2035

2136
/**
2237
* @param string $name
2338
* @throws UnknownCurrencyException
2439
*/
2540
public function __construct($name)
26-
{
27-
if(!isset(static::$currencies)) {
28-
static::$currencies = require __DIR__.'/currencies.php';
29-
}
41+
{
3042

31-
if (!array_key_exists($name, static::$currencies)) {
32-
throw new UnknownCurrencyException($name);
43+
$json_data = file_get_contents(__DIR__.'/currencymap.json');
44+
$this->map = json_decode($json_data, true);
45+
46+
47+
$key = strtolower($name);
48+
49+
if (array_key_exists($key, $this->map))
50+
{
51+
$this->name = $name;
52+
$this->symbol = $this->map[$key]['symbol'];
53+
$this->multiplier = $this->map[$key]['subunit_to_unit'];
54+
$this->decimals = $this->map[$key]['decimal_mark'];
55+
$this->thousands = $this->map[$key]['thousands_separator'];
56+
$this->symbolFirst = $this->map[$key]['symbol_first'];
3357
}
34-
$this->name = $name;
58+
else
59+
{
60+
throw new UnknownCurrencyException($name);
61+
}
3562
}
3663

3764

@@ -59,4 +86,45 @@ public function __toString()
5986
{
6087
return $this->getName();
6188
}
89+
90+
/**
91+
* @return string
92+
*/
93+
public function getSymbol()
94+
{
95+
return $this->symbol;
96+
}
97+
98+
/**
99+
* @return string
100+
*/
101+
public function getDecimals()
102+
{
103+
return $this->decimals;
104+
}
105+
106+
107+
/**
108+
* @return string
109+
*/
110+
public function getThousands()
111+
{
112+
return $this->thousands;
113+
}
114+
115+
/**
116+
* @return float
117+
*/
118+
public function getMultiplier()
119+
{
120+
return $this->multiplier;
121+
}
122+
123+
/**
124+
* @return bool
125+
*/
126+
public function hasSymbolFirst()
127+
{
128+
return $this->symbolFirst;
129+
}
62130
}

lib/Money/Money.php

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Money
1818
const ROUND_HALF_ODD = PHP_ROUND_HALF_ODD;
1919

2020
/**
21-
* @var int
21+
* @var float
2222
*/
2323
private $amount;
2424

@@ -27,22 +27,22 @@ class Money
2727

2828
/**
2929
* Create a Money instance
30-
* @param integer $amount Amount, expressed in the smallest units of $currency (eg cents)
30+
* @param float $amount Amount
3131
* @param \Money\Currency $currency
3232
* @throws \Money\InvalidArgumentException
3333
*/
3434
public function __construct($amount, Currency $currency)
3535
{
36-
if (!is_int($amount)) {
37-
throw new InvalidArgumentException("The first parameter of Money must be an integer. It's the amount, expressed in the smallest units of currency (eg cents)");
36+
if (!is_float($amount)) {
37+
throw new InvalidArgumentException("The first parameter of Money must be a float.");
3838
}
3939
$this->amount = $amount;
4040
$this->currency = $currency;
4141
}
4242

4343
/**
4444
* Convenience factory method for a Money object
45-
* @example $fiveDollar = Money::USD(500);
45+
* @example $fiveDollar = Money::USD(5);
4646
* @param string $method
4747
* @param array $arguments
4848
* @return \Money\Money
@@ -117,16 +117,18 @@ public function lessThan(Money $other)
117117
}
118118

119119
/**
120-
* @deprecated Use getAmount() instead
120+
* Returns the amount expressed in the smallest units of $currency (eg cents).
121+
* Units are treated as integers, which might cause issues when operating with
122+
* large numbers on 32 bit platforms
121123
* @return int
122124
*/
123125
public function getUnits()
124126
{
125-
return $this->amount;
127+
return (int)($this->amount * $this->currency->getMultiplier());
126128
}
127129

128130
/**
129-
* @return int
131+
* @return float
130132
*/
131133
public function getAmount()
132134
{
@@ -193,7 +195,7 @@ public function multiply($multiplier, $rounding_mode = self::ROUND_HALF_UP)
193195
$this->assertOperand($multiplier);
194196
$this->assertRoundingMode($rounding_mode);
195197

196-
$product = (int) round($this->amount * $multiplier, 0, $rounding_mode);
198+
$product = round($this->amount * $multiplier, 0, $rounding_mode);
197199

198200
return new Money($product, $this->currency);
199201
}
@@ -208,7 +210,7 @@ public function divide($divisor, $rounding_mode = self::ROUND_HALF_UP)
208210
$this->assertOperand($divisor);
209211
$this->assertRoundingMode($rounding_mode);
210212

211-
$quotient = (int) round($this->amount / $divisor, 0, $rounding_mode);
213+
$quotient = round($this->amount / $divisor, 0, $rounding_mode);
212214

213215
return new Money($quotient, $this->currency);
214216
}
@@ -225,7 +227,7 @@ public function allocate(array $ratios)
225227
$total = array_sum($ratios);
226228

227229
foreach ($ratios as $ratio) {
228-
$share = (int) floor($this->amount * $ratio / $total);
230+
$share = floor($this->amount * $ratio / $total);
229231
$results[] = new Money($share, $this->currency);
230232
$remainder -= $share;
231233
}
@@ -273,4 +275,42 @@ public static function stringToUnits( $string )
273275

274276
return (int) $units;
275277
}
278+
279+
/**
280+
* Extracts a formatted money string.
281+
* @example echo Money::USD(500)->formattedString();
282+
* @return string
283+
*/
284+
public function formattedString()
285+
{
286+
$decimal_separator = $this->currency->getDecimals();
287+
$thousand_separator = $this->currency->getThousands();
288+
$multiplier = $this->currency->getMultiplier();
289+
$decimals = (int) log10($multiplier);
290+
$number = $this->getAmount()/$multiplier;
291+
$value = '';
292+
$prefix = '';
293+
$suffix = '';
294+
295+
if($number < 0)
296+
{
297+
$prefix .= '-';
298+
$number = -$number;
299+
}
300+
301+
$value .= number_format($number, $decimals, $decimal_separator, $thousand_separator);
302+
303+
if($this->currency->hasSymbolFirst())
304+
{
305+
$prefix .= $this->currency->getSymbol();
306+
}
307+
else
308+
{
309+
$suffix .= $this->currency->getSymbol();
310+
}
311+
312+
return $prefix . $value . $suffix;
313+
}
314+
315+
276316
}

0 commit comments

Comments
 (0)