|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ShippoClient\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use TurmericSpice\ReadableAttributes; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* response type is different between each carrier. |
|
9
|
|
|
* if carrier label is generated via shippo, you will get type same to Transaction object. |
|
10
|
|
|
* if not, you will get type same to Tracks object. |
|
11
|
|
|
*/ |
|
12
|
|
|
class WebhookTracks |
|
13
|
|
|
{ |
|
14
|
|
|
use ReadableAttributes { |
|
15
|
|
|
// tracks object properties |
|
16
|
|
|
mayHaveAsString as public getCarrier; |
|
17
|
|
|
mayHaveAsString as public getTrackingNumber; |
|
18
|
|
|
mayHaveAsString as public getEta; |
|
19
|
|
|
// transaction object properties |
|
20
|
|
|
mayHaveAsString as public getObjectState; |
|
21
|
|
|
mayHaveAsString as public getObjectStatus; |
|
22
|
|
|
mayHaveAsString as public getObjectId; |
|
23
|
|
|
mayHaveAsString as public getObjectOwner; |
|
24
|
|
|
mayHaveAsString as public getLabelUrl; |
|
25
|
|
|
mayHaveAsString as public getTrackingUrlProvider; |
|
26
|
|
|
mayHaveAsString as public getCommercialInvoiceUrl; |
|
27
|
|
|
mayHaveAsString as public getCustomsNote; |
|
28
|
|
|
mayHaveAsString as public getSubmissionNote; |
|
29
|
|
|
mayHaveAsString as public getMetadata; |
|
30
|
|
|
mayHaveAsArray as public getMessages; |
|
31
|
|
|
// common properties |
|
32
|
|
|
mayHaveAsString as public getTrackingNumber; |
|
33
|
|
|
toArray as public __toArray; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* transaction object property |
|
38
|
|
|
* |
|
39
|
|
|
* @return \DateTime |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getObjectCreated() |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->attributes->mayHave('object_created')->asInstanceOf('\\DateTime'); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* transaction object property |
|
48
|
|
|
* |
|
49
|
|
|
* @return \DateTime |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getObjectUpdated() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->attributes->mayHave('object_updated')->asInstanceOf('\\DateTime'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* tracks object property |
|
58
|
|
|
* |
|
59
|
|
|
* @return \ShippoClient\Entity\Location |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getAddressFrom() |
|
62
|
|
|
{ |
|
63
|
|
|
$addressFrom = $this->attributes->mayHave('address_from')->asArray(); |
|
64
|
|
|
|
|
65
|
|
|
return new Location($addressFrom); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* tracks object property |
|
70
|
|
|
* |
|
71
|
|
|
* @return \ShippoClient\Entity\Location |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getAddressTo() |
|
74
|
|
|
{ |
|
75
|
|
|
$addressTo = $this->attributes->mayHave('address_to')->asArray(); |
|
76
|
|
|
|
|
77
|
|
|
return new Location($addressTo); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* tracks object property |
|
82
|
|
|
* |
|
83
|
|
|
* @return \ShippoClient\Entity\ServiceLevel |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getServiceLevel() |
|
86
|
|
|
{ |
|
87
|
|
|
$serviceLevel = $this->attributes->mayHave('servicelevel')->asArray(); |
|
88
|
|
|
|
|
89
|
|
|
return new ServiceLevel($serviceLevel); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* common property |
|
94
|
|
|
* |
|
95
|
|
|
* @return \ShippoClient\Entity\TrackingStatus |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getTrackingStatus() |
|
98
|
|
|
{ |
|
99
|
|
|
return new TrackingStatus($this->attributes->mayHave('tracking_status')->asArray()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* common property |
|
104
|
|
|
* |
|
105
|
|
|
* @return \ShippoClient\Entity\TrackingHistory |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getTrackingHistory() |
|
108
|
|
|
{ |
|
109
|
|
|
$entities = $this->attributes->mayHave('tracking_history') |
|
110
|
|
|
->asInstanceArray('ShippoClient\\Entity\\TrackingStatus'); |
|
111
|
|
|
|
|
112
|
|
|
return new TrackingHistory($entities); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function toArray() |
|
116
|
|
|
{ |
|
117
|
|
|
return [ |
|
118
|
|
|
// tracks |
|
119
|
|
|
'carrier' => $this->getCarrier(), |
|
|
|
|
|
|
120
|
|
|
'tracking_number' => $this->getTrackingNumber(), |
|
121
|
|
|
'tracking_status' => $this->getTrackingStatus()->toArray(), |
|
122
|
|
|
'tracking_history' => $this->getTrackingHistory()->toArray(), |
|
123
|
|
|
'eta' => $this->getEta(), |
|
|
|
|
|
|
124
|
|
|
'address_from' => $this->getAddressFrom()->toArray(), |
|
125
|
|
|
'address_to' => $this->getAddressTo()->toArray(), |
|
126
|
|
|
'servicelevel' => $this->getServiceLevel()->toArray(), |
|
127
|
|
|
// transaction |
|
128
|
|
|
'object_state' => $this->getObjectState(), |
|
|
|
|
|
|
129
|
|
|
'object_status' => $this->getObjectStatus(), |
|
|
|
|
|
|
130
|
|
|
'object_created' => $this->getObjectCreated(), |
|
131
|
|
|
'object_updated' => $this->getObjectUpdated(), |
|
132
|
|
|
'object_id' => $this->getObjectId(), |
|
|
|
|
|
|
133
|
|
|
'object_owner' => $this->getObjectOwner(), |
|
|
|
|
|
|
134
|
|
|
'label_url' => $this->getLabelUrl(), |
|
|
|
|
|
|
135
|
|
|
'tracking_url_provider' => $this->getTrackingUrlProvider(), |
|
|
|
|
|
|
136
|
|
|
'commercial_invoice_url' => $this->getCommercialInvoiceUrl(), |
|
|
|
|
|
|
137
|
|
|
'customs_note' => $this->getCustomsNote(), |
|
|
|
|
|
|
138
|
|
|
'submission_note' => $this->getSubmissionNote(), |
|
|
|
|
|
|
139
|
|
|
'metadata' => $this->getMetadata(), |
|
|
|
|
|
|
140
|
|
|
'messages' => $this->getMessages(), |
|
141
|
|
|
]; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: