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
|
|
|
/** |
34
|
|
|
* Informação de endereço que será informado nos clientes e no emitente |
35
|
|
|
*/ |
36
|
|
|
class Endereco implements Node |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
private $pais; |
40
|
|
|
private $cep; |
41
|
|
|
private $municipio; |
42
|
|
|
private $bairro; |
43
|
|
|
private $logradouro; |
44
|
|
|
private $numero; |
45
|
|
|
private $complemento; |
46
|
|
|
|
47
|
89 |
|
public function __construct($endereco = array()) |
48
|
|
|
{ |
49
|
89 |
|
$this->fromArray($endereco); |
50
|
89 |
|
} |
51
|
|
|
|
52
|
34 |
|
public function getPais() |
53
|
|
|
{ |
54
|
34 |
|
return $this->pais; |
55
|
|
|
} |
56
|
|
|
|
57
|
89 |
|
public function setPais($pais) |
58
|
|
|
{ |
59
|
89 |
|
$this->pais = $pais; |
60
|
89 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
34 |
|
public function getCEP($normalize = false) |
64
|
|
|
{ |
65
|
34 |
|
if (!$normalize) { |
66
|
8 |
|
return $this->cep; |
67
|
|
|
} |
68
|
33 |
|
return $this->cep; |
69
|
|
|
} |
70
|
|
|
|
71
|
89 |
|
public function setCEP($cep) |
72
|
|
|
{ |
73
|
89 |
|
$this->cep = $cep; |
74
|
89 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
57 |
|
public function getMunicipio() |
78
|
|
|
{ |
79
|
57 |
|
return $this->municipio; |
80
|
|
|
} |
81
|
|
|
|
82
|
89 |
|
public function setMunicipio($municipio) |
83
|
|
|
{ |
84
|
89 |
|
$this->municipio = $municipio; |
85
|
89 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
41 |
|
public function getBairro($normalize = false) |
89
|
|
|
{ |
90
|
41 |
|
if (!$normalize) { |
91
|
15 |
|
return $this->bairro; |
92
|
|
|
} |
93
|
33 |
|
return $this->bairro; |
94
|
|
|
} |
95
|
|
|
|
96
|
89 |
|
public function setBairro($bairro) |
97
|
|
|
{ |
98
|
89 |
|
$this->bairro = $bairro; |
99
|
89 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
41 |
|
public function getLogradouro($normalize = false) |
103
|
|
|
{ |
104
|
41 |
|
if (!$normalize) { |
105
|
15 |
|
return $this->logradouro; |
106
|
|
|
} |
107
|
33 |
|
return $this->logradouro; |
108
|
|
|
} |
109
|
|
|
|
110
|
89 |
|
public function setLogradouro($logradouro) |
111
|
|
|
{ |
112
|
89 |
|
$this->logradouro = $logradouro; |
113
|
89 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
41 |
|
public function getNumero($normalize = false) |
117
|
|
|
{ |
118
|
41 |
|
if (!$normalize) { |
119
|
15 |
|
return $this->numero; |
120
|
|
|
} |
121
|
33 |
|
return $this->numero; |
122
|
|
|
} |
123
|
|
|
|
124
|
89 |
|
public function setNumero($numero) |
125
|
|
|
{ |
126
|
89 |
|
$this->numero = $numero; |
127
|
89 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
34 |
|
public function getComplemento($normalize = false) |
131
|
|
|
{ |
132
|
34 |
|
if (!$normalize) { |
133
|
34 |
|
return $this->complemento; |
134
|
|
|
} |
135
|
2 |
|
return $this->complemento; |
136
|
|
|
} |
137
|
|
|
|
138
|
89 |
|
public function setComplemento($complemento) |
139
|
|
|
{ |
140
|
89 |
|
$this->complemento = $complemento; |
141
|
89 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Obtém as informações básicas do endereço em uma linha de texto |
146
|
|
|
* @param boolean $normalize informa se o valor deve ser normalizado para um XML |
147
|
|
|
* @return string endereço com logradouro, número e bairro |
148
|
|
|
*/ |
149
|
8 |
|
public function getDescricao($normalize = false) |
|
|
|
|
150
|
|
|
{ |
151
|
8 |
|
return $this->getLogradouro().', '.$this->getNumero().' - '.$this->getBairro(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Desmembra a descrição e salva as informações do endereço em seu respectivo campo |
156
|
|
|
* @param string $descricao linha de endereço com diversas informações |
157
|
|
|
* @return Endereco retorna a própria instância |
158
|
|
|
*/ |
159
|
4 |
|
public function parseDescricao($descricao) |
160
|
|
|
{ |
161
|
4 |
|
$pattern = '/(.*), (.*) - (.*)/'; |
162
|
4 |
|
if (!preg_match($pattern, $descricao, $matches)) { |
163
|
|
|
throw new \Exception('Não foi possível desmembrar a linha de endereço', 500); |
164
|
|
|
} |
165
|
4 |
|
$this->setLogradouro($matches[1]); |
166
|
4 |
|
$this->setNumero($matches[2]); |
167
|
4 |
|
$this->setBairro($matches[3]); |
168
|
4 |
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
8 |
|
public function toArray($recursive = false) |
172
|
|
|
{ |
173
|
8 |
|
$endereco = array(); |
174
|
8 |
|
if (!is_null($this->getPais()) && $recursive) { |
175
|
1 |
|
$endereco['pais'] = $this->getPais()->toArray($recursive); |
176
|
1 |
|
} else { |
177
|
7 |
|
$endereco['pais'] = $this->getPais(); |
178
|
|
|
} |
179
|
8 |
|
$endereco['cep'] = $this->getCEP(); |
180
|
8 |
|
if (!is_null($this->getMunicipio()) && $recursive) { |
181
|
1 |
|
$endereco['municipio'] = $this->getMunicipio()->toArray($recursive); |
182
|
1 |
|
} else { |
183
|
7 |
|
$endereco['municipio'] = $this->getMunicipio(); |
184
|
|
|
} |
185
|
8 |
|
$endereco['bairro'] = $this->getBairro(); |
186
|
8 |
|
$endereco['logradouro'] = $this->getLogradouro(); |
187
|
8 |
|
$endereco['numero'] = $this->getNumero(); |
188
|
8 |
|
$endereco['complemento'] = $this->getComplemento(); |
189
|
8 |
|
return $endereco; |
190
|
|
|
} |
191
|
|
|
|
192
|
89 |
|
public function fromArray($endereco = array()) |
|
|
|
|
193
|
|
|
{ |
194
|
89 |
|
if ($endereco instanceof Endereco) { |
195
|
7 |
|
$endereco = $endereco->toArray(); |
196
|
89 |
|
} elseif (!is_array($endereco)) { |
197
|
7 |
|
return $this; |
198
|
|
|
} |
199
|
89 |
|
if (!isset($endereco['pais']) || is_null($endereco['pais'])) { |
200
|
89 |
|
$this->setPais(new Pais(array('codigo' => 1058, 'nome' => 'Brasil'))); |
201
|
89 |
|
} else { |
202
|
7 |
|
$this->setPais($endereco['pais']); |
203
|
|
|
} |
204
|
89 |
|
if (isset($endereco['cep'])) { |
205
|
7 |
|
$this->setCEP($endereco['cep']); |
206
|
7 |
|
} else { |
207
|
89 |
|
$this->setCEP(null); |
208
|
|
|
} |
209
|
89 |
|
if (!isset($endereco['municipio']) || is_null($endereco['municipio'])) { |
210
|
89 |
|
$this->setMunicipio(new Municipio()); |
211
|
89 |
|
} else { |
212
|
7 |
|
$this->setMunicipio($endereco['municipio']); |
213
|
|
|
} |
214
|
89 |
|
if (isset($endereco['bairro'])) { |
215
|
7 |
|
$this->setBairro($endereco['bairro']); |
216
|
7 |
|
} else { |
217
|
89 |
|
$this->setBairro(null); |
218
|
|
|
} |
219
|
89 |
|
if (isset($endereco['logradouro'])) { |
220
|
7 |
|
$this->setLogradouro($endereco['logradouro']); |
221
|
7 |
|
} else { |
222
|
89 |
|
$this->setLogradouro(null); |
223
|
|
|
} |
224
|
89 |
|
if (isset($endereco['numero'])) { |
225
|
7 |
|
$this->setNumero($endereco['numero']); |
226
|
7 |
|
} else { |
227
|
89 |
|
$this->setNumero(null); |
228
|
|
|
} |
229
|
89 |
|
if (isset($endereco['complemento'])) { |
230
|
1 |
|
$this->setComplemento($endereco['complemento']); |
231
|
1 |
|
} else { |
232
|
89 |
|
$this->setComplemento(null); |
233
|
|
|
} |
234
|
89 |
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
33 |
|
public function checkCodigos() |
238
|
|
|
{ |
239
|
33 |
|
$this->getMunicipio()->checkCodigos(); |
240
|
33 |
|
$this->getMunicipio()->getEstado()->checkCodigos(); |
241
|
33 |
|
} |
242
|
|
|
|
243
|
33 |
|
public function getNode($name = null) |
244
|
|
|
{ |
245
|
33 |
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
246
|
33 |
|
$this->checkCodigos(); |
247
|
33 |
|
$element = $dom->createElement(is_null($name)?'enderEmit':$name); |
248
|
33 |
|
Util::appendNode($element, 'xLgr', $this->getLogradouro(true)); |
249
|
33 |
|
Util::appendNode($element, 'nro', $this->getNumero(true)); |
250
|
33 |
|
if (!is_null($this->getComplemento())) { |
251
|
2 |
|
Util::appendNode($element, 'xCpl', $this->getComplemento(true)); |
252
|
2 |
|
} |
253
|
33 |
|
Util::appendNode($element, 'xBairro', $this->getBairro(true)); |
254
|
33 |
|
Util::appendNode($element, 'cMun', $this->getMunicipio()->getCodigo(true)); |
255
|
33 |
|
Util::appendNode($element, 'xMun', $this->getMunicipio()->getNome(true)); |
256
|
33 |
|
Util::appendNode($element, 'UF', $this->getMunicipio()->getEstado()->getUF(true)); |
257
|
33 |
|
Util::appendNode($element, 'CEP', $this->getCEP(true)); |
258
|
33 |
|
Util::appendNode($element, 'cPais', $this->getPais()->getCodigo(true)); |
259
|
33 |
|
Util::appendNode($element, 'xPais', $this->getPais()->getNome(true)); |
260
|
|
|
// Util::appendNode($element, 'fone', $this->getTelefone(true)); |
261
|
33 |
|
return $element; |
262
|
|
|
} |
263
|
|
|
|
264
|
27 |
|
public function loadNode($element, $name = null) |
265
|
|
|
{ |
266
|
27 |
|
$name = is_null($name)?'enderEmit':$name; |
267
|
27 |
|
if ($element->nodeName != $name) { |
268
|
|
|
$_fields = $element->getElementsByTagName($name); |
269
|
|
|
if ($_fields->length == 0) { |
270
|
|
|
throw new \Exception('Tag "'.$name.'" não encontrada', 404); |
271
|
|
|
} |
272
|
|
|
$element = $_fields->item(0); |
273
|
|
|
} |
274
|
27 |
|
$this->setLogradouro( |
275
|
27 |
|
Util::loadNode( |
276
|
27 |
|
$element, |
277
|
27 |
|
'xLgr', |
278
|
|
|
'Tag "xLgr" do campo "Logradouro" não encontrada' |
279
|
27 |
|
) |
280
|
27 |
|
); |
281
|
27 |
|
$this->setNumero( |
282
|
27 |
|
Util::loadNode( |
283
|
27 |
|
$element, |
284
|
27 |
|
'nro', |
285
|
|
|
'Tag "nro" do campo "Numero" não encontrada' |
286
|
27 |
|
) |
287
|
27 |
|
); |
288
|
27 |
|
$this->setComplemento(Util::loadNode($element, 'xCpl')); |
289
|
27 |
|
$this->setBairro( |
290
|
27 |
|
Util::loadNode( |
291
|
27 |
|
$element, |
292
|
27 |
|
'xBairro', |
293
|
|
|
'Tag "xBairro" do campo "Bairro" não encontrada' |
294
|
27 |
|
) |
295
|
27 |
|
); |
296
|
27 |
|
$this->getMunicipio()->setCodigo( |
297
|
27 |
|
Util::loadNode( |
298
|
27 |
|
$element, |
299
|
27 |
|
'cMun', |
300
|
|
|
'Tag "cMun" do objeto "Municipio" não encontrada' |
301
|
27 |
|
) |
302
|
27 |
|
); |
303
|
27 |
|
$this->getMunicipio()->setNome( |
304
|
27 |
|
Util::loadNode( |
305
|
27 |
|
$element, |
306
|
27 |
|
'xMun', |
307
|
|
|
'Tag "xMun" do objeto "Municipio" não encontrada' |
308
|
27 |
|
) |
309
|
27 |
|
); |
310
|
27 |
|
$this->getMunicipio()->getEstado()->setUF( |
311
|
27 |
|
Util::loadNode( |
312
|
27 |
|
$element, |
313
|
27 |
|
'UF', |
314
|
|
|
'Tag "UF" do objeto "Estado" não encontrada' |
315
|
27 |
|
) |
316
|
27 |
|
); |
317
|
27 |
|
$this->setCEP( |
318
|
27 |
|
Util::loadNode( |
319
|
27 |
|
$element, |
320
|
27 |
|
'CEP', |
321
|
|
|
'Tag "CEP" do campo "CEP" não encontrada' |
322
|
27 |
|
) |
323
|
27 |
|
); |
324
|
27 |
|
$this->getPais()->setCodigo( |
325
|
27 |
|
Util::loadNode( |
326
|
27 |
|
$element, |
327
|
27 |
|
'cPais', |
328
|
|
|
'Tag "cPais" do objeto "Pais" não encontrada' |
329
|
27 |
|
) |
330
|
27 |
|
); |
331
|
27 |
|
$this->getPais()->setNome( |
332
|
27 |
|
Util::loadNode( |
333
|
27 |
|
$element, |
334
|
27 |
|
'xPais', |
335
|
|
|
'Tag "xPais" do objeto "Pais" não encontrada' |
336
|
27 |
|
) |
337
|
27 |
|
); |
338
|
27 |
|
return $element; |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.