Passed
Push — master ( b8aa68...00068d )
by Hannes
02:17
created

SekNoSubunitMoneyParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 30
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B parse() 0 28 7
1
<?php
2
/**
3
 * This file is part of byrokrat\giroapp.
4
 *
5
 * byrokrat\giroapp is free software: you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * byrokrat\giroapp is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Copyright 2016-19 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\giroapp\Money;
24
25
use Money\Money;
26
use Money\MoneyParser;
27
28
final class SekNoSubunitMoneyParser implements MoneyParser
29
{
30
    public function parse($money, $forceCurrency = null)
31
    {
32
        if (!is_string($money)) {
33
            throw new \InvalidArgumentException('Money must be a string');
34
        }
35
36
        $negation = '';
37
38
        if (strlen($money) && $money[0] == '-') {
39
            $negation = '-';
40
            $money = substr($money, 1);
41
        }
42
43
        $money = ltrim($money, '0');
44
45
        if (empty($money)) {
46
            return Money::SEK('0');
47
        }
48
49
        if (preg_match('/^([0-9]+)\.00$/', $money, $matches)) {
50
            return Money::SEK($negation . $matches[1]);
51
        }
52
53
        if (!ctype_digit($money)) {
54
            throw new \InvalidArgumentException('Money must consist of only numbers');
55
        }
56
57
        return Money::SEK($negation . $money . '00');
58
    }
59
}
60