1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MIT License |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 MZ Desenvolvimento de Sistemas LTDA |
6
|
|
|
* |
7
|
|
|
* @author Francimar Alves <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
11
|
|
|
* in the Software without restriction, including without limitation the rights |
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
14
|
|
|
* furnished to do so, subject to the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
17
|
|
|
* copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25
|
|
|
* SOFTWARE. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
namespace NFe\Entity; |
29
|
|
|
|
30
|
|
|
use NFe\Common\Node; |
31
|
|
|
use NFe\Common\Util; |
32
|
|
|
|
33
|
|
|
class Volume implements Node |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
private $quantidade; |
37
|
|
|
private $especie; |
38
|
|
|
private $marca; |
39
|
|
|
private $numeracoes; |
40
|
|
|
private $peso; |
41
|
|
|
private $lacres; |
42
|
|
|
|
43
|
11 |
|
public function __construct($volume = array()) |
44
|
|
|
{ |
45
|
11 |
|
$this->fromArray($volume); |
46
|
11 |
|
} |
47
|
|
|
|
48
|
9 |
|
public function getQuantidade($normalize = false) |
49
|
|
|
{ |
50
|
9 |
|
if (!$normalize) { |
51
|
9 |
|
return $this->quantidade; |
52
|
|
|
} |
53
|
9 |
|
return $this->quantidade; |
54
|
|
|
} |
55
|
|
|
|
56
|
11 |
|
public function setQuantidade($quantidade) |
57
|
|
|
{ |
58
|
11 |
|
if (!is_null($quantidade)) { |
59
|
9 |
|
$quantidade = intval($quantidade); |
60
|
9 |
|
} |
61
|
11 |
|
$this->quantidade = $quantidade; |
62
|
11 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
9 |
|
public function getEspecie($normalize = false) |
66
|
|
|
{ |
67
|
9 |
|
if (!$normalize) { |
68
|
9 |
|
return $this->especie; |
69
|
|
|
} |
70
|
9 |
|
return $this->especie; |
71
|
|
|
} |
72
|
|
|
|
73
|
11 |
|
public function setEspecie($especie) |
74
|
|
|
{ |
75
|
11 |
|
$this->especie = $especie; |
76
|
11 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
9 |
|
public function getMarca($normalize = false) |
80
|
|
|
{ |
81
|
9 |
|
if (!$normalize) { |
82
|
9 |
|
return $this->marca; |
83
|
|
|
} |
84
|
9 |
|
return $this->marca; |
85
|
|
|
} |
86
|
|
|
|
87
|
11 |
|
public function setMarca($marca) |
88
|
|
|
{ |
89
|
11 |
|
$this->marca = $marca; |
90
|
11 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
9 |
|
public function getNumeracoes() |
94
|
|
|
{ |
95
|
9 |
|
return $this->numeracoes; |
96
|
|
|
} |
97
|
|
|
|
98
|
11 |
|
public function setNumeracoes($numeracoes) |
99
|
|
|
{ |
100
|
11 |
|
$this->numeracoes = $numeracoes; |
101
|
11 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
4 |
|
public function addNumeracao($numeracao) |
105
|
|
|
{ |
106
|
4 |
|
$this->numeracoes[] = $numeracao; |
107
|
4 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
9 |
|
public function getPeso() |
111
|
|
|
{ |
112
|
9 |
|
return $this->peso; |
113
|
|
|
} |
114
|
|
|
|
115
|
11 |
|
public function setPeso($peso) |
116
|
|
|
{ |
117
|
11 |
|
$this->peso = $peso; |
118
|
11 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
9 |
|
public function getLacres() |
122
|
|
|
{ |
123
|
9 |
|
return $this->lacres; |
124
|
|
|
} |
125
|
|
|
|
126
|
11 |
|
public function setLacres($lacres) |
127
|
|
|
{ |
128
|
11 |
|
$this->lacres = $lacres; |
129
|
11 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
4 |
|
public function addLacre($lacre) |
133
|
|
|
{ |
134
|
4 |
|
$this->lacres[] = $lacre; |
135
|
4 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
public function toArray($recursive = false) |
139
|
|
|
{ |
140
|
1 |
|
$volume = array(); |
141
|
1 |
|
$volume['quantidade'] = $this->getQuantidade(); |
142
|
1 |
|
$volume['especie'] = $this->getEspecie(); |
143
|
1 |
|
$volume['marca'] = $this->getMarca(); |
144
|
1 |
|
$volume['numeracoes'] = $this->getNumeracoes(); |
145
|
1 |
|
if (!is_null($this->getPeso()) && $recursive) { |
146
|
|
|
$volume['peso'] = $this->getPeso()->toArray($recursive); |
147
|
|
|
} else { |
148
|
1 |
|
$volume['peso'] = $this->getPeso(); |
149
|
|
|
} |
150
|
1 |
|
if ($recursive) { |
151
|
|
|
$lacres = array(); |
152
|
|
|
$_lacres = $this->getLacres(); |
153
|
|
|
foreach ($_lacres as $_lacre) { |
154
|
|
|
$lacres[] = $_lacre->toArray($recursive); |
155
|
|
|
} |
156
|
|
|
$volume['lacres'] = $lacres; |
157
|
|
|
} else { |
158
|
1 |
|
$volume['lacres'] = $this->getLacres(); |
159
|
|
|
} |
160
|
1 |
|
return $volume; |
161
|
|
|
} |
162
|
|
|
|
163
|
11 |
|
public function fromArray($volume = array()) |
|
|
|
|
164
|
|
|
{ |
165
|
11 |
|
if ($volume instanceof Volume) { |
166
|
1 |
|
$volume = $volume->toArray(); |
167
|
11 |
|
} elseif (!is_array($volume)) { |
168
|
1 |
|
return $this; |
169
|
|
|
} |
170
|
11 |
|
if (isset($volume['quantidade'])) { |
171
|
1 |
|
$this->setQuantidade($volume['quantidade']); |
172
|
1 |
|
} else { |
173
|
11 |
|
$this->setQuantidade(null); |
174
|
|
|
} |
175
|
11 |
|
if (isset($volume['especie'])) { |
176
|
1 |
|
$this->setEspecie($volume['especie']); |
177
|
1 |
|
} else { |
178
|
11 |
|
$this->setEspecie(null); |
179
|
|
|
} |
180
|
11 |
|
if (isset($volume['marca'])) { |
181
|
1 |
|
$this->setMarca($volume['marca']); |
182
|
1 |
|
} else { |
183
|
11 |
|
$this->setMarca(null); |
184
|
|
|
} |
185
|
11 |
|
if (!isset($volume['numeracoes']) || is_null($volume['numeracoes'])) { |
186
|
11 |
|
$this->setNumeracoes(array()); |
187
|
11 |
|
} else { |
188
|
1 |
|
$this->setNumeracoes($volume['numeracoes']); |
189
|
|
|
} |
190
|
11 |
|
if (!isset($volume['peso']) || is_null($volume['peso'])) { |
191
|
11 |
|
$this->setPeso(new Peso()); |
192
|
11 |
|
} else { |
193
|
1 |
|
$this->setPeso($volume['peso']); |
194
|
|
|
} |
195
|
11 |
|
if (!isset($volume['lacres']) || is_null($volume['lacres'])) { |
196
|
11 |
|
$this->setLacres(array()); |
197
|
11 |
|
} else { |
198
|
1 |
|
$this->setLacres($volume['lacres']); |
199
|
|
|
} |
200
|
11 |
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
9 |
|
public function getNode($name = null) |
|
|
|
|
204
|
|
|
{ |
205
|
9 |
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
206
|
9 |
|
$element = $dom->createElement(is_null($name)?'vol':$name); |
207
|
9 |
|
if (!is_null($this->getQuantidade())) { |
208
|
9 |
|
Util::appendNode($element, 'qVol', $this->getQuantidade(true)); |
209
|
9 |
|
} |
210
|
9 |
|
if (!is_null($this->getEspecie())) { |
211
|
9 |
|
Util::appendNode($element, 'esp', $this->getEspecie(true)); |
212
|
9 |
|
} |
213
|
9 |
|
if (!is_null($this->getMarca())) { |
214
|
9 |
|
Util::appendNode($element, 'marca', $this->getMarca(true)); |
215
|
9 |
|
} |
216
|
9 |
|
$_numeracoes = $this->getNumeracoes(); |
217
|
9 |
|
if (!empty($_numeracoes)) { |
218
|
9 |
|
Util::appendNode($element, 'nVol', implode(', ', $_numeracoes)); |
219
|
9 |
|
} |
220
|
9 |
|
if (!is_null($this->getPeso())) { |
221
|
8 |
|
$peso = $this->getPeso(); |
222
|
8 |
|
Util::appendNode($element, 'pesoL', $peso->getLiquido(true)); |
223
|
8 |
|
Util::appendNode($element, 'pesoB', $peso->getBruto(true)); |
224
|
8 |
|
} |
225
|
9 |
|
$_lacres = $this->getLacres(); |
226
|
9 |
|
if (!empty($_lacres)) { |
227
|
9 |
|
foreach ($_lacres as $_lacre) { |
228
|
9 |
|
$lacre = $_lacre->getNode(); |
229
|
9 |
|
$lacre = $dom->importNode($lacre, true); |
230
|
9 |
|
$element->appendChild($lacre); |
231
|
9 |
|
} |
232
|
9 |
|
} |
233
|
9 |
|
return $element; |
234
|
|
|
} |
235
|
|
|
|
236
|
6 |
|
public function loadNode($element, $name = null) |
237
|
|
|
{ |
238
|
6 |
|
$name = is_null($name)?'vol':$name; |
239
|
6 |
|
if ($element->nodeName != $name) { |
240
|
2 |
|
$_fields = $element->getElementsByTagName($name); |
241
|
2 |
|
if ($_fields->length == 0) { |
242
|
1 |
|
throw new \Exception('Tag "'.$name.'" não encontrada', 404); |
243
|
|
|
} |
244
|
1 |
|
$element = $_fields->item(0); |
245
|
1 |
|
} |
246
|
5 |
|
$this->setQuantidade(Util::loadNode($element, 'qVol')); |
247
|
5 |
|
$this->setEspecie(Util::loadNode($element, 'esp')); |
248
|
5 |
|
$this->setMarca(Util::loadNode($element, 'marca')); |
249
|
5 |
|
$numeracoes = array(); |
250
|
5 |
|
$volumes = Util::loadNode($element, 'nVol'); |
251
|
5 |
|
if (trim($volumes) != '') { |
252
|
5 |
|
$numeracoes = explode(', ', $volumes); |
253
|
5 |
|
} |
254
|
5 |
|
$this->setNumeracoes($numeracoes); |
255
|
5 |
|
$peso = new Peso(); |
256
|
5 |
|
$peso->setLiquido(Util::loadNode($element, 'pesoL')); |
257
|
5 |
|
$peso->setBruto(Util::loadNode($element, 'pesoB')); |
258
|
5 |
|
if (is_null($peso->getLiquido()) || is_null($peso->getBruto())) { |
259
|
1 |
|
$peso = null; |
260
|
1 |
|
} |
261
|
5 |
|
$this->setPeso($peso); |
262
|
5 |
|
$lacres = array(); |
263
|
5 |
|
$_fields = $element->getElementsByTagName('lacres'); |
264
|
5 |
|
foreach ($_fields as $_item) { |
265
|
5 |
|
$lacre = new Lacre(); |
266
|
5 |
|
$lacre->loadNode($_item, 'lacres'); |
267
|
5 |
|
$lacres[] = $lacre; |
268
|
5 |
|
} |
269
|
5 |
|
$this->setLacres($lacres); |
270
|
5 |
|
return $element; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.
Some resources for further reading:
You can also find more detailed suggestions for refactoring in the “Code” section of your repository.