|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NFePHP\DA\Legacy; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Classe auxiliar com funções de DOM extendidas |
|
7
|
|
|
* @category NFePHP |
|
8
|
|
|
* @package NFePHP\DA\Legacy\Dom |
|
9
|
|
|
* @copyright Copyright (c) 2008-2015 |
|
10
|
|
|
* @license http://www.gnu.org/licenses/lesser.html LGPL v3 |
|
11
|
|
|
* @author Roberto L. Machado <linux.rlm at gmail dot com> |
|
12
|
|
|
* @link http://github.com/nfephp-org/nfephp for the canonical source repository |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
use \DOMDocument; |
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
|
|
18
|
|
|
class Dom extends DOMDocument |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* __construct |
|
22
|
|
|
* @param string $version |
|
23
|
|
|
* @param string $charset |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($version = '1.0', $charset = 'utf-8') |
|
26
|
|
|
{ |
|
27
|
|
|
parent::__construct($version, $charset); |
|
28
|
|
|
$this->formatOutput = false; |
|
29
|
|
|
$this->preserveWhiteSpace = false; |
|
30
|
|
|
} |
|
31
|
|
|
public function loadXMLString($xmlString = '') |
|
32
|
|
|
{ |
|
33
|
|
|
$msg = "O arquivo indicado não é um XML ou contêm B.O.M. no inicio do arquivo !"; |
|
34
|
|
|
if (substr($xmlString, 0, 1) != '<') { |
|
35
|
|
|
throw new InvalidArgumentException($msg); |
|
36
|
|
|
} |
|
37
|
|
|
if (! $this->loadXML($xmlString, LIBXML_NOBLANKS | LIBXML_NOEMPTYTAG)) { |
|
38
|
|
|
throw new InvalidArgumentException($msg); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* getNodeValue |
|
44
|
|
|
* Extrai o valor do node DOM |
|
45
|
|
|
* @param string $nodeName identificador da TAG do xml |
|
46
|
|
|
* @param int $itemNum numero do item a ser retornado |
|
47
|
|
|
* @param string $extraTextBefore prefixo do retorno |
|
48
|
|
|
* @param string $extraTextAfter sufixo do retorno |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getNodeValue($nodeName, $itemNum = 0, $extraTextBefore = '', $extraTextAfter = '') |
|
52
|
|
|
{ |
|
53
|
|
|
$node = $this->getElementsByTagName($nodeName)->item($itemNum); |
|
54
|
|
|
if (isset($node)) { |
|
55
|
|
|
$texto = html_entity_decode(trim($node->nodeValue), ENT_QUOTES, 'UTF-8'); |
|
56
|
|
|
return $extraTextBefore . $texto . $extraTextAfter; |
|
57
|
|
|
} |
|
58
|
|
|
return ''; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* getValue |
|
63
|
|
|
* @param DOMElement $node |
|
64
|
|
|
* @param string $name |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getValue($node, $name) |
|
68
|
|
|
{ |
|
69
|
|
|
if (empty($node)) { |
|
70
|
|
|
return ''; |
|
71
|
|
|
} |
|
72
|
|
|
$texto = ! empty($node->getElementsByTagName($name)->item(0)->nodeValue) ? |
|
73
|
|
|
$node->getElementsByTagName($name)->item(0)->nodeValue : ''; |
|
74
|
|
|
return html_entity_decode($texto, ENT_QUOTES, 'UTF-8'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* getNode |
|
79
|
|
|
* Retorna o node solicitado |
|
80
|
|
|
* @param string $nodeName |
|
81
|
|
|
* @param integer $itemNum |
|
82
|
|
|
* @return DOMElement se existir ou string vazia se não |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getNode($nodeName, $itemNum = 0) |
|
85
|
|
|
{ |
|
86
|
|
|
$node = $this->getElementsByTagName($nodeName)->item($itemNum); |
|
87
|
|
|
if (isset($node)) { |
|
88
|
|
|
return $node; |
|
89
|
|
|
} |
|
90
|
|
|
return ''; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* getChave |
|
95
|
|
|
* @param string $nodeName |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getChave($nodeName = 'infNFe') |
|
99
|
|
|
{ |
|
100
|
|
|
$node = $this->getElementsByTagName($nodeName)->item(0); |
|
101
|
|
|
if (! empty($node)) { |
|
102
|
|
|
$chaveId = $node->getAttribute("Id"); |
|
103
|
|
|
$chave = preg_replace('/[^0-9]/', '', $chaveId); |
|
104
|
|
|
return $chave; |
|
105
|
|
|
} |
|
106
|
|
|
return ''; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* addChild |
|
111
|
|
|
* Adiciona um elemento ao node xml passado como referencia |
|
112
|
|
|
* Serão inclusos erros na array $erros[] sempre que a tag for obrigatória e |
|
113
|
|
|
* nenhum parâmetro for passado na variável $content e $force for false |
|
114
|
|
|
* @param \DOMElement $parent |
|
115
|
|
|
* @param string $name |
|
116
|
|
|
* @param string $content |
|
117
|
|
|
* @param boolean $obrigatorio |
|
118
|
|
|
* @param string $descricao |
|
119
|
|
|
* @param boolean $force força a criação do elemento mesmo sem dados e não considera como erro |
|
120
|
|
|
* @return void |
|
121
|
|
|
*/ |
|
122
|
|
|
public function addChild(&$parent, $name, $content = '', $obrigatorio = false, $descricao = "", $force = false) |
|
123
|
|
|
{ |
|
124
|
|
|
$content = trim($content); |
|
125
|
|
|
if ($obrigatorio && $content === '' && !$force) { |
|
126
|
|
|
$this->erros[] = array( |
|
|
|
|
|
|
127
|
|
|
"tag" => $name, |
|
128
|
|
|
"desc" => $descricao, |
|
129
|
|
|
"erro" => "Preenchimento Obrigatório!" |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
if ($obrigatorio || $content !== '' || $force) { |
|
133
|
|
|
$content = htmlspecialchars($content, ENT_QUOTES); |
|
134
|
|
|
$temp = $this->createElement($name, $content); |
|
135
|
|
|
$parent->appendChild($temp); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* appChild |
|
141
|
|
|
* Acrescenta DOMElement a pai DOMElement |
|
142
|
|
|
* Caso o pai esteja vazio retorna uma exception com a mensagem |
|
143
|
|
|
* O parametro "child" pode ser vazio |
|
144
|
|
|
* @param \DOMNode $parent |
|
145
|
|
|
* @param \DOMNode $child |
|
146
|
|
|
* @param string $msg |
|
147
|
|
|
* @return void |
|
148
|
|
|
* @throws Exception\InvalidArgumentException |
|
149
|
|
|
*/ |
|
150
|
|
|
public function appChild(&$parent, $child, $msg = '') |
|
151
|
|
|
{ |
|
152
|
|
|
if (empty($parent)) { |
|
153
|
|
|
throw new Exception\InvalidArgumentException($msg); |
|
154
|
|
|
} |
|
155
|
|
|
if (!empty($child)) { |
|
156
|
|
|
$parent->appendChild($child); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* appChildBefore |
|
162
|
|
|
* Acrescenta DOMElement a pai DOMElement |
|
163
|
|
|
* Caso o pai esteja vazio retorna uma exception com a mensagem |
|
164
|
|
|
* O parametro "child" pode ser vazio |
|
165
|
|
|
* @param \DOMNode $parent |
|
166
|
|
|
* @param \DOMNode $child |
|
167
|
|
|
* @param string $before |
|
168
|
|
|
* @param string $msg |
|
169
|
|
|
* @return void |
|
170
|
|
|
* @throws Exception\InvalidArgumentException |
|
171
|
|
|
*/ |
|
172
|
|
|
public function appChildBefore(&$parent, $child, $before, $msg = '') |
|
173
|
|
|
{ |
|
174
|
|
|
if (empty($parent)) { |
|
175
|
|
|
throw new Exception\InvalidArgumentException($msg); |
|
176
|
|
|
} |
|
177
|
|
|
$bnode = $parent->getElementsByTagName($before)->item(0); |
|
178
|
|
|
if (!empty($child) && !empty($bnode)) { |
|
179
|
|
|
$parent->insertBefore($child, $bnode); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* addArrayChild |
|
185
|
|
|
* Adiciona a um DOMNode parent, outros elementos passados em um array de DOMElements |
|
186
|
|
|
* @param DOMElement $parent |
|
187
|
|
|
* @param array $arr |
|
188
|
|
|
* @return int |
|
189
|
|
|
*/ |
|
190
|
|
|
public function addArrayChild(&$parent, $arr) |
|
191
|
|
|
{ |
|
192
|
|
|
$num = 0; |
|
193
|
|
|
if (! empty($arr) && ! empty($parent)) { |
|
194
|
|
|
foreach ($arr as $node) { |
|
195
|
|
|
$this->appChild($parent, $node, ''); |
|
196
|
|
|
$num++; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
return $num; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: