Completed
Push — master ( 5c3ab9...4f1dd0 )
by Joachim
04:04
created

functions.php ➔ floatFromMoney()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Loevgaard\AltaPay;
3
4
use Alcohol\ISO4217;
5
use Money\Currencies\ISOCurrencies;
6
use Money\Currency;
7
use Money\Formatter\DecimalMoneyFormatter;
8
use Money\Money;
9
10
/**
11
 * Creates a Money object
12
 *
13
 * @param string $currency
14
 * @param int $amount
15
 * @return Money|null
16
 */
17
function createMoney(string $currency, int $amount = 0) : ?Money
18
{
19 69
    if (!$currency) {
20 15
        return null;
21
    }
22
23 60
    return new Money($amount, new Currency($currency));
24
}
25
26
/**
27
 * Takes a Money object and returns a float
28
 *
29
 * @param Money $money
30
 * @return float
31
 */
32
function floatFromMoney(Money $money = null) : ?float
33
{
34 18
    if (is_null($money)) {
35 15
        return null;
36
    }
37
38 12
    $currencies = new ISOCurrencies();
39 12
    $moneyFormatter = new DecimalMoneyFormatter($currencies);
40
41 12
    return $moneyFormatter->format($money);
42
}
43
44
/**
45
 * Creates a Money object from a float/string
46
 *
47
 * @param string $currency
48
 * @param float|string $amount
49
 * @return Money|null
50
 */
51
function createMoneyFromFloat(string $currency, $amount = 0.0) : ?Money
52
{
53 45
    return createMoney($currency, intval(100 * $amount));
54
}
55
56
function alphaCurrencyFromNumeric(int $numericCurrency) : string
57
{
58
    try {
59 33
        $iso4217 = new ISO4217();
60 33
        return $iso4217->getByNumeric($numericCurrency)['alpha3'];
61
    } catch (\OutOfBoundsException $e) {
62
        return '';
63
    }
64
}
65