Completed
Push — master ( 842c53...e59924 )
by Joachim
14:33
created

createMoney()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Loevgaard\DandomainFoundation;
3
4
use Money\Currency;
5
use Money\Money;
6
7
/**
8
 * Creates a Money object
9
 *
10
 * @param string $currency
11
 * @param int $amount
12
 * @return Money|null
13
 */
14
function createMoney(string $currency, int $amount = 0) : ?Money
15
{
16
    if (!$currency) {
17
        return null;
18
    }
19
20
    return new Money($amount, new Currency($currency));
21
}
22
23
/**
24
 * Creates a Money object from a float/string
25
 *
26
 * @param string $currency
27
 * @param float|string $amount
28
 * @return Money|null
29
 */
30
function createMoneyFromFloat(string $currency, $amount = 0.0) : ?Money
31
{
32
    return createMoney($currency, intval(100 * $amount));
33
}
34
35
function objectToArray($obj) : array
36
{
37
    if ($obj instanceof \stdClass) {
38
        $obj = json_decode(json_encode($obj), true);
39
    }
40
41
    return (array)$obj;
42
}
43