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