-
Notifications
You must be signed in to change notification settings - Fork 0
/
CurrencyUtils.php
51 lines (44 loc) · 1.38 KB
/
CurrencyUtils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
class CurrencyUtils
{
/**
* @param mixed $amount
* @param string $currency
* @return integer
*/
public static function toMinorUnits($amount, $currency)
{
return self::roundToInt($amount * self::getMinorUnitsFactor($currency));
}
/**
* @param integer $amountInMinorUnits
* @param string $currency
* @return float
*/
public static function fromMinorUnits($amountInMinorUnits, $currency)
{
return (float) ($amountInMinorUnits / self::getMinorUnitsFactor($currency));
}
/**
* @param string $currency
* @return float
*/
protected static function getMinorUnitsFactor($currency)
{
$minorUnitsLookup = array(
'BHD' => 3, 'BYR' => 0, 'BIF' => 0, 'CLF' => 0, 'CLP' => 0, 'KMF' => 0, 'DJF' => 0, 'XAF' => 0, 'GNF' => 0,
'ISK' => 0, 'IQD' => 3, 'JPY' => 0, 'JOD' => 3, 'KRW' => 0, 'KWD' => 3, 'LYD' => 3, 'OMR' => 3, 'PYG' => 0,
'RWF' => 0, 'XOF' => 0, 'TND' => 3, 'UYI' => 0, 'VUV' => 0, 'VND' => 0, 'XPF' => 0
);
$minorUnits = isset($minorUnitsLookup[$currency]) ? $minorUnitsLookup[$currency] : 2;
return bcpow("10", "$minorUnits");
}
/**
* @param mixed $value
* @return integer
*/
protected static function roundToInt($value)
{
return (int) round($value, 0, PHP_ROUND_HALF_UP);
}
}