1
|
|
|
<?php |
2
|
|
|
namespace MrPrompt\Centercob\Received; |
3
|
|
|
|
4
|
|
|
use DateTime; |
5
|
|
|
use MrPrompt\ShipmentCommon\Base\Cart; |
6
|
|
|
use MrPrompt\ShipmentCommon\Base\Customer; |
7
|
|
|
use MrPrompt\ShipmentCommon\Util\Number; |
8
|
|
|
use MrPrompt\Centercob\Received\Partial\Detail; |
9
|
|
|
use MrPrompt\Centercob\Received\Partial\Footer; |
10
|
|
|
use MrPrompt\Centercob\Received\Partial\Header; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Received file class |
14
|
|
|
* |
15
|
|
|
* @author Thiago Paes <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class File |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* File name template |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
const TEMPLATE_GENERATED = 'RET{CLIENT}-{DD.MM.YYYY-HH.MM.SS}.TXT'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* File name template |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
const TEMPLATE_PROCESSED = 'RET{CLIENT}-{DD.MM.YYYY-HH.MM.SS}.TXT'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Header |
35
|
|
|
*/ |
36
|
|
|
private $header; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ArrayObject |
40
|
|
|
*/ |
41
|
|
|
private $cart; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Footer |
45
|
|
|
*/ |
46
|
|
|
private $footer; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
private $rows = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var Customer |
55
|
|
|
*/ |
56
|
|
|
private $customer; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var DateTime |
60
|
|
|
*/ |
61
|
|
|
private $now; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Customer $customer |
65
|
|
|
* @param DateTime $date |
66
|
|
|
* @param string $storageDir |
67
|
|
|
*/ |
68
|
1 |
|
public function __construct(Customer $customer, DateTime $date, $storageDir = null) |
69
|
|
|
{ |
70
|
1 |
|
$this->customer = $customer; |
71
|
1 |
|
$this->now = $date; |
72
|
|
|
|
73
|
1 |
|
$file = $storageDir . DIRECTORY_SEPARATOR . $this->createFilename(); |
74
|
1 |
|
$content = file_get_contents($file); |
75
|
|
|
|
76
|
1 |
|
$this->rows = explode(PHP_EOL, $content); |
77
|
|
|
|
78
|
1 |
|
$this->getHeader(); |
79
|
1 |
|
$this->getFooter(); |
80
|
1 |
|
$this->getCart(); |
81
|
1 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Create the file name |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
1 |
|
private function createFilename($type = self::TEMPLATE_GENERATED) |
89
|
|
|
{ |
90
|
|
|
$search = [ |
91
|
1 |
|
'{CLIENT}', |
92
|
|
|
'{DD.MM.YYYY-HH.MM.SS}', |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$replace = [ |
96
|
1 |
|
Number::zeroFill($this->customer->getCode(), 7, Number::FILL_LEFT), |
97
|
1 |
|
$this->now->format('d.m.Y-H.i.s'), |
98
|
|
|
]; |
99
|
|
|
|
100
|
1 |
|
return str_replace($search, $replace, $type); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return Header |
105
|
|
|
*/ |
106
|
1 |
|
public function getHeader() |
107
|
|
|
{ |
108
|
1 |
|
$headerLine = array_shift($this->rows); |
109
|
1 |
|
$this->header = new Header($headerLine); |
110
|
|
|
|
111
|
1 |
|
return $this->header; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return Footer |
116
|
|
|
*/ |
117
|
1 |
|
public function getFooter() |
118
|
|
|
{ |
119
|
1 |
|
$footerLine = array_pop($this->rows); |
120
|
|
|
|
121
|
1 |
|
if (null === $footerLine || "" === $footerLine) { |
122
|
1 |
|
$footerLine = array_pop($this->rows); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
$this->footer = new Footer($footerLine); |
126
|
|
|
|
127
|
1 |
|
return $this->footer; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return \ArrayObject |
132
|
|
|
*/ |
133
|
1 |
|
public function getCart() |
134
|
|
|
{ |
135
|
1 |
|
$this->cart = new Cart(); |
|
|
|
|
136
|
|
|
|
137
|
|
|
/* @var $detail \Centercob\Gateway\Received\Partial\Detail */ |
138
|
1 |
|
foreach ($this->rows as $row) { |
139
|
1 |
|
$detail = new Detail($row); |
140
|
|
|
|
141
|
1 |
|
$this->cart->addItem($detail); |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
return $this->cart; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return mixed |
149
|
|
|
*/ |
150
|
1 |
|
public function getAll() |
151
|
|
|
{ |
152
|
|
|
return [ |
153
|
1 |
|
$this->header, |
154
|
1 |
|
$this->cart, |
155
|
1 |
|
$this->footer |
156
|
|
|
]; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..