|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ghc\Rosetta\Transformers; |
|
4
|
|
|
|
|
5
|
|
|
use Money\Currencies\ISOCurrencies; |
|
6
|
|
|
use Money\Parser\AggregateMoneyParser; |
|
7
|
|
|
use Money\Parser\DecimalMoneyParser; |
|
8
|
|
|
use Money\Parser\IntlMoneyParser; |
|
9
|
|
|
|
|
10
|
|
|
class MoneyValues extends Transformer |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var AggregateMoneyParser |
|
14
|
|
|
*/ |
|
15
|
|
|
private $moneyParser; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Boot Transformer. |
|
19
|
|
|
*/ |
|
20
|
|
|
protected function boot() |
|
21
|
|
|
{ |
|
22
|
|
|
$defaultLocale = 'en_US'; |
|
23
|
|
|
$numberFormatter = new \NumberFormatter($defaultLocale, \NumberFormatter::CURRENCY); |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
$this->addDefaultConfig([ |
|
26
|
|
|
'locale' => $defaultLocale, |
|
27
|
|
|
'currency_code' => $numberFormatter->getTextAttribute(\NumberFormatter::CURRENCY_CODE), |
|
28
|
|
|
]); |
|
29
|
|
|
|
|
30
|
|
|
$currencies = new ISOCurrencies(); |
|
31
|
|
|
$intlParser = new IntlMoneyParser($numberFormatter, $currencies); |
|
32
|
|
|
$decimalParser = new DecimalMoneyParser($currencies); |
|
33
|
|
|
|
|
34
|
|
|
$this->moneyParser = new AggregateMoneyParser([ |
|
35
|
|
|
$intlParser, |
|
36
|
|
|
$decimalParser, |
|
37
|
|
|
]); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param array $data |
|
42
|
|
|
* |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
|
View Code Duplication |
public function transform($data) |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
foreach ($this->config['properties'] as $property) { |
|
48
|
|
|
if (isset($data[$property])) { |
|
49
|
|
|
$data[$property] = $this->parseMoney($data[$property]); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $data; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param $value |
|
58
|
|
|
* |
|
59
|
|
|
* @return \Money\Money |
|
60
|
|
|
*/ |
|
61
|
|
|
private function parseMoney($value) |
|
62
|
|
|
{ |
|
63
|
|
|
$isNegative = str_contains($value, '-'); |
|
64
|
|
|
$valueToParse = str_replace([' ', '+', '-'], '', $value); |
|
65
|
|
|
|
|
66
|
|
|
try { |
|
67
|
|
|
/** @var \Money\Money $money */ |
|
68
|
|
|
$money = $this->moneyParser->parse($valueToParse); |
|
69
|
|
|
$value = [ |
|
70
|
|
|
'amount' => (int) (!$isNegative ? $money->getAmount() : -$money->getAmount()), |
|
71
|
|
|
'currency' => $money->getCurrency()->getCode(), |
|
72
|
|
|
]; |
|
73
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $value; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths