Passed
Push — master ( 1bd18b...966309 )
by Alec
03:04
created

Currency::__construct()   A

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