Currency   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 52
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCode() 0 3 1
A __construct() 0 6 2
A __toString() 0 3 1
A equals() 0 3 1
A jsonSerialize() 0 3 1
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