|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace XoopsModules\Adslight; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
You may not change or alter any portion of this comment or credits |
|
7
|
|
|
of supporting developers from this source code or any supporting source code |
|
8
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
|
9
|
|
|
|
|
10
|
|
|
This program is distributed in the hope that it will be useful, |
|
11
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @copyright {@link https://xoops.org/ XOOPS Project} |
|
17
|
|
|
* @license {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2.0 or later} |
|
18
|
|
|
* @author Hervé Thouzard (https://www.herve-thouzard.com/) |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Gestion de la Currency |
|
23
|
|
|
*/ |
|
24
|
|
|
class Currency |
|
25
|
|
|
{ |
|
26
|
|
|
protected $decimalsCount = 0; |
|
27
|
|
|
protected $thousandsSep; |
|
28
|
|
|
protected $decimalSep; |
|
29
|
|
|
protected $moneyFull; |
|
30
|
|
|
protected $moneyShort; |
|
31
|
|
|
protected $currencyPosition; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Currency constructor. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct() |
|
37
|
|
|
{ |
|
38
|
|
|
$moduleDirName = \basename(__DIR__); |
|
39
|
|
|
$helper = Helper::getHelper($moduleDirName); // Get the module's preferences |
|
40
|
|
|
$this->decimalsCount = $helper->getConfig('decimals_count'); |
|
41
|
|
|
$this->thousandsSep = $helper->getConfig('thousands_sep'); |
|
42
|
|
|
$this->decimalSep = $helper->getConfig('decimal_sep'); |
|
43
|
|
|
$this->moneyFull = $helper->getConfig('money_full'); |
|
44
|
|
|
$this->moneyShort = $helper->getConfig('money_short'); |
|
45
|
|
|
$this->currencyPosition = $helper->getConfig('currency_position'); |
|
46
|
|
|
$this->thousandsSep = \str_replace('[space]', ' ', $this->thousandsSep); |
|
47
|
|
|
$this->decimalSep = \str_replace('[space]', ' ', $this->decimalSep); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Access the only instance of this class |
|
52
|
|
|
* |
|
53
|
|
|
* @static |
|
54
|
|
|
* @staticvar object |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function getInstance(): object |
|
57
|
|
|
{ |
|
58
|
|
|
static $instance; |
|
59
|
|
|
if (null === $instance) { |
|
60
|
|
|
$instance = new static(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $instance; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns an amount according to the currency's preferences (defined in the module's options) |
|
68
|
|
|
* |
|
69
|
|
|
* @param float|int $amount The amount to work on |
|
70
|
|
|
* @return string The amount formated according to the currency |
|
71
|
|
|
*/ |
|
72
|
|
|
public function amountInCurrency($amount = 0): string |
|
73
|
|
|
{ |
|
74
|
|
|
return \number_format($amount, $this->decimalsCount, $this->decimalSep, $this->thousandsSep); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Format an amount for display according to module's preferences |
|
79
|
|
|
* |
|
80
|
|
|
* @param float $originalAmount The amount to format |
|
81
|
|
|
* @param string $format Format to use, 's' for Short and 'l' for Long |
|
82
|
|
|
* @return string The amount formated |
|
83
|
|
|
*/ |
|
84
|
|
|
public function amountForDisplay($originalAmount, $format = 's'): string |
|
85
|
|
|
{ |
|
86
|
|
|
$amount = $this->amountInCurrency($originalAmount); |
|
87
|
|
|
|
|
88
|
|
|
$currencyLeft = $currencyRight = $currencyLeftShort = $currencyRightShort = ''; |
|
89
|
|
|
if (1 === $this->currencyPosition) { // To the right |
|
90
|
|
|
$currencyRight = '' . $this->moneyFull; // Long version |
|
91
|
|
|
$currencyRightShort = '' . $this->moneyShort; // Short version |
|
92
|
|
|
} else { // To the left |
|
93
|
|
|
$currencyLeft = $this->moneyFull . ''; // Long version |
|
94
|
|
|
$currencyLeftShort = $this->moneyShort . ''; // Short version |
|
95
|
|
|
} |
|
96
|
|
|
if ('s' !== $format) { |
|
97
|
|
|
return $currencyLeft . $amount . $currencyRight; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $currencyLeftShort . $amount . $currencyRightShort; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|