|
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\Core\SEFAZ; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Município de um endereço |
|
34
|
|
|
*/ |
|
35
|
|
|
class Municipio |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
private $estado; |
|
39
|
|
|
private $codigo; |
|
40
|
|
|
private $nome; |
|
41
|
|
|
|
|
42
|
107 |
|
public function __construct($municipio = []) |
|
43
|
|
|
{ |
|
44
|
107 |
|
$this->fromArray($municipio); |
|
45
|
107 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Estado do município |
|
49
|
|
|
*/ |
|
50
|
84 |
|
public function getEstado() |
|
51
|
|
|
{ |
|
52
|
84 |
|
return $this->estado; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
107 |
|
public function setEstado($estado) |
|
56
|
|
|
{ |
|
57
|
107 |
|
$this->estado = $estado; |
|
58
|
107 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Código do município (utilizar a tabela do IBGE), informar 9999999 para |
|
63
|
|
|
* operações com o exterior. |
|
64
|
|
|
*/ |
|
65
|
52 |
|
public function getCodigo($normalize = false) |
|
66
|
|
|
{ |
|
67
|
52 |
|
if (!$normalize) { |
|
68
|
52 |
|
return $this->codigo; |
|
69
|
|
|
} |
|
70
|
48 |
|
return $this->codigo; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
107 |
|
public function setCodigo($codigo) |
|
74
|
|
|
{ |
|
75
|
107 |
|
$this->codigo = $codigo; |
|
76
|
107 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Nome do munícipio |
|
81
|
|
|
*/ |
|
82
|
52 |
|
public function getNome($normalize = false) |
|
83
|
|
|
{ |
|
84
|
52 |
|
if (!$normalize) { |
|
85
|
18 |
|
return $this->nome; |
|
86
|
|
|
} |
|
87
|
48 |
|
return $this->nome; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
107 |
|
public function setNome($nome) |
|
91
|
|
|
{ |
|
92
|
107 |
|
$this->nome = $nome; |
|
93
|
107 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Verifica se o código do municipio foi preenchido, |
|
98
|
|
|
* caso contrário realiza uma busca usando o nome e a UF e preenche |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
49 |
|
public function checkCodigos() |
|
102
|
|
|
{ |
|
103
|
49 |
|
if (is_numeric($this->getCodigo())) { |
|
104
|
43 |
|
return; |
|
105
|
|
|
} |
|
106
|
12 |
|
$db = SEFAZ::getInstance()->getConfiguracao()->getBanco(); |
|
|
|
|
|
|
107
|
12 |
|
$this->setCodigo($db->getCodigoMunicipio( |
|
108
|
12 |
|
$this->getNome(), |
|
109
|
12 |
|
$this->getEstado()->getUF() |
|
110
|
|
|
)); |
|
111
|
12 |
|
} |
|
112
|
|
|
|
|
113
|
18 |
|
public function toArray($recursive = false) |
|
114
|
|
|
{ |
|
115
|
18 |
|
$municipio = []; |
|
116
|
18 |
|
if (!is_null($this->getEstado()) && $recursive) { |
|
117
|
|
|
$municipio['estado'] = $this->getEstado()->toArray($recursive); |
|
118
|
|
|
} else { |
|
119
|
18 |
|
$municipio['estado'] = $this->getEstado(); |
|
120
|
|
|
} |
|
121
|
18 |
|
$municipio['codigo'] = $this->getCodigo(); |
|
122
|
18 |
|
$municipio['nome'] = $this->getNome(); |
|
123
|
18 |
|
return $municipio; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
107 |
|
public function fromArray($municipio = []) |
|
127
|
|
|
{ |
|
128
|
107 |
|
if ($municipio instanceof Municipio) { |
|
129
|
18 |
|
$municipio = $municipio->toArray(); |
|
130
|
107 |
|
} elseif (!is_array($municipio)) { |
|
131
|
1 |
|
return $this; |
|
132
|
|
|
} |
|
133
|
107 |
|
$this->setEstado(new Estado(isset($municipio['estado']) ? $municipio['estado'] : [])); |
|
134
|
107 |
|
if (isset($municipio['codigo'])) { |
|
135
|
8 |
|
$this->setCodigo($municipio['codigo']); |
|
136
|
|
|
} else { |
|
137
|
107 |
|
$this->setCodigo(null); |
|
138
|
|
|
} |
|
139
|
107 |
|
if (isset($municipio['nome'])) { |
|
140
|
17 |
|
$this->setNome($municipio['nome']); |
|
141
|
|
|
} else { |
|
142
|
107 |
|
$this->setNome(null); |
|
143
|
|
|
} |
|
144
|
107 |
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.