Passed
Push — master ( 1f5936...ef809d )
by Alec
05:25
created

Currency::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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