1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Savjee\BpostTrack; |
4
|
|
|
|
5
|
|
|
use Curl\Curl; |
6
|
|
|
use Exception; |
7
|
|
|
use Sunra\PhpSimple\HtmlDomParser; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Created by PhpStorm. |
11
|
|
|
* User: Xavier |
12
|
|
|
* Date: 29/08/15 |
13
|
|
|
* Time: 12:00 |
14
|
|
|
*/ |
15
|
|
|
class BpostPackage |
16
|
|
|
{ |
17
|
|
|
const BPOST_API_ENDPOINT = 'http://track.bpost.be/btr/api/'; |
18
|
|
|
const LANGUAGE = 'nl'; // nl, en, fr |
19
|
|
|
|
20
|
|
|
// Pakcage information |
21
|
|
|
private $statusUpdates = array(); |
22
|
|
|
private $itemNumber; |
23
|
|
|
private $weight; |
24
|
|
|
private $customerReference; |
25
|
|
|
private $requestedDeliveryMethod; |
26
|
|
|
|
27
|
|
|
/** @var SenderReceiver $sender */ |
28
|
|
|
private $sender; |
29
|
|
|
|
30
|
|
|
/** @var SenderReceiver $receiver */ |
31
|
|
|
private $receiver; |
32
|
|
|
|
33
|
|
|
private $translationsCache = null; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
public function __construct($itemNumber) |
37
|
|
|
{ |
38
|
|
|
$this->setItemNumber($itemNumber); |
39
|
|
|
$this->fetchTranslations(); |
40
|
|
|
$this->getTrackingInformation(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function fetchTranslations() |
44
|
|
|
{ |
45
|
|
|
$curl = new Curl(); |
46
|
|
|
$curl->get(self::BPOST_API_ENDPOINT . 'translations?lang=' . self::LANGUAGE); |
47
|
|
|
|
48
|
|
|
if ($curl->error) { |
49
|
|
|
throw new Exception('CURL error while fetching translations: ' . $curl->errorCode . ': ' . $curl->errorMessage); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->translationsCache = $curl->response; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function translateKey($key) |
56
|
|
|
{ |
57
|
|
|
if ($this->translationsCache == null) { |
58
|
|
|
$this->fetchTranslations(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this->translationsCache->event->$key->description; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function getTrackingInformation() |
65
|
|
|
{ |
66
|
|
|
$curl = new Curl(); |
67
|
|
|
$curl->get(self::BPOST_API_ENDPOINT . 'items?itemIdentifier=' . $this->getItemNumber()); |
68
|
|
|
|
69
|
|
|
if ($curl->error) { |
70
|
|
|
throw new Exception('CURL error while fetching tracking information: ' . $curl->errorCode . ': ' . $curl->errorMessage); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Curl library already decoded the JSON (cool!) |
74
|
|
|
$response = $curl->response[0]; |
75
|
|
|
|
76
|
|
|
// Decode sender |
77
|
|
|
$rawSender = $response->sender; |
78
|
|
|
$this->sender = new SenderReceiver($rawSender->countryCode, $rawSender->municipality, $rawSender->postcode); |
79
|
|
|
|
80
|
|
|
// Decode receiver |
81
|
|
|
$rawReceiver = $response->receiver; |
82
|
|
|
$this->receiver = new SenderReceiver($rawReceiver->countryCode, $rawReceiver->municipality, $rawReceiver->postcode, $rawReceiver->name); |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
// Some other stuff |
86
|
|
|
$this->weight = (int)$response->weightInGrams; |
87
|
|
|
$this->customerReference = $response->customerReference; |
88
|
|
|
$this->requestedDeliveryMethod = $response->requestedDeliveryMethod; |
89
|
|
|
|
90
|
|
|
// Decode events |
91
|
|
|
$rawEvents = $response->events; |
92
|
|
|
|
93
|
|
|
foreach ($rawEvents as $rawEvent) { |
94
|
|
|
$lang = self::LANGUAGE; |
95
|
|
|
$eventDescription = $this->translateKey($rawEvent->key); |
96
|
|
|
|
97
|
|
|
$statusUpdate = new StatusUpdate($rawEvent->date, $rawEvent->time, $eventDescription, $rawEvent->location->$lang); |
98
|
|
|
|
99
|
|
|
array_push($this->statusUpdates, $statusUpdate); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
|
|
public function getItemNumber() |
107
|
|
|
{ |
108
|
|
|
return $this->itemNumber; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
public function getStatusUpdates() |
115
|
|
|
{ |
116
|
|
|
return $this->statusUpdates; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
|
|
public function getWeight() |
123
|
|
|
{ |
124
|
|
|
return $this->weight; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
|
|
public function getCustomerReference() |
131
|
|
|
{ |
132
|
|
|
return $this->customerReference; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return mixed |
137
|
|
|
*/ |
138
|
|
|
public function getRequestedDeliveryMethod() |
139
|
|
|
{ |
140
|
|
|
return $this->requestedDeliveryMethod; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return SenderReceiver |
145
|
|
|
*/ |
146
|
|
|
public function getSender() |
147
|
|
|
{ |
148
|
|
|
return $this->sender; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return SenderReceiver |
153
|
|
|
*/ |
154
|
|
|
public function getReceiver() |
155
|
|
|
{ |
156
|
|
|
return $this->receiver; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
private function setItemNumber($itemNumber) |
160
|
|
|
{ |
161
|
|
|
$this->itemNumber = $itemNumber; |
162
|
|
|
} |
163
|
|
|
} |