Currency::getISOCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Adsmurai\Currency;
6
7
use Adsmurai\Currency\Contracts\Currency as CurrencyContract;
8
use Adsmurai\Currency\Errors\InconsistentCurrenciesError;
9
use InvalidArgumentException;
10
11
final class Currency implements CurrencyContract
12
{
13
    private $ISOCode;
14
15
    private $name;
16
17
    private $symbol;
18
19
    private $numFractionalDigits;
20
21
    private $symbolPlacement;
22
23 5
    public function __construct(
24
        string $ISOCode,
25
        string $symbol,
26
        int $numFractionalDigits,
27
        int $symbolPlacement = self::AFTER_PLACEMENT,
28
        string $name = ''
29
    ) {
30 5
        $symbol = \trim($symbol);
31 5
        $name = \trim($name);
32
33 5
        if (!in_array($symbolPlacement, [self::BEFORE_PLACEMENT, self::AFTER_PLACEMENT])) {
34 1
            throw new InvalidArgumentException('Invalid symbol placement');
35
        }
36
37 4
        if ($numFractionalDigits < 0 || $numFractionalDigits > 8) {
38 1
            throw new InvalidArgumentException('Invalid number of fractional digits');
39
        }
40
41 3
        if ('' === $symbol) {
42 2
            throw new InvalidArgumentException('Empty symbol');
43
        }
44
45 1
        $this->ISOCode = $ISOCode;
46 1
        $this->symbol = $symbol;
47 1
        $this->numFractionalDigits = $numFractionalDigits;
48 1
        $this->symbolPlacement = $symbolPlacement;
49 1
        $this->name = $name;
50 1
    }
51
52 1
    public function getISOCode(): string
53
    {
54 1
        return $this->ISOCode;
55
    }
56
57 1
    public function getName(): string
58
    {
59 1
        return $this->name;
60
    }
61
62 1
    public function getSymbol(): string
63
    {
64 1
        return $this->symbol;
65
    }
66
67 1
    public function getNumFractionalDigits(): int
68
    {
69 1
        return $this->numFractionalDigits;
70
    }
71
72 1
    public function getSymbolPlacement(): int
73
    {
74 1
        return $this->symbolPlacement;
75
    }
76
77 4
    public function equals(CurrencyContract $currency): bool
78
    {
79 4
        if ($this === $currency) {
80 1
            return true;
81
        }
82
83 3
        if ($currency->getISOCode() !== $this->ISOCode) {
84 1
            return false;
85
        }
86
87
        if (!(
88 2
            $currency->getName() === $this->name &&
89 2
            $currency->getSymbol() === $this->symbol &&
90 2
            $currency->getNumFractionalDigits() === $this->numFractionalDigits &&
91 2
            $currency->getSymbolPlacement() === $this->symbolPlacement
92
        )) {
93 1
            throw new InconsistentCurrenciesError($this, $currency);
94
        }
95
96 1
        return true;
97
    }
98
}
99