Bitcoin::zero()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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