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