Test Failed
Push — master ( e5d517...cfa2aa )
by Laurens
01:33
created

NumberField::setValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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
    /**
14
     * @var string|null
15
     */
16
    private $currencyCode;
17
18
    /**
19
     * @var NumberStyle|null
20
     */
21
    private $numberStyle;
22
23 19
    public function __construct(?string $key = null, $value = null, ?string $label = null)
24
    {
25 19
        parent::__construct($key, null, $label);
26
27 19
        if ($value !== null) {
28 18
            $this->setValue($value);
29
        }
30 14
    }
31
32 18
    public function setValue($value): void
33
    {
34 18
        if (!is_numeric($value)) {
35 5
            throw new InvalidArgumentException('Value should be numeric.');
36
        }
37
38 13
        parent::setValue($value);
39 13
    }
40
41 3
    public function setCurrencyCode(string $currencyCode): void
42
    {
43 3
        if ($this->numberStyle) {
44 1
            throw new LogicException('You can not set both a \'currencyCode\' and a \'numberStyle\'. Please set only one of the 2.');
45
        }
46
47 2
        $this->currencyCode = $currencyCode;
48 2
    }
49
50 3
    public function setNumberStyle(NumberStyle $numberStyle): void
51
    {
52 3
        if ($this->currencyCode) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->currencyCode of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
53 1
            throw new LogicException('You can not set both a \'currencyCode\' and a \'numberStyle\'. Please set only one of the 2.');
54
        }
55
56 2
        $this->numberStyle = $numberStyle;
57 2
    }
58
59 11
    public function getMetadata(): array
60
    {
61 11
        $data = parent::getMetadata();
62 11
        if ($this->currencyCode) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->currencyCode of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
63 1
            $data['currencyCode'] = $this->currencyCode;
64
        }
65
66 11
        if ($this->numberStyle) {
67 1
            $data['numberStyle'] = $this->numberStyle->getValue();
68
        }
69
70 11
        return $data;
71
    }
72
}
73