Currency::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Date: 05.11.18
4
 * Time: 20:27
5
 */
6
7
namespace AlecRabbit\Currency;
8
9
use AlecRabbit\Currency\Contracts\CurrencyInterface;
10
11
class Currency implements CurrencyInterface, \JsonSerializable
12
{
13
    private const _CODE_LENGTH = 9;
14
15
    /** @var string */
16
    private $code;
17
18
    /**
19
     * @param string $code
20
     */
21 136
    public function __construct(string $code)
22
    {
23 136
        if (\strlen($code) > static::_CODE_LENGTH) {
24 1
            throw new \InvalidArgumentException('Currency code must be not longer then ' . static::_CODE_LENGTH);
25
        }
26 135
        $this->code = strtoupper($code);
27 135
    }
28
29
    /**
30
     * Checks whether this currency is the same as an other.
31
     *
32
     * @param Currency $currency
33
     *
34
     * @return bool
35
     */
36 51
    public function equals(Currency $currency): bool
37
    {
38 51
        return $this->code === $currency->code;
39
    }
40
41
    /**
42
     * @return string
43
     */
44 22
    public function __toString()
45
    {
46 22
        return $this->getCode();
47
    }
48
49
    /**
50
     * Returns the currency code.
51
     */
52 23
    public function getCode(): string
53
    {
54 23
        return $this->code;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2
    public function jsonSerialize(): string
61
    {
62 2
        return $this->code;
63
    }
64
}
65