Completed
Push — master ( 00e474...9d3fbd )
by Michael
04:26
created

class/oledrion_currency.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * oledrion
14
 *
15
 * @copyright   {@link http://xoops.org/ XOOPS Project}
16
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
17
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
18
 */
19
20
/**
21
 * Gestion de la monnaie
22
 */
23
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
24
25
class Oledrion_Currency
26
{
27
    protected $_decimalsCount;
28
    protected $_thousandsSep;
29
    protected $_decimalSep;
30
    protected $_moneyFull;
31
    protected $_moneyShort;
32
    protected $_monnaiePlace;
33
34
    /**
35
     * Oledrion_Currency constructor.
36
     */
37
    public function __construct()
38
    {
39
        // Get the module's preferences
40
        $this->_decimalsCount = Oledrion_utils::getModuleOption('decimals_count');
41
        $this->_thousandsSep  = Oledrion_utils::getModuleOption('thousands_sep');
42
        $this->_decimalSep    = Oledrion_utils::getModuleOption('decimal_sep');
43
        $this->_moneyFull     = Oledrion_utils::getModuleOption('money_full');
44
        $this->_moneyShort    = Oledrion_utils::getModuleOption('money_short');
45
        $this->_monnaiePlace  = Oledrion_utils::getModuleOption('monnaie_place');
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
     * @return object
54
     *
55
     * @static
56
     * @staticvar   object
57
     */
58
59
    public static function getInstance()
60
    {
61
        static $instance;
62
        if (null === $instance) {
63
            $instance = new static();
64
        }
65
66
        return $instance;
67
    }
68
69
    /**
70
     * Returns an amount according to the currency's preferences (defined in the module's options)
71
     *
72
     * @param  float|int $amount The amount to work on
73
     * @return string    The amount formated according to the currency
74
     */
75
    public function amountInCurrency($amount = 0)
76
    {
77
        return number_format($amount, $this->_decimalsCount, $this->_decimalSep, $this->_thousandsSep);
78
    }
79
80
    /**
81
     * Format an amount for display according to module's preferences
82
     *
83
     * @param  float  $originalAmount The amount to format
84
     * @param  string $format         Format to use, 's' for Short and 'l' for Long
85
     * @return string The amount formated
86
     */
87
    public function amountForDisplay($originalAmount, $format = 's')
88
    {
89
        $amount = $this->amountInCurrency($originalAmount);
90
91
        $monnaieLeft = $monnaieRight = $monnaieSleft = $monnaieSright = '';
92
        if ($this->_monnaiePlace == 1) { // To the right
93
            $monnaieRight  = '' . $this->_moneyFull; // Long version
94
            $monnaieSright = '' . $this->_moneyShort; // Short version
95
        } else { // To the left
96
            $monnaieLeft  = $this->_moneyFull . ''; // Long version
97
            $monnaieSleft = $this->_moneyShort . ''; // Short version
98
        }
99
        if ($format !== 's') {
100
            return $monnaieLeft . $amount . $monnaieRight;
101
        } else {
102
            return $monnaieSleft . $amount . $monnaieSright;
103
        }
104
    }
105
}
106