Completed
Branch FET-10619-money-entity (f4b3f8)
by
unknown
139:32 queued 127:27
created

CreateMoney::getSiteCurrency()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 5
nop 0
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\currency;
4
5
use EE_Error;
6
use EventEspresso\core\domain\values\currency\Money;
7
use EventEspresso\core\exceptions\InvalidDataTypeException;
8
use EventEspresso\core\exceptions\InvalidInterfaceException;
9
use InvalidArgumentException;
10
11
defined('EVENT_ESPRESSO_VERSION') || exit;
12
13
14
15
/**
16
 * Class CreateMoney
17
 * Factory class for creating Money objects
18
 *
19
 * @package       Event Espresso
20
 * @author        Brent Christensen
21
 * @since         $VID:$
22
 */
23
class CreateMoney
24
{
25
26
    /**
27
     * @var Calculator $calculator
28
     */
29
    protected static $calculator;
30
31
    /**
32
     * @var MoneyFormatter[] $formatters
33
     */
34
    protected static $formatters;
35
36
37
38
    /**
39
     * factory method that returns a Money object using amount specified in the currency's subunits
40
     * example: for $12.50 USD use CreateMoney::fromSubUnits(1250, 'USD')
41
     *
42
     * @param int    $subunits_amount money amount IN THE SUBUNITS FOR THE CURRENCY ie: cents
43
     *                                example: $12.50 USD would equate to a subunits amount of 1250
44
     * @param string $currency_code
45
     * @return Money
46
     * @throws EE_Error
47
     * @throws InvalidArgumentException
48
     * @throws InvalidDataTypeException
49
     * @throws InvalidInterfaceException
50
     */
51
    public static function fromSubUnits($subunits_amount, $currency_code = '')
52
    {
53
        $currency = CreateCurrency::fromCode($currency_code);
54
        return new Money(
55
            // shift decimal BACK by number of places for currency
56
            $subunits_amount * pow(10, $currency->decimalPlaces() * -1),
57
            $currency,
58
            CreateMoney::calculator(),
59
            CreateMoney::formatters()
60
        );
61
    }
62
63
64
65
    /**
66
     * factory method that returns a Money object using the currency corresponding to the site's country
67
     * example: CreateMoney::forSite(12.5)
68
     *
69
     * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
70
     *                                 example: $12.5 USD would equate to a standard amount of 12.50
71
     * @return Money
72
     * @throws EE_Error
73
     * @throws InvalidArgumentException
74
     * @throws InvalidDataTypeException
75
     * @throws InvalidInterfaceException
76
     */
77
    public static function forSite($amount)
78
    {
79
        return new Money(
80
            $amount,
81
            CreateCurrency::fromCountryCode(),
82
            CreateMoney::calculator(),
83
            CreateMoney::formatters()
84
        );
85
    }
86
87
88
89
    /**
90
     * factory method that returns a Money object using the currency as specified by the supplied ISO country code
91
     * example: CreateMoney::forCountry(12.5,'US')
92
     *
93
     * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
94
     *                                 example: $12.5 USD would equate to a value amount of 12.50
95
     * @param string           $CNT_ISO
96
     * @return Money
97
     * @throws EE_Error
98
     * @throws InvalidArgumentException
99
     * @throws InvalidDataTypeException
100
     * @throws InvalidInterfaceException
101
     */
102
    public static function forCountry($amount, $CNT_ISO)
103
    {
104
        return new Money(
105
            $amount,
106
            CreateCurrency::fromCountryCode($CNT_ISO),
107
            CreateMoney::calculator(),
108
            CreateMoney::formatters()
109
        );
110
    }
111
112
113
114
    /**
115
     * factory method that returns a Money object using the currency as specified by the supplied currency code
116
     * example: CreateMoney::forCurrency(12.5, 'USD')
117
     *
118
     * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
119
     *                                 example: $12.5 USD would equate to a value amount of 12.50
120
     * @param string           $currency_code
121
     * @return Money
122
     * @throws EE_Error
123
     * @throws InvalidArgumentException
124
     * @throws InvalidDataTypeException
125
     * @throws InvalidInterfaceException
126
     */
127
    public static function forCurrency($amount, $currency_code)
128
    {
129
        return new Money(
130
            $amount,
131
            CreateCurrency::fromCode($currency_code),
132
            CreateMoney::calculator(),
133
            CreateMoney::formatters()
134
        );
135
    }
136
137
138
139
140
    /**
141
     * @return Calculator
142
     */
143
    public static function calculator()
144
    {
145
        CreateMoney::initializeCalculators();
146
        return CreateMoney::$calculator;
147
    }
148
149
150
151
    /**
152
     * loops through a filterable array of Calculator services
153
     * and selects the first one that is supported by the current server
154
     */
155
    protected static function initializeCalculators()
156
    {
157
        if (CreateMoney::$calculator instanceof Calculator) {
158
            return;
159
        }
160
        $calculators = apply_filters(
161
            'FHEE__EventEspresso\core\services\currency\MoneyFactory__initializeCalculators__Calculator_array',
162
            array(
163
                '\EventEspresso\core\services\currency\DefaultCalculator',
164
            )
165
        );
166
        foreach ($calculators as $calculator) {
167
            if (! class_exists($calculator)) {
168
                continue;
169
            }
170
            $calculator = new $calculator();
171
            if ($calculator instanceof Calculator && $calculator->isSupported()) {
172
                CreateMoney::$calculator = $calculator;
173
                break;
174
            }
175
        }
176
    }
177
178
179
180
    /**
181
     * @return MoneyFormatter[]
182
     */
183
    public static function formatters()
184
    {
185
        CreateMoney::initializeFormatters();
186
        return CreateMoney::$formatters;
187
    }
188
189
190
191
    /**
192
     * initializes a filterable array of MoneyFormatter services
193
     */
194
    protected static function initializeFormatters()
195
    {
196
        if (! empty(CreateMoney::$formatters)) {
197
            return;
198
        }
199
        CreateMoney::$formatters = apply_filters(
200
            'FHEE__EventEspresso\core\services\currency\MoneyFactory__initializeFormatters__MoneyFormatters_array',
201
            array(
202
                1 => new DecimalMoneyFormatter(),
203
                2 => new ThousandsMoneyFormatter(),
204
                3 => new CurrencySignMoneyFormatter(),
205
                4 => new CurrencyCodeMoneyFormatter(),
206
                5 => new InternationalMoneyFormatter(),
207
            )
208
        );
209
    }
210
211
212
}
213
// End of file CreateMoney.php
214
// Location: core/services/currency/CreateMoney.php
215