1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NFePHP\CTe\Common; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class for identification and convertion of eletronic documents in xml |
7
|
|
|
* for documents used in sped-nfe, sped-esocial, sped-cte, sped-mdfe, etc. |
8
|
|
|
* |
9
|
|
|
* @category NFePHP |
10
|
|
|
* @package NFePHP\Common\Standardize |
11
|
|
|
* @copyright NFePHP Copyright (c) 2008-2017 |
12
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPLv3+ |
13
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
14
|
|
|
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+ |
15
|
|
|
* @author Roberto L. Machado <linux.rlm at gmail dot com> |
16
|
|
|
* @link http://github.com/nfephp-org/sped-nfe for the canonical source repository |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use NFePHP\Common\Validator; |
20
|
|
|
use NFePHP\CTe\Exception\DocumentsException; |
21
|
|
|
use stdClass; |
22
|
|
|
|
23
|
|
|
class Standardize |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $node = ''; |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
public $json = ''; |
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
public $key = ''; |
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
public $rootTagList = [ |
41
|
|
|
'enviCTe', |
42
|
|
|
'retEnviCte', |
43
|
|
|
'retCTeOS', |
44
|
|
|
'retConsReciCTe', |
45
|
|
|
'consSitCTe', |
46
|
|
|
'retConsSitCTe', |
47
|
|
|
'eventoCTe', |
48
|
|
|
'retEventoCTe', |
49
|
|
|
'cteRecepcaoOSResult', |
50
|
|
|
'evCancCTe', |
51
|
|
|
'inutCTe', |
52
|
|
|
'retInutCTe', |
53
|
|
|
'procInutCTe', |
54
|
|
|
'CTe', |
55
|
|
|
'CTeOS', |
56
|
|
|
'retConsStatServCte', |
57
|
|
|
'cteDistDFeInteresseResponse' |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Constructor |
62
|
|
|
* @param string $xml |
63
|
|
|
*/ |
64
|
|
|
public function __construct($xml = null) |
65
|
|
|
{ |
66
|
|
|
$this->toStd($xml); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Identify node and extract from XML for convertion type |
71
|
|
|
* @param string $xml |
72
|
|
|
* @return string identificated node name |
73
|
|
|
* @throws InvalidArgumentException |
74
|
|
|
*/ |
75
|
|
|
public function whichIs($xml) |
76
|
|
|
{ |
77
|
|
|
if (!Validator::isXML($xml)) { |
78
|
|
|
//invalid document is not a XML |
79
|
|
|
throw DocumentsException::wrongDocument(6); |
80
|
|
|
} |
81
|
|
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
82
|
|
|
$dom->preserveWhiteSpace = false; |
83
|
|
|
$dom->formatOutput = false; |
84
|
|
|
$dom->loadXML($xml); |
85
|
|
|
foreach ($this->rootTagList as $key) { |
86
|
|
|
$node = !empty($dom->getElementsByTagName($key)->item(0)) |
87
|
|
|
? $dom->getElementsByTagName($key)->item(0) |
88
|
|
|
: ''; |
89
|
|
|
if (!empty($node)) { |
90
|
|
|
$this->node = $dom->saveXML($node); |
91
|
|
|
return $key; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
//documento does not belong to the SPED-NFe project |
95
|
|
|
throw DocumentsException::wrongDocument(7); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns extract node from XML |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function __toString() |
103
|
|
|
{ |
104
|
|
|
return $this->node; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns stdClass converted from xml |
109
|
|
|
* @param string $xml |
110
|
|
|
* @return stdClass |
111
|
|
|
*/ |
112
|
|
|
public function toStd($xml = null) |
113
|
|
|
{ |
114
|
|
|
if (!empty($xml)) { |
115
|
|
|
$this->key = $this->whichIs($xml); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$sxml = simplexml_load_string($this->node); |
119
|
|
|
$this->json = str_replace( |
120
|
|
|
'@attributes', |
121
|
|
|
'attributes', |
122
|
|
|
json_encode($sxml, JSON_PRETTY_PRINT) |
123
|
|
|
); |
124
|
|
|
return json_decode($this->json); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Retruns JSON string form XML |
129
|
|
|
* @param string $xml |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function toJson($xml = null) |
133
|
|
|
{ |
134
|
|
|
if (!empty($xml)) { |
135
|
|
|
$this->toStd($xml); |
136
|
|
|
} |
137
|
|
|
return $this->json; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns array from XML |
142
|
|
|
* @param string $xml |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function toArray($xml = null) |
146
|
|
|
{ |
147
|
|
|
if (!empty($xml)) { |
148
|
|
|
$this->toStd($xml); |
149
|
|
|
} |
150
|
|
|
return json_decode($this->json, true); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|