|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sfneal\Currency; |
|
4
|
|
|
|
|
5
|
|
|
class Currency |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Currency Symbols. |
|
9
|
|
|
*/ |
|
10
|
|
|
public const USD = '$'; |
|
11
|
|
|
public const EUR = '€'; |
|
12
|
|
|
public const GBP = '£'; |
|
13
|
|
|
public const INR = '₹'; |
|
14
|
|
|
public const JPY = '¥'; |
|
15
|
|
|
public const CNY = '¥'; |
|
16
|
|
|
public const THB = '฿'; |
|
17
|
|
|
public const AUD = self::USD; |
|
18
|
|
|
public const CAD = self::USD; |
|
19
|
|
|
public const NZD = self::USD; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Format a currency float value to a standard format. |
|
23
|
|
|
* |
|
24
|
|
|
* @param float $amount |
|
25
|
|
|
* @param string|null $currency |
|
26
|
|
|
* @param bool $commaSeparated |
|
27
|
|
|
* @param int $decimals |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function format(float $amount, |
|
31
|
|
|
string $currency = null, |
|
32
|
|
|
bool $commaSeparated = false, |
|
33
|
|
|
int $decimals = 2): string |
|
34
|
|
|
{ |
|
35
|
|
|
// Format the current amount |
|
36
|
|
|
$value = number_format( |
|
37
|
|
|
$amount, |
|
38
|
|
|
$decimals, |
|
39
|
|
|
'.', |
|
40
|
|
|
$commaSeparated ? ',' : '' |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
// Prepend currency symbol |
|
44
|
|
|
if ($currency) { |
|
45
|
|
|
return $currency.$value; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// Value without currency symbol |
|
49
|
|
|
return $value; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Format a USD float value to a standard format. |
|
54
|
|
|
* |
|
55
|
|
|
* @param float $amount |
|
56
|
|
|
* @param bool $commaSeparated |
|
57
|
|
|
* @param int $decimals |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function dollars(float $amount, bool $commaSeparated = false, int $decimals = 2): string |
|
61
|
|
|
{ |
|
62
|
|
|
return self::format($amount, self::USD, $commaSeparated, $decimals); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Format a EUR float value to a standard format. |
|
67
|
|
|
* |
|
68
|
|
|
* @param float $amount |
|
69
|
|
|
* @param bool $commaSeparated |
|
70
|
|
|
* @param int $decimals |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function euros(float $amount, bool $commaSeparated = false, int $decimals = 2): string |
|
74
|
|
|
{ |
|
75
|
|
|
return self::format($amount, self::EUR, $commaSeparated, $decimals); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Format a GBP float value to a standard format. |
|
80
|
|
|
* |
|
81
|
|
|
* @param float $amount |
|
82
|
|
|
* @param bool $commaSeparated |
|
83
|
|
|
* @param int $decimals |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function pounds(float $amount, bool $commaSeparated = false, int $decimals = 2): string |
|
87
|
|
|
{ |
|
88
|
|
|
return self::format($amount, self::GBP, $commaSeparated, $decimals); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|