Passed
Push — master ( 94c8ca...b89ddc )
by Mr
02:16
created

Bitcoin::isNegative()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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