1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class EcommerceMoney extends Extension |
4
|
|
|
{ |
5
|
|
|
/** |
6
|
|
|
* returns the symbol for a currency, e.g. $. |
7
|
|
|
* |
8
|
|
|
* @param string $currency |
9
|
|
|
* |
10
|
|
|
* @return string |
11
|
|
|
*/ |
12
|
|
|
public static function get_default_symbol($currency) |
13
|
|
|
{ |
14
|
|
|
$money = Money::create(); |
15
|
|
|
|
16
|
|
|
return $money->getSymbol($currency); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* returns the short symbol for a currency |
21
|
|
|
* This is shorter than the default one. |
22
|
|
|
* |
23
|
|
|
* @param string $currency |
24
|
|
|
* |
25
|
|
|
* @return string |
|
|
|
|
26
|
|
|
*/ |
27
|
|
|
public static function get_short_symbol($currency) |
28
|
|
|
{ |
29
|
|
|
$symbol = self::get_default_symbol($currency); |
30
|
|
|
if ($symbol) { |
31
|
|
|
$i = 0; |
32
|
|
|
while ($i < mb_strlen($symbol) && $symbol[$i] === $currency[$i]) { |
33
|
|
|
++$i; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return substr($symbol, $i); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* returns the long symbol for a currency. |
42
|
|
|
* |
43
|
|
|
* @param string $currency |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public static function get_long_symbol($currency) |
48
|
|
|
{ |
49
|
|
|
$symbol = self::get_default_symbol($currency); |
50
|
|
|
if ($symbol && mb_strlen($symbol) < 3) { |
51
|
|
|
$symbol = substr($currency, 0, 3 - mb_strlen($symbol)).$symbol; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $symbol; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* returns the default symbol for a site. |
59
|
|
|
* with or without html. |
60
|
|
|
* |
61
|
|
|
* @param bool $html |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function NiceDefaultSymbol($html = true) |
66
|
|
|
{ |
67
|
|
|
return self::get_default_symbol($this->owner->currency) == self::get_short_symbol($this->owner->currency) ? $this->NiceShortSymbol($html) : $this->NiceLongSymbol($html); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* returns the short symbol for a site. |
72
|
|
|
* with or without html. |
73
|
|
|
* |
74
|
|
|
* @param bool $html |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function NiceShortSymbol($html = true) |
79
|
|
|
{ |
80
|
|
|
$symbol = self::get_short_symbol($this->owner->currency); |
|
|
|
|
81
|
|
|
if ($html) { |
82
|
|
|
$symbol = "<span class=\"currencyHolder currencyHolderShort currency{$this->owner->currency}\"><span class=\"currencySymbol\">$symbol</span></span>"; |
83
|
|
|
} |
84
|
|
|
$amount = $this->owner->getAmount(); |
85
|
|
|
|
86
|
|
|
return (is_numeric($amount)) ? $this->owner->currencyLib->toCurrency($amount, array('symbol' => $symbol, 'display' => Zend_Currency::USE_SYMBOL)) : ''; |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* returns the long symbol for a site. |
91
|
|
|
* with or without html. |
92
|
|
|
* |
93
|
|
|
* @param bool $html |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function NiceLongSymbol($html = true) |
98
|
|
|
{ |
99
|
|
|
$symbol = self::get_long_symbol($this->owner->currency); |
|
|
|
|
100
|
|
|
$short = self::get_short_symbol($this->owner->currency); |
101
|
|
|
$pre = substr($symbol, 0, mb_strlen($symbol) - mb_strlen($short)); |
102
|
|
|
if ($html) { |
103
|
|
|
$symbol = "<span class=\"currencyHolder currencyHolderLong currency{$this->owner->currency}\"><span class=\"currencyPreSymbol\">$pre</span><span class=\"currencySymbol\">$short</span></span>"; |
104
|
|
|
} else { |
105
|
|
|
$symbol = $pre.$short; |
106
|
|
|
} |
107
|
|
|
$amount = $this->owner->getAmount(); |
108
|
|
|
|
109
|
|
|
return (is_numeric($amount)) ? $this->owner->currencyLib->toCurrency($amount, array('symbol' => $symbol, 'display' => Zend_Currency::USE_SYMBOL)) : ''; |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* returns a currency like this: 8,001 usd / 12.12 nzd. |
114
|
|
|
* |
115
|
|
|
* @param bool $html |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function SymbolNumberAndCode($html = true) |
120
|
|
|
{ |
121
|
|
|
$symbol = self::get_short_symbol($this->owner->currency); |
|
|
|
|
122
|
|
|
if ($html) { |
123
|
|
|
$symbol = "<span class=\"currencySymbol\">$symbol</span>"; |
124
|
|
|
} |
125
|
|
|
$code = strtolower($this->owner->currency); |
126
|
|
|
if ($html) { |
127
|
|
|
$code = "<span class=\"currencyHolder\">$code</span>"; |
128
|
|
|
} |
129
|
|
|
$amount = $this->owner->getAmount(); |
130
|
|
|
|
131
|
|
|
return (is_numeric($amount)) ? $symbol.$this->owner->currencyLib->toCurrency($amount, array('symbol' => '', 'precision' => 0)).' '.$code : ''; |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* returns the default format for a site for currency. |
136
|
|
|
* |
137
|
|
|
* @param bool $html |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function NiceDefaultFormat($html = true) |
142
|
|
|
{ |
143
|
|
|
$function = EcommerceConfig::get('EcommerceMoney', 'default_format'); |
144
|
|
|
|
145
|
|
|
return $this->owner->$function($html); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.