Passed
Push — google-wallet-support ( 15e26d...cd8842 )
by Razvan
04:15
created

NumberField::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Passbook package.
5
 *
6
 * (c) Eymen Gunay <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Passbook\Apple;
13
14
use InvalidArgumentException;
15
16
/**
17
 * Class NumberField
18
 *
19
 * @author Florian Morello <[email protected]>
20
 */
21
class NumberField extends Field
22
{
23
    /**
24
     * @var string
25
     */
26
    public const NUMBER_STYLE_DECIMAL = 'PKNumberStyleDecimal';
27
28
    /**
29
     * @var string
30
     */
31
    public const NUMBER_STYLE_PERCENT = 'PKNumberStylePercent';
32
33
    /**
34
     * @var string
35
     */
36
    public const NUMBER_STYLE_SCIENTIFIC = 'PKNumberStyleScientific';
37
38
    /**
39
     * ISO 4217
40
     *
41
     * @var string
42
     */
43
    protected $currencyCode = null;
44
45
    /**
46
     * @var string
47
     */
48
    protected $numberStyle = null;
49
50
    /**
51
     * NumberField constructor.
52
     * @param $key
53
     * @param $value
54
     * @throws InvalidArgumentException
55
     */
56 3
    public function __construct($key, $value)
57
    {
58 3
        if (!is_numeric($value)) {
59
            throw new InvalidArgumentException("Value has to be numeric. '$value' given!");
60
        }
61
62 3
        parent::__construct($key, $value);
63
    }
64
65
    /**
66
     * @return array
67
     */
68 2
    public function toArray()
69
    {
70 2
        $array = parent::toArray();
71 2
        if ($this->getCurrencyCode() !== null) {
0 ignored issues
show
introduced by
The condition $this->getCurrencyCode() !== null is always true.
Loading history...
72 2
            $array['currencyCode'] = $this->getCurrencyCode();
73
        }
74 2
        if ($this->getNumberStyle() !== null) {
0 ignored issues
show
introduced by
The condition $this->getNumberStyle() !== null is always true.
Loading history...
75 1
            $array['numberStyle'] = $this->getNumberStyle();
76
        }
77
78 2
        return $array;
79
    }
80
81
    /**
82
     * @param string $currencyCode an ISO 4217 currency code
83
     *
84
     * @return $this
85
     */
86 2
    public function setCurrencyCode($currencyCode)
87
    {
88 2
        $this->currencyCode = $currencyCode;
89
90 2
        return $this;
91
    }
92
93
    /**
94
     * @return string
95
     */
96 2
    public function getCurrencyCode()
97
    {
98 2
        return $this->currencyCode;
99
    }
100
101
    /**
102
     * @param string $numberStyle
103
     *
104
     * @return $this
105
     */
106 1
    public function setNumberStyle($numberStyle)
107
    {
108 1
        $this->numberStyle = $numberStyle;
109
110 1
        return $this;
111
    }
112
113
    /**
114
     * @return string
115
     */
116 2
    public function getNumberStyle()
117
    {
118 2
        return $this->numberStyle;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     *
124
     * @return int|float
125
     */
126 3
    public function getValue()
127
    {
128
        // Ensure value is int or float; adding 0 will convert type from string
129 3
        return 0 + parent::getValue();
130
    }
131
}
132