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\DataDetector; |
9
|
|
|
use LauLamanApps\ApplePassbook\Style\TextAlignment; |
10
|
|
|
|
11
|
|
|
class Field |
12
|
|
|
{ |
13
|
|
|
private string $key; |
|
|
|
|
14
|
|
|
/** @var string|int|bool */ |
15
|
|
|
private $value; |
16
|
|
|
private string $label; |
17
|
|
|
/** @var DataDetector[]|null */ |
18
|
|
|
private ?array $dataDetectorTypes; |
19
|
|
|
private string $changeMessage; |
20
|
|
|
private TextAlignment $textAlignment; |
21
|
|
|
private string $attributedValue; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string|int|bool $value |
25
|
|
|
*/ |
26
|
|
|
public function __construct(?string $key = null, $value = null, ?string $label = null) |
27
|
|
|
{ |
28
|
|
|
if ($key !== null) { |
29
|
|
|
$this->setKey($key); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($value !== null) { |
33
|
|
|
$this->setValue($value); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($label !== null) { |
37
|
|
|
$this->setLabel($label); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setKey(string $key): void |
42
|
|
|
{ |
43
|
|
|
$this->key = $key; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string|int|bool $value |
48
|
43 |
|
*/ |
49
|
|
|
public function setValue($value): void |
50
|
43 |
|
{ |
51
|
39 |
|
if (!is_scalar($value)) { |
52
|
|
|
throw new InvalidArgumentException('Value should be a scalar type.'); |
53
|
|
|
} |
54
|
43 |
|
|
55
|
11 |
|
$this->value = $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
43 |
|
public function setLabel(string $label): void |
59
|
1 |
|
{ |
60
|
|
|
$this->label = $label; |
61
|
43 |
|
} |
62
|
|
|
|
63
|
40 |
|
public function addDataDetectorType(DataDetector $dataDetector): void |
64
|
|
|
{ |
65
|
40 |
|
$this->dataDetectorTypes[$dataDetector->getValue()] = $dataDetector; |
66
|
40 |
|
} |
67
|
|
|
|
68
|
37 |
|
public function setChangeMessage(string $changeMessage): void |
69
|
|
|
{ |
70
|
37 |
|
$this->changeMessage = $changeMessage; |
71
|
3 |
|
} |
72
|
|
|
|
73
|
|
|
public function setTextAlignment(TextAlignment $textAlignment): void |
74
|
34 |
|
{ |
75
|
34 |
|
$this->textAlignment = $textAlignment; |
76
|
|
|
} |
77
|
5 |
|
|
78
|
|
|
public function setAttributedValue(string $attributedValue): void |
79
|
5 |
|
{ |
80
|
5 |
|
$this->attributedValue = $attributedValue; |
81
|
|
|
} |
82
|
3 |
|
|
83
|
|
|
/** |
84
|
3 |
|
* @return array<string, array<int, string>|bool|int|string> |
85
|
3 |
|
*/ |
86
|
|
|
public function getMetadata(): array |
87
|
4 |
|
{ |
88
|
|
|
$data = [ |
89
|
4 |
|
'key' => $this->key, |
90
|
4 |
|
'value'=> $this->value, |
91
|
|
|
]; |
92
|
3 |
|
|
93
|
|
|
if (isset($this->label)) { |
94
|
3 |
|
$data['label'] = $this->label; |
95
|
3 |
|
} |
96
|
|
|
|
97
|
3 |
|
if (isset($this->dataDetectorTypes)) { |
98
|
|
|
foreach ($this->dataDetectorTypes as $dataDetector) { |
99
|
3 |
|
$data['dataDetectorTypes'][] = (string) $dataDetector->getValue(); |
100
|
3 |
|
} |
101
|
|
|
} |
102
|
32 |
|
|
103
|
|
|
if (isset($this->changeMessage)) { |
104
|
|
|
$data['changeMessage'] = $this->changeMessage; |
105
|
32 |
|
} |
106
|
32 |
|
|
107
|
|
|
if (isset($this->textAlignment)) { |
108
|
|
|
$data['textAlignment'] = (string) $this->textAlignment->getValue(); |
109
|
32 |
|
} |
110
|
5 |
|
|
111
|
|
|
if (isset($this->attributedValue)) { |
112
|
|
|
$data['attributedValue'] = $this->attributedValue; |
113
|
32 |
|
} |
114
|
3 |
|
|
115
|
3 |
|
return $data; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|