|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Phalcon\Cashier; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
class Cashier |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The current currency. |
|
11
|
|
|
* |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
protected static $currency = 'usd'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The current currency symbol. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected static $currencySymbol = '$'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The custom currency formatter. |
|
25
|
|
|
* |
|
26
|
|
|
* @var callable |
|
27
|
|
|
*/ |
|
28
|
|
|
protected static $formatCurrencyUsing; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Set the currency to be used when billing users. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $currency |
|
34
|
|
|
* @param string|null $symbol |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function useCurrency($currency, $symbol = null) |
|
38
|
|
|
{ |
|
39
|
|
|
static::$currency = $currency; |
|
40
|
|
|
|
|
41
|
|
|
static::useCurrencySymbol($symbol ?: static::guessCurrencySymbol($currency)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Guess the currency symbol for the given currency. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $currency |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected static function guessCurrencySymbol($currency) |
|
51
|
|
|
{ |
|
52
|
|
|
switch (strtolower($currency)) { |
|
53
|
|
|
case 'usd': |
|
54
|
|
|
case 'aud': |
|
55
|
|
|
case 'cad': |
|
56
|
|
|
return '$'; |
|
57
|
|
|
case 'eur': |
|
58
|
|
|
return '€'; |
|
59
|
|
|
case 'gbp': |
|
60
|
|
|
return '£'; |
|
61
|
|
|
default: |
|
62
|
|
|
throw new Exception('Unable to guess symbol for currency. Please explicitly specify it.'); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get the currency currently in use. |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function usesCurrency() |
|
72
|
|
|
{ |
|
73
|
|
|
return static::$currency; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Set the currency symbol to be used when formatting currency. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $symbol |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function useCurrencySymbol($symbol) |
|
83
|
|
|
{ |
|
84
|
|
|
static::$currencySymbol = $symbol; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get the currency symbol currently in use. |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public static function usesCurrencySymbol() |
|
93
|
|
|
{ |
|
94
|
|
|
return static::$currencySymbol; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set the custom currency formatter. |
|
99
|
|
|
* |
|
100
|
|
|
* @param callable $callback |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function formatCurrencyUsing(callable $callback) |
|
104
|
|
|
{ |
|
105
|
|
|
static::$formatCurrencyUsing = $callback; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Format the given amount into a displayable currency. |
|
110
|
|
|
* |
|
111
|
|
|
* @param int $amount |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
public static function formatAmount($amount) |
|
115
|
|
|
{ |
|
116
|
|
|
if (static::$formatCurrencyUsing) { |
|
117
|
|
|
return call_user_func(static::$formatCurrencyUsing, $amount); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$amount = number_format($amount / 100, 2); |
|
121
|
|
|
|
|
122
|
|
|
if (static::startsWith($amount, '-')) { |
|
123
|
|
|
return '-' . static::usesCurrencySymbol() . ltrim($amount, '-'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return static::usesCurrencySymbol() . $amount; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param $haystack |
|
131
|
|
|
* @param $needle |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
|
|
public static function startsWith($haystack, $needle) |
|
135
|
|
|
{ |
|
136
|
|
|
// search backwards starting from haystack length characters from the end |
|
137
|
|
|
return $needle === '' || strrpos($haystack, $needle, -strlen($haystack)) !== false; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param $haystack |
|
142
|
|
|
* @param $needle |
|
143
|
|
|
* @return bool |
|
144
|
|
|
*/ |
|
145
|
|
|
public static function endsWith($haystack, $needle) |
|
146
|
|
|
{ |
|
147
|
|
|
// search forward starting from end minus needle length characters |
|
148
|
|
|
return $needle === '' || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|