These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 NFePHP\DA\Legacy\FilesFolders; |
||
17 | use InvalidArgumentException; |
||
18 | |||
19 | class Dom extends DOMDocument |
||
20 | { |
||
21 | /** |
||
22 | * __construct |
||
23 | * @param string $version |
||
24 | * @param string $charset |
||
25 | */ |
||
26 | public function __construct($version = '1.0', $charset = 'utf-8') |
||
27 | { |
||
28 | parent::__construct($version, $charset); |
||
29 | $this->formatOutput = false; |
||
30 | $this->preserveWhiteSpace = false; |
||
31 | } |
||
32 | public function loadXMLString($xmlString = '') |
||
33 | { |
||
34 | $msg = "O arquivo indicado não é um XML ou contêm B.O.M. no inicio do arquivo !"; |
||
35 | if (substr($xmlString, 0, 1) != '<') { |
||
36 | throw new InvalidArgumentException($msg); |
||
37 | } |
||
38 | if (! $this->loadXML($xmlString, LIBXML_NOBLANKS | LIBXML_NOEMPTYTAG)) { |
||
39 | throw new InvalidArgumentException($msg); |
||
40 | } |
||
41 | } |
||
42 | |||
43 | public function loadXMLFile($pathXmlFile = '') |
||
44 | { |
||
45 | $data = FilesFolders::readFile($pathXmlFile); |
||
46 | $this->loadXMLString($data); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * getNodeValue |
||
51 | * Extrai o valor do node DOM |
||
52 | * @param string $nodeName identificador da TAG do xml |
||
53 | * @param int $itemNum numero do item a ser retornado |
||
54 | * @param string $extraTextBefore prefixo do retorno |
||
55 | * @param string $extraTextAfter sufixo do retorno |
||
56 | * @return string |
||
57 | */ |
||
58 | public function getNodeValue($nodeName, $itemNum = 0, $extraTextBefore = '', $extraTextAfter = '') |
||
59 | { |
||
60 | $node = $this->getElementsByTagName($nodeName)->item($itemNum); |
||
61 | if (isset($node)) { |
||
62 | $texto = html_entity_decode(trim($node->nodeValue), ENT_QUOTES, 'UTF-8'); |
||
63 | return $extraTextBefore . $texto . $extraTextAfter; |
||
64 | } |
||
65 | return ''; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * getValue |
||
70 | * @param DOMElement $node |
||
71 | * @param string $name |
||
72 | * @return string |
||
73 | */ |
||
74 | public function getValue($node, $name) |
||
75 | { |
||
76 | if (empty($node)) { |
||
77 | return ''; |
||
78 | } |
||
79 | $texto = ! empty($node->getElementsByTagName($name)->item(0)->nodeValue) ? |
||
80 | $node->getElementsByTagName($name)->item(0)->nodeValue : ''; |
||
81 | return html_entity_decode($texto, ENT_QUOTES, 'UTF-8'); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * getNode |
||
86 | * Retorna o node solicitado |
||
87 | * @param string $nodeName |
||
88 | * @param integer $itemNum |
||
89 | * @return DOMElement se existir ou string vazia se não |
||
90 | */ |
||
91 | public function getNode($nodeName, $itemNum = 0) |
||
92 | { |
||
93 | $node = $this->getElementsByTagName($nodeName)->item($itemNum); |
||
94 | if (isset($node)) { |
||
95 | return $node; |
||
96 | } |
||
97 | return ''; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * getChave |
||
102 | * @param string $nodeName |
||
103 | * @return string |
||
104 | */ |
||
105 | public function getChave($nodeName = 'infNFe') |
||
106 | { |
||
107 | $node = $this->getElementsByTagName($nodeName)->item(0); |
||
108 | if (! empty($node)) { |
||
109 | $chaveId = $node->getAttribute("Id"); |
||
110 | $chave = preg_replace('/[^0-9]/', '', $chaveId); |
||
111 | return $chave; |
||
112 | } |
||
113 | return ''; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * addChild |
||
118 | * Adiciona um elemento ao node xml passado como referencia |
||
119 | * Serão inclusos erros na array $erros[] sempre que a tag for obrigatória e |
||
120 | * nenhum parâmetro for passado na variável $content e $force for false |
||
121 | * @param \DOMElement $parent |
||
122 | * @param string $name |
||
123 | * @param string $content |
||
124 | * @param boolean $obrigatorio |
||
125 | * @param string $descricao |
||
126 | * @param boolean $force força a criação do elemento mesmo sem dados e não considera como erro |
||
127 | * @return void |
||
128 | */ |
||
129 | public function addChild(&$parent, $name, $content = '', $obrigatorio = false, $descricao = "", $force = false) |
||
130 | { |
||
131 | $content = trim($content); |
||
132 | if ($obrigatorio && $content === '' && !$force) { |
||
133 | $this->erros[] = array( |
||
0 ignored issues
–
show
|
|||
134 | "tag" => $name, |
||
135 | "desc" => $descricao, |
||
136 | "erro" => "Preenchimento Obrigatório!" |
||
137 | ); |
||
138 | } |
||
139 | if ($obrigatorio || $content !== '' || $force) { |
||
140 | $content = htmlspecialchars($content, ENT_QUOTES); |
||
141 | $temp = $this->createElement($name, $content); |
||
142 | $parent->appendChild($temp); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * appChild |
||
148 | * Acrescenta DOMElement a pai DOMElement |
||
149 | * Caso o pai esteja vazio retorna uma exception com a mensagem |
||
150 | * O parametro "child" pode ser vazio |
||
151 | * @param \DOMNode $parent |
||
152 | * @param \DOMNode $child |
||
153 | * @param string $msg |
||
154 | * @return void |
||
155 | * @throws Exception\InvalidArgumentException |
||
156 | */ |
||
157 | public function appChild(&$parent, $child, $msg = '') |
||
158 | { |
||
159 | if (empty($parent)) { |
||
160 | throw new Exception\InvalidArgumentException($msg); |
||
161 | } |
||
162 | if (!empty($child)) { |
||
163 | $parent->appendChild($child); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * appChildBefore |
||
169 | * Acrescenta DOMElement a pai DOMElement |
||
170 | * Caso o pai esteja vazio retorna uma exception com a mensagem |
||
171 | * O parametro "child" pode ser vazio |
||
172 | * @param \DOMNode $parent |
||
173 | * @param \DOMNode $child |
||
174 | * @param string $before |
||
175 | * @param string $msg |
||
176 | * @return void |
||
177 | * @throws Exception\InvalidArgumentException |
||
178 | */ |
||
179 | public function appChildBefore(&$parent, $child, $before, $msg = '') |
||
180 | { |
||
181 | if (empty($parent)) { |
||
182 | throw new Exception\InvalidArgumentException($msg); |
||
183 | } |
||
184 | $bnode = $parent->getElementsByTagName($before)->item(0); |
||
185 | if (!empty($child) && !empty($bnode)) { |
||
186 | $parent->insertBefore($child, $bnode); |
||
187 | } |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * addArrayChild |
||
192 | * Adiciona a um DOMNode parent, outros elementos passados em um array de DOMElements |
||
193 | * @param DOMElement $parent |
||
194 | * @param array $arr |
||
195 | * @return int |
||
196 | */ |
||
197 | public function addArrayChild(&$parent, $arr) |
||
198 | { |
||
199 | $num = 0; |
||
200 | if (! empty($arr) && ! empty($parent)) { |
||
201 | foreach ($arr as $node) { |
||
202 | $this->appChild($parent, $node, ''); |
||
203 | $num++; |
||
204 | } |
||
205 | } |
||
206 | return $num; |
||
207 | } |
||
208 | } |
||
209 |
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: