Test Failed
Pull Request — master (#6)
by Laurens
02:27
created

NumberField   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 62
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\ApplePassbook\MetaData\Field;
6
7
use LauLamanApps\ApplePassbook\Exception\InvalidArgumentException;
8
use LauLamanApps\ApplePassbook\Style\NumberStyle;
9
use LogicException;
10
11
class NumberField extends Field
12
{
13
    private string $currencyCode;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
14
    private NumberStyle $numberStyle;
15
16 19
    public function __construct(?string $key = null, $value = null, ?string $label = null)
17
    {
18 19
        parent::__construct($key, null, $label);
19
20 19
        if ($value !== null) {
21 13
            $this->setValue($value);
22
        }
23 19
    }
24
25
    /**
26
     * @param string|int|bool $value
27
     */
28 19
    public function setValue($value): void
29
    {
30 19
        if (!is_numeric($value)) {
31 6
            throw new InvalidArgumentException('Value should be numeric.');
32
        }
33
34 13
        parent::setValue($value);
35 13
    }
36
37 3
    public function setCurrencyCode(string $currencyCode): void
38
    {
39 3
        if (isset($this->numberStyle)) {
40 1
            throw new LogicException('You can not set both a \'currencyCode\' and a \'numberStyle\'. Please set only one of the 2.');
41
        }
42
43 2
        $this->currencyCode = $currencyCode;
44 2
    }
45
46 3
    public function setNumberStyle(NumberStyle $numberStyle): void
47
    {
48 3
        if (isset($this->currencyCode)) {
49 1
            throw new LogicException('You can not set both a \'currencyCode\' and a \'numberStyle\'. Please set only one of the 2.');
50
        }
51
52 2
        $this->numberStyle = $numberStyle;
53 2
    }
54
55
    /**
56
     * @return array<string, array<int, string>|bool|int|string>
57
     */
58 11
    public function getMetadata(): array
59
    {
60 11
        $data = parent::getMetadata();
61 11
        if (isset($this->currencyCode)) {
62 1
            $data['currencyCode'] = $this->currencyCode;
63
        }
64
65 11
        if (isset($this->numberStyle)) {
66 1
            $data['numberStyle'] = (string) $this->numberStyle->getValue();
67
        }
68
69 11
        return $data;
70
    }
71
}
72