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

Bitcoin   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 63.64%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 27
ccs 7
cts 11
cp 0.6364
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A makeEmpty() 0 3 1
A isEmpty() 0 3 1
A zero() 0 3 1
A fromNative() 0 9 2
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