1
|
|
|
<?php |
2
|
|
|
namespace MrPrompt\Celesc\Received; |
3
|
|
|
|
4
|
|
|
use MrPrompt\ShipmentCommon\Base\Sequence; |
5
|
|
|
use MrPrompt\ShipmentCommon\Base\Cart; |
6
|
|
|
use MrPrompt\Celesc\Received\Partial\Detail; |
7
|
|
|
use MrPrompt\Celesc\Received\Partial\Footer; |
8
|
|
|
use MrPrompt\Celesc\Received\Partial\Header; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Received file class |
12
|
|
|
* |
13
|
|
|
* @author Thiago Paes <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class File |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* File name template |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
const TEMPLATE_GENERATED = 'r{CLIENT}{SEQUENCE}.{SUBTYPE}'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Header |
26
|
|
|
*/ |
27
|
|
|
private $header; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ArrayObject |
|
|
|
|
31
|
|
|
*/ |
32
|
|
|
private $cart; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Footer |
36
|
|
|
*/ |
37
|
|
|
private $footer; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $rows = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var Sequence |
46
|
|
|
*/ |
47
|
|
|
private $sequence; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Sequence $sequence |
51
|
|
|
* @param string $storageDir |
52
|
|
|
*/ |
53
|
1 |
|
public function __construct(Sequence $sequence, $storageDir = null) |
54
|
|
|
{ |
55
|
1 |
|
$this->sequence = $sequence; |
56
|
|
|
|
57
|
1 |
|
$file = $storageDir . DIRECTORY_SEPARATOR . $this->createFilename(); |
58
|
1 |
|
$content = file_get_contents($file); |
59
|
|
|
|
60
|
1 |
|
$this->rows = explode(PHP_EOL, $content); |
61
|
|
|
|
62
|
1 |
|
$this->getHeader(); |
63
|
1 |
|
$this->getFooter(); |
64
|
1 |
|
$this->getCart(); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Create the file name |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
1 |
|
private function createFilename() |
73
|
|
|
{ |
74
|
1 |
|
$type = self::TEMPLATE_GENERATED; |
75
|
|
|
|
76
|
|
|
$search = [ |
77
|
1 |
|
'{CLIENT}', |
78
|
|
|
'{SEQUENCE}', |
79
|
|
|
'{SUBTYPE}' |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
$replace = [ |
83
|
1 |
|
'CEL', |
84
|
1 |
|
$this->sequence->getValue(), |
85
|
1 |
|
'063', |
86
|
|
|
]; |
87
|
|
|
|
88
|
1 |
|
return str_replace($search, $replace, $type); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return Header |
93
|
|
|
*/ |
94
|
1 |
|
public function getHeader() |
95
|
|
|
{ |
96
|
1 |
|
$headerLine = array_shift($this->rows); |
97
|
1 |
|
$this->header = new Header($headerLine); |
98
|
|
|
|
99
|
1 |
|
return $this->header; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return Footer |
104
|
|
|
*/ |
105
|
1 |
|
public function getFooter() |
106
|
|
|
{ |
107
|
1 |
|
$footerLine = array_pop($this->rows); |
108
|
|
|
|
109
|
1 |
|
if (null === $footerLine || "" === $footerLine) { |
110
|
1 |
|
$footerLine = array_pop($this->rows); |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
$this->footer = new Footer($footerLine); |
114
|
|
|
|
115
|
1 |
|
return $this->footer; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return \ArrayObject |
120
|
|
|
*/ |
121
|
1 |
|
public function getCart() |
122
|
|
|
{ |
123
|
1 |
|
$this->cart = new Cart(); |
|
|
|
|
124
|
|
|
|
125
|
|
|
/* @var $detail \MrPrompt\Celesc\Received\Partial\Detail */ |
126
|
1 |
|
foreach ($this->rows as $row) { |
127
|
1 |
|
$detail = new Detail($row); |
128
|
|
|
|
129
|
1 |
|
$this->cart->addItem($detail); |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
return $this->cart; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return mixed |
137
|
|
|
*/ |
138
|
1 |
|
public function getAll() |
139
|
|
|
{ |
140
|
|
|
return [ |
141
|
1 |
|
$this->header, |
142
|
1 |
|
$this->cart, |
143
|
1 |
|
$this->footer |
144
|
|
|
]; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|