1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bpost\BpostApiClient\Bpost; |
4
|
|
|
|
5
|
|
|
use Bpost\BpostApiClient\Bpost\Label\Barcode; |
6
|
|
|
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException; |
7
|
|
|
use SimpleXMLElement; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* bPost Label class |
11
|
|
|
* |
12
|
|
|
* @author Tijs Verkoyen <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Label |
15
|
|
|
{ |
16
|
|
|
const LABEL_MIME_TYPE_IMAGE_PNG = 'image/png'; |
17
|
|
|
const LABEL_MIME_TYPE_IMAGE_PDF = 'image/pdf'; |
18
|
|
|
const LABEL_MIME_TYPE_APPLICATION_PDF = 'application/pdf'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Barcode[] |
22
|
|
|
*/ |
23
|
|
|
private $barcodes; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $mimeType; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $bytes; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Barcode $barcode |
37
|
|
|
*/ |
38
|
|
|
public function addBarcode(Barcode $barcode) |
39
|
|
|
{ |
40
|
|
|
$this->barcodes[] = $barcode; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getBarcode() |
47
|
|
|
{ |
48
|
|
|
if (is_array($this->getBarcodes())) { |
|
|
|
|
49
|
|
|
$barcode = current($this->getBarcodes()); |
50
|
|
|
|
51
|
|
|
return $barcode->getBarcode(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return ''; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Barcode[] $barcodes |
59
|
|
|
*/ |
60
|
|
|
public function setBarcodes(array $barcodes) |
61
|
|
|
{ |
62
|
|
|
$this->barcodes = $barcodes; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return Barcode[] |
67
|
|
|
*/ |
68
|
|
|
public function getBarcodes() |
69
|
|
|
{ |
70
|
|
|
return $this->barcodes; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $bytes |
75
|
|
|
*/ |
76
|
|
|
public function setBytes($bytes) |
77
|
|
|
{ |
78
|
|
|
$this->bytes = $bytes; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getBytes() |
85
|
|
|
{ |
86
|
|
|
return $this->bytes; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $mimeType |
91
|
|
|
* |
92
|
|
|
* @throws BpostInvalidValueException |
93
|
|
|
*/ |
94
|
|
|
public function setMimeType($mimeType) |
95
|
|
|
{ |
96
|
|
|
if (!in_array($mimeType, self::getPossibleMimeTypeValues())) { |
97
|
|
|
throw new BpostInvalidValueException('mimeType', $mimeType, self::getPossibleMimeTypeValues()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->mimeType = $mimeType; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getMimeType() |
107
|
|
|
{ |
108
|
|
|
return $this->mimeType; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
public static function getPossibleMimeTypeValues() |
115
|
|
|
{ |
116
|
|
|
return array( |
117
|
|
|
self::LABEL_MIME_TYPE_IMAGE_PNG, |
118
|
|
|
self::LABEL_MIME_TYPE_IMAGE_PDF, |
119
|
|
|
self::LABEL_MIME_TYPE_APPLICATION_PDF, |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Output the bytes directly to the screen |
125
|
|
|
*/ |
126
|
|
|
public function output() |
127
|
|
|
{ |
128
|
|
|
header('Content-type: ' . $this->getMimeType()); |
129
|
|
|
echo $this->getBytes(); |
130
|
|
|
exit; |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param SimpleXMLElement $xml |
135
|
|
|
* |
136
|
|
|
* @return Label |
137
|
|
|
* |
138
|
|
|
* @throws BpostInvalidValueException |
139
|
|
|
*/ |
140
|
|
|
public static function createFromXML(SimpleXMLElement $xml) |
141
|
|
|
{ |
142
|
|
|
$label = new Label(); |
143
|
|
|
if (isset($xml->barcodeWithReference)) { |
144
|
|
|
foreach ($xml->barcodeWithReference as $barcodeWithReference) { |
145
|
|
|
$label->addBarcode(Barcode::createFromXML($barcodeWithReference)); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
if (isset($xml->mimeType) && $xml->mimeType != '') { |
149
|
|
|
$label->setMimeType((string) $xml->mimeType); |
150
|
|
|
} |
151
|
|
|
if (isset($xml->bytes) && $xml->bytes != '') { |
152
|
|
|
$label->setBytes((string) base64_decode($xml->bytes)); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $label; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|