1 | <?php |
||
9 | |||
10 | class Barcode |
||
11 | { |
||
12 | private string $altText; |
||
|
|||
13 | private BarcodeFormat $format; |
||
14 | private string $message; |
||
15 | private string $messageEncoding = 'iso-8859-1'; |
||
16 | |||
17 | 10 | public function __construct(?BarcodeFormat $format = null, ?string $message = null, ?string $altText = null) |
|
18 | { |
||
19 | 10 | $this->format = $format ?? BarcodeFormat::pdf417(); |
|
20 | |||
21 | 10 | if ($message) { |
|
22 | 3 | $this->message = $message; |
|
23 | } |
||
24 | |||
25 | 10 | if ($altText){ |
|
26 | 1 | $this->altText = $altText; |
|
27 | } |
||
28 | 10 | } |
|
29 | |||
30 | 7 | public function setFormat(BarcodeFormat $format): void |
|
31 | { |
||
32 | 7 | $this->format = $format; |
|
33 | 7 | } |
|
34 | |||
35 | 7 | public function setMessage(string $message): void |
|
36 | { |
||
37 | 7 | $this->message = $message; |
|
38 | 7 | } |
|
39 | |||
40 | 1 | public function setAltText(string $altText): void |
|
41 | { |
||
42 | 1 | $this->altText = $altText; |
|
43 | 1 | } |
|
44 | |||
45 | 1 | public function setMessageEncoding(string $messageEncoding): void |
|
46 | { |
||
47 | 1 | $this->messageEncoding = $messageEncoding; |
|
48 | 1 | } |
|
49 | |||
50 | /** |
||
51 | * @return array<string, string> |
||
52 | */ |
||
53 | 10 | public function toArray(): array |
|
54 | { |
||
55 | 10 | $this->validate(); |
|
56 | |||
57 | $data = [ |
||
58 | 10 | 'format' => (string) $this->format->getValue(), |
|
59 | 10 | 'message' => $this->message, |
|
60 | 10 | 'messageEncoding' => $this->messageEncoding, |
|
61 | ]; |
||
62 | |||
63 | 10 | if (isset($this->altText)) { |
|
64 | 1 | $data['altText'] = $this->altText; |
|
65 | } |
||
66 | |||
67 | 10 | return $data; |
|
68 | } |
||
69 | |||
70 | 10 | private function validate(): void |
|
71 | { |
||
72 | 10 | if (!isset($this->message)) { |
|
73 | throw new LogicException('no message specified'); |
||
77 |