1 | <?php |
||
11 | class NumberField extends Field |
||
12 | { |
||
13 | private string $currencyCode; |
||
|
|||
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 |