|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Bpost\BpostApiClient\Bpost; |
|
5
|
|
|
|
|
6
|
|
|
use Bpost\BpostApiClient\Bpost\Label\Barcode; |
|
7
|
|
|
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException; |
|
8
|
|
|
use SimpleXMLElement; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* bPost Label class |
|
12
|
|
|
* |
|
13
|
|
|
* @author Tijs Verkoyen <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class Label |
|
16
|
|
|
{ |
|
17
|
|
|
public const LABEL_MIME_TYPE_IMAGE_PNG = 'image/png'; |
|
18
|
|
|
public const LABEL_MIME_TYPE_IMAGE_PDF = 'image/pdf'; |
|
19
|
|
|
public const LABEL_MIME_TYPE_APPLICATION_PDF = 'application/pdf'; |
|
20
|
|
|
|
|
21
|
|
|
private array $barcodes = []; |
|
22
|
|
|
private ?string $mimeType = null; |
|
23
|
|
|
private string $bytes = ''; |
|
24
|
|
|
|
|
25
|
|
|
public function addBarcode(Barcode $barcode): void |
|
26
|
|
|
{ |
|
27
|
|
|
$this->barcodes[] = $barcode; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function setBarcodes(array $barcodes): void |
|
31
|
|
|
{ |
|
32
|
|
|
$this->barcodes = $barcodes; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** @return Barcode[] */ |
|
36
|
|
|
public function getBarcodes(): array |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->barcodes; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getBarcode(): string |
|
42
|
|
|
{ |
|
43
|
|
|
if (!empty($this->barcodes)) { |
|
44
|
|
|
$first = $this->barcodes[0]; |
|
45
|
|
|
return $first->getBarcode() ?? ''; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return ''; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function setBytes(string $bytes): void |
|
52
|
|
|
{ |
|
53
|
|
|
$this->bytes = $bytes; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getBytes(): string |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->bytes; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @throws BpostInvalidValueException |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setMimeType(string $mimeType): void |
|
65
|
|
|
{ |
|
66
|
|
|
if (!in_array($mimeType, self::getPossibleMimeTypeValues(), true)) { |
|
67
|
|
|
throw new BpostInvalidValueException('mimeType', $mimeType, self::getPossibleMimeTypeValues()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->mimeType = $mimeType; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getMimeType(): ?string |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->mimeType; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** @return string[] */ |
|
79
|
|
|
public static function getPossibleMimeTypeValues(): array |
|
80
|
|
|
{ |
|
81
|
|
|
return [ |
|
82
|
|
|
self::LABEL_MIME_TYPE_IMAGE_PNG, |
|
83
|
|
|
self::LABEL_MIME_TYPE_IMAGE_PDF, |
|
84
|
|
|
self::LABEL_MIME_TYPE_APPLICATION_PDF, |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Output the bytes directly to the screen |
|
91
|
|
|
*/ |
|
92
|
|
|
public function output() |
|
93
|
|
|
{ |
|
94
|
|
|
header('Content-type: ' . $this->getMimeType()); |
|
95
|
|
|
echo $this->getBytes(); |
|
96
|
|
|
exit; |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @throws BpostInvalidValueException |
|
101
|
|
|
*/ |
|
102
|
|
|
public static function createFromXML(SimpleXMLElement $xml): self |
|
103
|
|
|
{ |
|
104
|
|
|
$label = new self(); |
|
105
|
|
|
|
|
106
|
|
|
if (isset($xml->barcodeWithReference)) { |
|
107
|
|
|
foreach ($xml->barcodeWithReference as $barcodeWithReference) { |
|
108
|
|
|
$label->addBarcode(Barcode::createFromXML($barcodeWithReference)); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (!empty($xml->mimeType)) { |
|
113
|
|
|
$label->setMimeType((string) $xml->mimeType); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if (!empty($xml->bytes)) { |
|
117
|
|
|
$label->setBytes((string) base64_decode((string) $xml->bytes)); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $label; |
|
121
|
|
|
} |
|
122
|
|
|
} |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.