|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NFePHP\Atende; |
|
4
|
|
|
|
|
5
|
|
|
use DOMDocument; |
|
6
|
|
|
use DOMElement; |
|
7
|
|
|
use stdClass; |
|
8
|
|
|
|
|
9
|
|
|
class Response |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Convert Atende xml response to stdClass |
|
13
|
|
|
* @param string $xml |
|
14
|
|
|
* @return stdClass |
|
15
|
|
|
*/ |
|
16
|
|
|
public static function toStd($xml) |
|
17
|
|
|
{ |
|
18
|
|
|
return json_decode(self::select($xml)); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Convert Atende xml response to array |
|
23
|
|
|
* @param string $xml |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function toArray($xml) |
|
27
|
|
|
{ |
|
28
|
|
|
return json_decode(self::select($xml), true); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Convert Atende xml response to json string |
|
33
|
|
|
* @param string $xml |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function toJson($xml) |
|
37
|
|
|
{ |
|
38
|
|
|
return self::select($xml); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Identify and convert xml |
|
43
|
|
|
* @param string $xml |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected static function select($xml) |
|
47
|
|
|
{ |
|
48
|
|
|
$infolist = [ |
|
49
|
|
|
'infoServidor', |
|
50
|
|
|
'infoDependente', |
|
51
|
|
|
'infoContribuicao', |
|
52
|
|
|
'infoContribuicaoAnalitica', |
|
53
|
|
|
'infoContribuicao', |
|
54
|
|
|
'infoTempoContribuicao' |
|
55
|
|
|
]; |
|
56
|
|
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
|
57
|
|
|
$dom->formatOutput = false; |
|
58
|
|
|
$dom->loadXML($xml); |
|
59
|
|
|
foreach ($infolist as $item) { |
|
60
|
|
|
$node = null; |
|
|
|
|
|
|
61
|
|
|
$node = $dom->getElementsByTagName($item)->item(0); |
|
62
|
|
|
if (!empty($node)) { |
|
63
|
|
|
return self::render($node); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Renderize DOMElement do json string |
|
70
|
|
|
* @param DOMElement $node |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
protected static function render(DOMElement $node) |
|
74
|
|
|
{ |
|
75
|
|
|
$newdoc = new DOMDocument('1.0', 'utf-8'); |
|
76
|
|
|
$newdoc->appendChild($newdoc->importNode($node, true)); |
|
77
|
|
|
$xml = $newdoc->saveXML(); |
|
78
|
|
|
$newdoc = null; |
|
|
|
|
|
|
79
|
|
|
$xml = str_replace('<?xml version="1.0" encoding="UTF-8"?>', '', $xml); |
|
80
|
|
|
$xml = str_replace('<?xml version="1.0" encoding="utf-8"?>', '', $xml); |
|
81
|
|
|
$resp = simplexml_load_string($xml, null, LIBXML_NOCDATA); |
|
82
|
|
|
$json = json_encode($resp, JSON_PRETTY_PRINT); |
|
83
|
|
|
$json = str_replace('@attributes', 'attributes', $json); |
|
84
|
|
|
$std = json_decode($json); |
|
85
|
|
|
return str_replace('{}', '""', json_encode($std, JSON_PRETTY_PRINT)); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.