1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Slince shipment tracker library |
4
|
|
|
* @author Tao <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Slince\ShipmentTracking\Foundation; |
7
|
|
|
|
8
|
|
|
use Carbon\Carbon; |
9
|
|
|
use Slince\ShipmentTracking\Foundation\Location\LocationInterface; |
10
|
|
|
|
11
|
|
|
class ShipmentEvent implements \JsonSerializable |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \DateTime |
15
|
|
|
* @deprecated |
16
|
|
|
*/ |
17
|
|
|
protected $date; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $description; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string|LocationInterface |
26
|
|
|
*/ |
27
|
|
|
protected $location; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $status; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \DateTime |
36
|
|
|
*/ |
37
|
|
|
protected $time; |
38
|
|
|
|
39
|
|
|
public function __construct(\DateTime $time = null, $description = null, $location = null) |
40
|
|
|
{ |
41
|
|
|
$this->setTime($time); |
|
|
|
|
42
|
|
|
$this->description = $description; |
43
|
|
|
$this->location = $location; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return \DateTime |
48
|
|
|
* @deprecated |
49
|
|
|
*/ |
50
|
|
|
public function getDate() |
51
|
|
|
{ |
52
|
|
|
return $this->date; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param \DateTime $date |
57
|
|
|
* @return ShipmentEvent |
58
|
|
|
* @deprecated |
59
|
|
|
*/ |
60
|
|
|
public function setDate($date) |
61
|
|
|
{ |
62
|
|
|
if (!$date instanceof \DateTime) { |
63
|
|
|
$date = Carbon::parse($date); |
64
|
|
|
} |
65
|
|
|
$this->setTime($date); |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return \DateTime |
71
|
|
|
*/ |
72
|
|
|
public function getTime() |
73
|
|
|
{ |
74
|
|
|
return $this->time; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param \DateTime $time |
79
|
|
|
* @return ShipmentEvent |
80
|
|
|
*/ |
81
|
|
|
public function setTime($time) |
82
|
|
|
{ |
83
|
|
|
$this->time = $time; |
84
|
|
|
if ($time) { |
85
|
|
|
$this->date = $time->format('Y-m-d H:i:s'); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getDescription() |
94
|
|
|
{ |
95
|
|
|
return $this->description; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $description |
100
|
|
|
* @return ShipmentEvent |
101
|
|
|
*/ |
102
|
|
|
public function setDescription($description) |
103
|
|
|
{ |
104
|
|
|
$this->description = $description; |
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return string|LocationInterface |
110
|
|
|
*/ |
111
|
|
|
public function getLocation() |
112
|
|
|
{ |
113
|
|
|
return $this->location; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string|LocationInterface $location |
118
|
|
|
* @return ShipmentEvent |
119
|
|
|
*/ |
120
|
|
|
public function setLocation($location) |
121
|
|
|
{ |
122
|
|
|
$this->location = $location; |
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function getStatus() |
130
|
|
|
{ |
131
|
|
|
return $this->status; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $status |
136
|
|
|
* @return ShipmentEvent |
137
|
|
|
*/ |
138
|
|
|
public function setStatus($status) |
139
|
|
|
{ |
140
|
|
|
$this->status = $status; |
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Creates an event from an array |
146
|
|
|
* @param array $data |
147
|
|
|
* @return static |
148
|
|
|
*/ |
149
|
|
|
public static function fromArray($data) |
150
|
|
|
{ |
151
|
|
|
$event = new static(); |
152
|
|
|
foreach ($data as $property => $value) { |
153
|
|
|
if (method_exists($event, $method = 'set' . ucfirst($property))) { |
154
|
|
|
$event->$method($value); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
return $event; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Converts the event to array |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function toArray() |
165
|
|
|
{ |
166
|
|
|
$methods = get_class_methods($this); |
167
|
|
|
$data = []; |
168
|
|
|
foreach ($methods as $method) { |
169
|
|
|
if (substr($method, 0, 3) == 'get') { |
170
|
|
|
$property = lcfirst(substr($method, 3)); |
171
|
|
|
$data[$property] = $this->$method(); |
172
|
|
|
} elseif (substr($method, 0, 2) == 'is') { |
173
|
|
|
$property = lcfirst(substr($method, 2)); |
174
|
|
|
$data[$property] = $this->$method(); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
return $data; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
|
|
public function jsonSerialize() |
184
|
|
|
{ |
185
|
|
|
return $this->toArray(); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):