| 1 | <?php |
||
| 2 | |||
| 3 | namespace Loevgaard\DandomainFoundation; |
||
| 4 | |||
| 5 | use Brick\Math\BigDecimal; |
||
| 6 | use Money\Currency; |
||
| 7 | use Money\Money; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Creates a Money object. |
||
| 11 | * |
||
| 12 | * @param string $currency |
||
| 13 | * @param int $amount |
||
| 14 | 20 | * |
|
| 15 | * @return Money|null |
||
| 16 | */ |
||
| 17 | function createMoney(string $currency, int $amount = 0): ?Money |
||
| 18 | 20 | { |
|
| 19 | 10 | if (!$currency) { |
|
| 20 | return null; |
||
| 21 | } |
||
| 22 | |||
| 23 | 10 | return new Money($amount, new Currency($currency)); |
|
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Creates a Money object from a float/string. |
||
| 28 | 14 | * |
|
| 29 | * @param string $currency |
||
| 30 | 14 | * @param float|string $amount |
|
| 31 | * |
||
| 32 | * @return Money|null |
||
| 33 | */ |
||
| 34 | function createMoneyFromFloat(string $currency, $amount = 0.0): ?Money |
||
| 35 | { |
||
| 36 | 7 | $amount = BigDecimal::of((string) round($amount, 2))->multipliedBy(100)->toInt(); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 37 | |||
| 38 | 7 | return createMoney($currency, $amount); |
|
| 39 | } |
||
| 40 | |||
| 41 | function objectToArray($obj): array |
||
| 42 | { |
||
| 43 | if ($obj instanceof \stdClass) { |
||
| 44 | $obj = json_decode(json_encode($obj), true); |
||
| 45 | } |
||
| 46 | |||
| 47 | return (array) $obj; |
||
| 48 | } |
||
| 49 |