1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace floor12\DalliApi; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use floor12\DalliApi\Models\Item; |
8
|
|
|
|
9
|
|
|
class OrderStatusDispatcher |
10
|
|
|
{ |
11
|
|
|
/** @var string */ |
12
|
|
|
private $xmlBody; |
13
|
|
|
/** @var int */ |
14
|
|
|
private $statusTimestamp; |
15
|
|
|
/** @var string */ |
16
|
|
|
private $statusName; |
17
|
|
|
/** @var string */ |
18
|
|
|
private $statusId; |
19
|
|
|
/** @var ?Item[] */ |
20
|
|
|
protected $items = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $xmlBody |
24
|
|
|
*/ |
25
|
|
|
public function __construct(string $xmlBody) |
26
|
|
|
{ |
27
|
|
|
$this->xmlBody = $xmlBody; |
28
|
|
|
$this->dispatchOrderStatus(); |
29
|
|
|
$this->dispatchItemStatus(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private function dispatchOrderStatus(): void |
33
|
|
|
{ |
34
|
|
|
$pattern = '/<status eventtime="(.+)" createtimegmt="(.+)" title="(.+)">(.+)<\/status/'; |
35
|
|
|
if (preg_match($pattern, $this->xmlBody, $matches)) { |
36
|
|
|
$this->statusId = $matches[4]; |
37
|
|
|
$this->statusName = $matches[3]; |
38
|
|
|
$this->statusTimestamp = strtotime($matches[1]); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function dispatchItemStatus() |
43
|
|
|
{ |
44
|
|
|
$pattern = '/<item.+barcode="(.+)" a.+returns="(\d)">/'; |
45
|
|
|
if (preg_match_all($pattern, $this->xmlBody, $matches)) { |
46
|
|
|
for ($i = 0; $i < sizeof($matches[0]); $i++) { |
|
|
|
|
47
|
|
|
$barcode = $matches[1][$i]; |
48
|
|
|
$return = boolval($matches[2][$i]); |
49
|
|
|
$this->items[$barcode] = (new Item()) |
50
|
|
|
->setBarcode($barcode) |
51
|
|
|
->setReturn($return); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string|int $barcode |
58
|
|
|
* @return bool|null |
59
|
|
|
*/ |
60
|
|
|
public function isItemReturned($barcode): ?bool |
61
|
|
|
{ |
62
|
|
|
return ($item = $this->items[$barcode]) ? $item->isReturned() : null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getStatusId(): string |
66
|
|
|
{ |
67
|
|
|
return $this->statusId; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getStatusName(): string |
71
|
|
|
{ |
72
|
|
|
return $this->statusName; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getStatusTimestamp(): int |
76
|
|
|
{ |
77
|
|
|
return $this->statusTimestamp; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return Item[] |
82
|
|
|
*/ |
83
|
|
|
public function getItems(): array |
84
|
|
|
{ |
85
|
|
|
return $this->items; |
|
|
|
|
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: