NGUtech /
bitcoin-interop
| 1 | <?php declare(strict_types=1); |
||
| 2 | /** |
||
| 3 | * This file is part of the ngutech/bitcoin-interop project. |
||
| 4 | * |
||
| 5 | * For the full copyright and license information, please view the LICENSE |
||
| 6 | * file that was distributed with this source code. |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace NGUtech\Bitcoin\Service; |
||
| 10 | |||
| 11 | use Money\Currency; |
||
| 12 | use Money\Exception\ParserException; |
||
| 13 | use Money\Money; |
||
| 14 | use Money\MoneyParser; |
||
| 15 | |||
| 16 | final class SatoshiMoneyParser implements MoneyParser |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @param string $money |
||
| 20 | * @param null|Currency $forceCurrency |
||
| 21 | */ |
||
| 22 | public function parse($money, $forceCurrency = null) |
||
| 23 | { |
||
| 24 | if (is_string($money) === false) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 25 | throw new ParserException('Formatted raw money should be string, e.g. 100SAT'); |
||
| 26 | } |
||
| 27 | |||
| 28 | if (!preg_match('/^(?<amount>-?\d+)\s?(?<currency>M?SAT)$/i', $money, $matches)) { |
||
| 29 | throw new ParserException('Value cannot be parsed to Satoshi.'); |
||
| 30 | } |
||
| 31 | |||
| 32 | $currency = $forceCurrency ?: new Currency(strtoupper($matches['currency'])); |
||
| 33 | |||
| 34 | return new Money($matches['amount'], $currency); |
||
| 35 | } |
||
| 36 | } |
||
| 37 |