Bitcoin::fromNative()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
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\ValueObject;
10
11
use Daikon\Interop\Assertion;
12
use Daikon\Interop\InvalidArgumentException;
13
use Daikon\Money\ValueObject\Money;
14
use NGUtech\Bitcoin\Service\SatoshiCurrencies;
15
16
final class Bitcoin extends Money
17
{
18
    /** @param null|string $value */
19 7
    public static function fromNative($value): self
20
    {
21 7
        Assertion::nullOrString($value, 'Must be a string.');
22 7
        if ($value === null) {
23
            return new self;
24
        }
25
26 7
        if (!preg_match('/^(?<amount>-?\d+)\s?(?<currency>M?SAT|BTC|XBT)$/i', $value, $matches)) {
27 2
            throw new InvalidArgumentException('Invalid amount.');
28
        }
29
30 7
        return new self(self::asBaseMoney($matches['amount'], strtoupper($matches['currency'])));
31
    }
32
33
    /** @param null|string $currency */
34 1
    public static function zero($currency = null): self
35
    {
36 1
        return self::fromNative('0'.($currency ?? SatoshiCurrencies::MSAT));
37
    }
38
}
39