Test Failed
Push — master ( 7fb687...840826 )
by Gabriel
12:03
created

MoneyParserTrait::parse()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 16
c 1
b 0
f 1
nc 14
nop 3
dl 0
loc 33
rs 8.4444
1
<?php
2
3
namespace ByTIC\Money\Traits;
4
5
use ByTIC\Money\Money;
6
use Money\Currency;
7
use Money\Currencies;
8
use Money\MoneyParser;
9
use Money\Exception\ParserException;
10
use Money\Parser\AggregateMoneyParser;
11
use Money\Parser\BitcoinMoneyParser;
12
use Money\Parser\DecimalMoneyParser;
13
use InvalidArgumentException;
14
use Money\Parser\IntlLocalizedDecimalParser;
15
use Money\Parser\IntlMoneyParser;
16
use NumberFormatter;
17
18
/**
19
 * Trait MoneyParserTrait
20
 * @package ByTIC\Money\Traits
21
 */
22
trait MoneyParserTrait
23
{
24
25
    /**
26
     * Convert the given value into an instance of Money.
27
     *
28
     * @param mixed                       $value
29
     * @param \Money\Currency|string|null $currency
30
     * @param iny                         $bitCointDigits
0 ignored issues
show
Bug introduced by
The type ByTIC\Money\Traits\iny was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
     *
32
     * @return \ByTIC\Money\Money|null
33
     */
34
    public static function parse($value, $currency = null, $bitCointDigits = 2)
0 ignored issues
show
Unused Code introduced by
The parameter $bitCointDigits is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

34
    public static function parse($value, $currency = null, /** @scrutinizer ignore-unused */ $bitCointDigits = 2)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
    {
36
        if ($value instanceof Money) {
37
            return $value;
38
        }
39
40
        if ($value instanceof \Money\Money) {
41
            return static::fromMoney($value);
42
        }
43
44
        $currency = static::currency($currency);
45
46
        if (is_string($value)) {
47
            if (filter_var($value, FILTER_VALIDATE_FLOAT) === true) {
48
                $value = floatval($value);
49
            } else {
50
//                return static::parseNonFloatStrings($value, $currency, $bitCointDigits);
51
            }
52
        }
53
54
        if (is_string($value)) {
55
            return new Money($value, $currency);
56
        }
57
58
        if (is_int($value)) {
59
            return new Money($value, $currency);
60
        }
61
62
        if (is_float($value)) {
63
            return static::parseByDecimal((string) $value, $currency);
64
        }
65
66
        throw new InvalidArgumentException(sprintf('Invalid value: %s', json_encode($value)));
67
    }
68
69
    protected static function parseNonFloatStrings($value,$currency = null, $bitCointDigits = 2)
70
    {
71
        $locale = static::getLocale();
72
        $currencies = static::getCurrencies();
73
74
        try {
75
            return static::parseByAggregate($value, null, [
76
                new IntlMoneyParser(new NumberFormatter($locale, NumberFormatter::CURRENCY), $currencies),
77
                new BitcoinMoneyParser($bitCointDigits),
78
            ]);
79
        } catch (ParserException $e) {
80
            try {
81
                return static::parseByIntlLocalizedDecimal($value, $currency, $locale, $currencies);
82
            } catch (ParserException $e) {
83
                throw new ParserException(sprintf('Unable to parse: %s', $value));
84
            }
85
        }
86
    }
87
88
    /**
89
     * Parse by aggregate.
90
     *
91
     * @param string        $money
92
     * @param string|null   $forceCurrency
93
     * @param MoneyParser[] $parsers
94
     *
95
     * @return \ByTIC\Money\Money
96
     */
97
    public static function parseByAggregate($money, $forceCurrency = null, array $parsers = [])
98
    {
99
        $parser = new AggregateMoneyParser($parsers);
100
101
        return static::parseByParser($parser, $money, $forceCurrency);
102
    }
103
104
    /**
105
     * Parse by bitcoin.
106
     *
107
     * @param string      $money
108
     * @param string|null $forceCurrency
109
     * @param int         $fractionDigits
110
     *
111
     * @return \ByTIC\Money\Money
112
     */
113
    public static function parseByBitcoin($money, $forceCurrency = null, $fractionDigits = 2)
114
    {
115
        $parser = new BitcoinMoneyParser($fractionDigits);
116
117
        return static::parseByParser($parser, $money, $forceCurrency);
118
    }
119
120
    /**
121
     * Parse by decimal.
122
     *
123
     * @param string            $money
124
     * @param string|null       $forceCurrency
125
     * @param \Money\Currencies $currencies
126
     *
127
     * @return \ByTIC\Money\Money
128
     */
129
    public static function parseByDecimal($money, $forceCurrency = null, Currencies $currencies = null)
130
    {
131
        $parser = new DecimalMoneyParser($currencies ?: static::getCurrencies());
132
133
        return static::parseByParser($parser,(string) $money, $forceCurrency);
134
    }
135
136
    /**
137
     * Parse by intl.
138
     *
139
     * @param string            $money
140
     * @param string|null       $forceCurrency
141
     * @param string|null       $locale
142
     * @param \Money\Currencies $currencies
143
     * @param int               $style
144
     *
145
     * @return \ByTIC\Money\Money
146
     */
147
    public static function parseByIntl(
148
        $money,
149
        $forceCurrency = null,
150
        $locale = null,
151
        Currencies $currencies = null,
152
        $style = NumberFormatter::CURRENCY
153
    ) {
154
        $numberFormatter = new NumberFormatter($locale ?: static::getLocale(), $style);
155
        $parser = new IntlMoneyParser($numberFormatter, $currencies ?: static::getCurrencies());
156
157
        return static::parseByParser($parser, $money, $forceCurrency);
158
    }
159
160
    /**
161
     * Parse by intl localized decimal.
162
     *
163
     * @param string            $money
164
     * @param string            $forceCurrency
165
     * @param string|null       $locale
166
     * @param \Money\Currencies $currencies
167
     * @param int               $style
168
     *
169
     * @return \ByTIC\Money\Money
170
     */
171
    public static function parseByIntlLocalizedDecimal(
172
        $money,
173
        $forceCurrency,
174
        $locale = null,
175
        Currencies $currencies = null,
176
        $style = NumberFormatter::DECIMAL
177
    ) {
178
        $numberFormatter = new NumberFormatter($locale ?: static::getLocale(), $style);
179
        $parser = new IntlLocalizedDecimalParser($numberFormatter, $currencies ?: static::getCurrencies());
180
181
        return static::parseByParser($parser, $money, $forceCurrency);
182
    }
183
184
    /**
185
     * Parse by parser.
186
     *
187
     * @param \Money\MoneyParser $parser
188
     * @param string             $money
189
     * @param string|null        $forceCurrency
190
     *
191
     * @return \ByTIC\Money\Money
192
     */
193
    public static function parseByParser(MoneyParser $parser, $money, $forceCurrency = null)
194
    {
195
        return static::convert($parser->parse($money, $forceCurrency));
196
    }
197
}