Completed
Push — master ( a5b05f...ea8859 )
by Eymen
18s queued 12s
created

NumberField::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
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\Pass;
13
14
use InvalidArgumentException;
15
16
/**
17
 * Class NumberField
18
 *
19
 * @package Passbook\Pass
20
 * @author Florian Morello <[email protected]>
21
 */
22
class NumberField extends Field
23
{
24
    /**
25
     * @var string
26
     */
27
    const PKNumberStyleDecimal = 'PKNumberStyleDecimal';
28
29
    /**
30
     * @var string
31
     */
32
    const PKNumberStylePercent = 'PKNumberStylePercent';
33
34
    /**
35
     * @var string
36
     */
37
    const PKNumberStyleScientific = 'PKNumberStyleScientific';
38
39
    /**
40
     * ISO 4217
41
     *
42
     * @var string
43
     */
44
    protected $currencyCode = null;
45
46
    /**
47
     * @var string
48
     */
49
    protected $numberStyle = null;
50
51
    /**
52
     * NumberField constructor.
53
     * @param $key
54
     * @param $value
55
     * @throws InvalidArgumentException
56
     */
57
    public function __construct($key, $value)
58
    {
59
        if (!is_numeric($value)) {
60
            throw new InvalidArgumentException("Value has to be numeric. '$value' given!");
61
        }
62
63
        parent::__construct($key, $value);
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function toArray()
70
    {
71
        $array = parent::toArray();
72
        if ($this->getCurrencyCode() !== null) {
73
            $array['currencyCode'] = $this->getCurrencyCode();
74
        }
75
        if ($this->getNumberStyle() !== null) {
76
            $array['numberStyle'] = $this->getNumberStyle();
77
        }
78
79
        return $array;
80
    }
81
82
    /**
83
     * @param string $currencyCode an ISO 4217 currency code
84
     *
85
     * @return $this
86
     */
87
    public function setCurrencyCode($currencyCode)
88
    {
89
        $this->currencyCode = $currencyCode;
90
91
        return $this;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getCurrencyCode()
98
    {
99
        return $this->currencyCode;
100
    }
101
102
    /**
103
     * @param string $numberStyle
104
     *
105
     * @return $this
106
     */
107
    public function setNumberStyle($numberStyle)
108
    {
109
        $this->numberStyle = $numberStyle;
110
111
        return $this;
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    public function getNumberStyle()
118
    {
119
        return $this->numberStyle;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     *
125
     * @return int|float
126
     */
127
    public function getValue()
128
    {
129
        // Ensure value is int or float; adding 0 will convert type from string
130
        return 0 + parent::getValue();
131
    }
132
}