1 | <?php |
||||
2 | |||||
3 | namespace Bpost\BpostApiClient\Bpost; |
||||
4 | |||||
5 | use Bpost\BpostApiClient\Bpost\Order\Box; |
||||
6 | use Bpost\BpostApiClient\Bpost\Order\Line; |
||||
7 | use Bpost\BpostApiClient\Exception\BpostNotImplementedException; |
||||
8 | use Bpost\BpostApiClient\Exception\XmlException\BpostXmlNoReferenceFoundException; |
||||
9 | use DOMDocument; |
||||
10 | use DOMElement; |
||||
11 | use SimpleXMLElement; |
||||
12 | |||||
13 | /** |
||||
14 | * bPost Order class |
||||
15 | * |
||||
16 | * @author Tijs Verkoyen <[email protected]> |
||||
17 | */ |
||||
18 | class Order |
||||
19 | { |
||||
20 | /** |
||||
21 | * Order reference: unique ID used in your web shop to assign to an order. |
||||
22 | * The value of this parameter is not managed by bpost. If the value |
||||
23 | * already exists, it will update current order info. Existing boxes will |
||||
24 | * not be changed, new boxes will be added. |
||||
25 | * |
||||
26 | * @var string |
||||
27 | */ |
||||
28 | private $reference; |
||||
29 | |||||
30 | /** |
||||
31 | * This information is used on your invoice and allows you to attribute |
||||
32 | * different cost centers |
||||
33 | * |
||||
34 | * @var string |
||||
35 | */ |
||||
36 | private $costCenter; |
||||
37 | |||||
38 | /** |
||||
39 | * The items that are included in the order. |
||||
40 | * Order lines are shown in the back end of the Shipping Manager and |
||||
41 | * facilitate the use of the tool. |
||||
42 | * |
||||
43 | * @var array |
||||
44 | */ |
||||
45 | private $lines; |
||||
46 | |||||
47 | /** |
||||
48 | * Box tags |
||||
49 | * |
||||
50 | * @var array |
||||
51 | */ |
||||
52 | private $boxes; |
||||
53 | |||||
54 | /** |
||||
55 | * Create an order |
||||
56 | * |
||||
57 | * @param string $reference |
||||
58 | */ |
||||
59 | public function __construct($reference) |
||||
60 | { |
||||
61 | $this->setReference($reference); |
||||
62 | } |
||||
63 | |||||
64 | /** |
||||
65 | * @param Box[] $boxes |
||||
66 | */ |
||||
67 | public function setBoxes(array $boxes) |
||||
68 | { |
||||
69 | $this->boxes = $boxes; |
||||
70 | } |
||||
71 | |||||
72 | /** |
||||
73 | * @return Box[] |
||||
74 | */ |
||||
75 | public function getBoxes() |
||||
76 | { |
||||
77 | return $this->boxes; |
||||
78 | } |
||||
79 | |||||
80 | /** |
||||
81 | * Add a box |
||||
82 | * |
||||
83 | * @param Box $box |
||||
84 | */ |
||||
85 | public function addBox(Box $box) |
||||
86 | { |
||||
87 | $this->boxes[] = $box; |
||||
88 | } |
||||
89 | |||||
90 | /** |
||||
91 | * @param string $costCenter |
||||
92 | */ |
||||
93 | public function setCostCenter($costCenter) |
||||
94 | { |
||||
95 | $this->costCenter = $costCenter; |
||||
96 | } |
||||
97 | |||||
98 | /** |
||||
99 | * @return string |
||||
100 | */ |
||||
101 | public function getCostCenter() |
||||
102 | { |
||||
103 | return $this->costCenter; |
||||
104 | } |
||||
105 | |||||
106 | /** |
||||
107 | * @param Line[] $lines |
||||
108 | */ |
||||
109 | public function setLines(array $lines) |
||||
110 | { |
||||
111 | $this->lines = $lines; |
||||
112 | } |
||||
113 | |||||
114 | /** |
||||
115 | * @return Line[] |
||||
116 | */ |
||||
117 | public function getLines() |
||||
118 | { |
||||
119 | return $this->lines; |
||||
120 | } |
||||
121 | |||||
122 | /** |
||||
123 | * Add an order line |
||||
124 | * |
||||
125 | * @param Line $line |
||||
126 | */ |
||||
127 | public function addLine(Line $line) |
||||
128 | { |
||||
129 | $this->lines[] = $line; |
||||
130 | } |
||||
131 | |||||
132 | /** |
||||
133 | * @param string $reference |
||||
134 | */ |
||||
135 | public function setReference($reference) |
||||
136 | { |
||||
137 | $this->reference = $reference; |
||||
138 | } |
||||
139 | |||||
140 | /** |
||||
141 | * @return string |
||||
142 | */ |
||||
143 | public function getReference() |
||||
144 | { |
||||
145 | return $this->reference; |
||||
146 | } |
||||
147 | |||||
148 | /** |
||||
149 | * Return the object as an array for usage in the XML |
||||
150 | * |
||||
151 | * @param DOMDocument $document |
||||
152 | * @param string $accountId |
||||
153 | * |
||||
154 | * @return DOMElement |
||||
155 | */ |
||||
156 | public function toXML(DOMDocument $document, $accountId) |
||||
157 | { |
||||
158 | $order = $document->createElement( |
||||
159 | 'tns:order' |
||||
160 | ); |
||||
161 | $order->setAttribute( |
||||
162 | 'xmlns:common', |
||||
163 | 'http://schema.post.be/shm/deepintegration/v5/common' |
||||
164 | ); |
||||
165 | $order->setAttribute( |
||||
166 | 'xmlns:tns', |
||||
167 | 'http://schema.post.be/shm/deepintegration/v5/' |
||||
168 | ); |
||||
169 | $order->setAttribute( |
||||
170 | 'xmlns', |
||||
171 | 'http://schema.post.be/shm/deepintegration/v5/national' |
||||
172 | ); |
||||
173 | $order->setAttribute( |
||||
174 | 'xmlns:international', |
||||
175 | 'http://schema.post.be/shm/deepintegration/v5/international' |
||||
176 | ); |
||||
177 | $order->setAttribute( |
||||
178 | 'xmlns:xsi', |
||||
179 | 'http://www.w3.org/2001/XMLSchema-instance' |
||||
180 | ); |
||||
181 | $order->setAttribute( |
||||
182 | 'xsi:schemaLocation', |
||||
183 | 'http://schema.post.be/shm/deepintegration/v5/' |
||||
184 | ); |
||||
185 | |||||
186 | $document->appendChild($order); |
||||
187 | |||||
188 | $order->appendChild( |
||||
189 | $document->createElement( |
||||
190 | 'tns:accountId', |
||||
191 | (string) $accountId |
||||
192 | ) |
||||
193 | ); |
||||
194 | |||||
195 | if ($this->getReference() !== null) { |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
196 | $order->appendChild( |
||||
197 | $document->createElement( |
||||
198 | 'tns:reference', |
||||
199 | $this->getReference() |
||||
200 | ) |
||||
201 | ); |
||||
202 | } |
||||
203 | if ($this->getCostCenter() !== null) { |
||||
0 ignored issues
–
show
|
|||||
204 | $order->appendChild( |
||||
205 | $document->createElement( |
||||
206 | 'tns:costCenter', |
||||
207 | $this->getCostCenter() |
||||
208 | ) |
||||
209 | ); |
||||
210 | } |
||||
211 | |||||
212 | $lines = $this->getLines(); |
||||
213 | if (!empty($lines)) { |
||||
214 | foreach ($lines as $line) { |
||||
215 | /** @var $line \Bpost\BpostApiClient\Bpost\Order\Line */ |
||||
216 | $order->appendChild( |
||||
217 | $line->toXML($document, 'tns') |
||||
218 | ); |
||||
219 | } |
||||
220 | } |
||||
221 | |||||
222 | $boxes = $this->getBoxes(); |
||||
223 | if (!empty($boxes)) { |
||||
224 | foreach ($boxes as $box) { |
||||
225 | /** @var $box \Bpost\BpostApiClient\Bpost\Order\Box */ |
||||
226 | $order->appendChild( |
||||
227 | $box->toXML($document, 'tns') |
||||
228 | ); |
||||
229 | } |
||||
230 | } |
||||
231 | |||||
232 | return $order; |
||||
233 | } |
||||
234 | |||||
235 | /** |
||||
236 | * @param SimpleXMLElement $xml |
||||
237 | * |
||||
238 | * @return Order |
||||
239 | * |
||||
240 | * @throws BpostXmlNoReferenceFoundException |
||||
241 | * @throws BpostNotImplementedException |
||||
242 | */ |
||||
243 | public static function createFromXML(SimpleXMLElement $xml) |
||||
244 | { |
||||
245 | // @todo work with classmaps ... |
||||
246 | if (!isset($xml->reference)) { |
||||
247 | throw new BpostXmlNoReferenceFoundException(); |
||||
248 | } |
||||
249 | |||||
250 | $order = new Order((string) $xml->reference); |
||||
251 | |||||
252 | if (isset($xml->costCenter) && $xml->costCenter != '') { |
||||
253 | $order->setCostCenter((string) $xml->costCenter); |
||||
254 | } |
||||
255 | if (isset($xml->orderLine)) { |
||||
256 | foreach ($xml->orderLine as $orderLine) { |
||||
257 | $order->addLine( |
||||
258 | Line::createFromXML($orderLine) |
||||
0 ignored issues
–
show
It seems like
$orderLine can also be of type null ; however, parameter $xml of Bpost\BpostApiClient\Bpo...r\Line::createFromXML() does only seem to accept SimpleXMLElement , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
259 | ); |
||||
260 | } |
||||
261 | } |
||||
262 | if (isset($xml->box)) { |
||||
263 | foreach ($xml->box as $box) { |
||||
264 | $order->addBox(Box::createFromXML($box)); |
||||
0 ignored issues
–
show
It seems like
$box can also be of type null ; however, parameter $xml of Bpost\BpostApiClient\Bpo...er\Box::createFromXML() does only seem to accept SimpleXMLElement , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
265 | } |
||||
266 | } |
||||
267 | |||||
268 | return $order; |
||||
269 | } |
||||
270 | } |
||||
271 |