|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NFePHP\Common\Identify; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Classe auxiliar para a identificação dos documentos eletrônicos |
|
7
|
|
|
* @category NFePHP |
|
8
|
|
|
* @package NFePHP\Common\Identify |
|
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 NFePHP\Common\Dom\Dom; |
|
16
|
|
|
use NFePHP\Common\Files\FilesFolders; |
|
17
|
|
|
|
|
18
|
|
|
class Identify |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Lista com a identificação das TAGs principais que identificam o documento |
|
22
|
|
|
* e o respectivo arquivo xsd |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected static $schemesId = array(); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* setListSchemesId |
|
29
|
|
|
* @param array $aList |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public static function setListSchemesId($aList = array()) |
|
32
|
|
|
{ |
|
33
|
1 |
|
if (count($aList) > 0) { |
|
34
|
1 |
|
self::$schemesId = $aList; |
|
35
|
|
|
} |
|
36
|
1 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* identificacao |
|
40
|
|
|
* Identifica o documento |
|
41
|
|
|
* @param type $xml |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public static function identificacao($xml = '', &$aResp = array()) |
|
45
|
|
|
{ |
|
46
|
1 |
|
if ($xml == '') { |
|
47
|
|
|
return ''; |
|
48
|
1 |
|
} elseif (is_file($xml)) { |
|
49
|
1 |
|
$xml = FilesFolders::readFile($xml); |
|
50
|
|
|
} |
|
51
|
1 |
|
$dom = new Dom('1.0', 'utf-8'); |
|
52
|
1 |
|
$dom->loadXMLString($xml); |
|
53
|
1 |
|
$key = ''; |
|
54
|
1 |
|
$schId = (string) self::zSearchNode($dom, $key); |
|
|
|
|
|
|
55
|
1 |
|
if ($schId == '') { |
|
56
|
|
|
return ''; |
|
57
|
|
|
} |
|
58
|
1 |
|
$chave = ''; |
|
59
|
1 |
|
$tpAmb = ''; |
|
60
|
1 |
|
$dhEmi = ''; |
|
61
|
1 |
|
if ($schId == 'nfe' || $schId == 'cte' || $schId == 'mdfe') { |
|
62
|
|
|
switch ($schId) { |
|
63
|
1 |
|
case 'nfe': |
|
64
|
1 |
|
$tag = 'infNFe'; |
|
65
|
1 |
|
break; |
|
66
|
|
|
case 'cte': |
|
67
|
|
|
$tag = 'infCTe'; |
|
68
|
|
|
break; |
|
69
|
|
|
case 'mdfe': |
|
70
|
|
|
$tag = 'infMDFe'; |
|
71
|
|
|
break; |
|
72
|
|
|
} |
|
73
|
1 |
|
$chave = $dom->getChave($tag); |
|
|
|
|
|
|
74
|
1 |
|
$tpAmb = $dom->getNodeValue('tpAmb'); |
|
75
|
1 |
|
$dhEmi = $dom->getNodeValue('dhEmi'); |
|
76
|
|
|
} |
|
77
|
1 |
|
$aResp['Id'] = $schId; |
|
78
|
1 |
|
$aResp['tag'] = $key; |
|
79
|
1 |
|
$aResp['dom'] = $dom; |
|
80
|
1 |
|
$aResp['chave'] = $chave; |
|
81
|
1 |
|
$aResp['tpAmb'] = $tpAmb; |
|
82
|
1 |
|
$aResp['dhEmi'] = $dhEmi; |
|
83
|
1 |
|
return $schId; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* zSearchNode |
|
88
|
|
|
* @param DOMDocument $dom |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
1 |
|
protected static function zSearchNode($dom, &$key) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
1 |
|
foreach (self::$schemesId as $key => $schId) { |
|
94
|
1 |
|
$node = $dom->getElementsByTagName($key)->item(0); |
|
95
|
1 |
|
if (! empty($node)) { |
|
96
|
1 |
|
return $schId; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
return ''; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: