1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NFePHP\EFDReinf\Common; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class for identification of eletronic documents in xml |
7
|
|
|
* used for Sped EFD-Reinf comunications |
8
|
|
|
* |
9
|
|
|
* @category library |
10
|
|
|
* @package NFePHP\EFDReinf |
11
|
|
|
* @copyright NFePHP Copyright (c) 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-efdreinf for the canonical source repository |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use DOMDocument; |
20
|
|
|
use stdClass; |
21
|
|
|
use InvalidArgumentException; |
22
|
|
|
use NFePHP\Common\Validator; |
23
|
|
|
|
24
|
|
|
class Standardize |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public $node = ''; |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $json = ''; |
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
public $rootTagList = [ |
38
|
|
|
'loteEventos', |
39
|
|
|
'retornoLoteEventos', |
40
|
|
|
'evtTotalContrib', |
41
|
|
|
'evtTotal', |
42
|
|
|
'Reinf' |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
public function __construct($xml = null) |
46
|
|
|
{ |
47
|
|
|
$this->toStd($xml); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Identify node and extract from XML for convertion type |
52
|
|
|
* @param string $xml |
53
|
|
|
* @return string identificated node name |
54
|
|
|
* @throws InvalidArgumentException |
55
|
|
|
*/ |
56
|
|
|
public function whichIs($xml) |
57
|
|
|
{ |
58
|
|
|
if (!Validator::isXML($xml)) { |
59
|
|
|
throw new InvalidArgumentException( |
60
|
|
|
"O argumento passado não é um XML válido." |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
64
|
|
|
$dom->preserveWhiteSpace = false; |
65
|
|
|
$dom->formatOutput = false; |
66
|
|
|
$dom->loadXML($xml); |
67
|
|
|
foreach ($this->rootTagList as $key) { |
68
|
|
|
$node = !empty($dom->getElementsByTagName($key)->item(0)) |
69
|
|
|
? $dom->getElementsByTagName($key)->item(0) |
70
|
|
|
: ''; |
71
|
|
|
if (!empty($node)) { |
72
|
|
|
$this->node = $dom->saveXML($node); |
73
|
|
|
return $key; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
throw new InvalidArgumentException( |
77
|
|
|
"Este xml não pertence ao projeto eSocial." |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns extract node from XML |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function __toString() |
86
|
|
|
{ |
87
|
|
|
return $this->node; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns stdClass converted from xml |
92
|
|
|
* @param string $xml |
93
|
|
|
* @return stdClass |
94
|
|
|
*/ |
95
|
|
|
public function toStd($xml = null) |
96
|
|
|
{ |
97
|
|
|
if (!empty($xml)) { |
98
|
|
|
$this->whichIs($xml); |
99
|
|
|
} |
100
|
|
|
$sxml = simplexml_load_string($this->node); |
101
|
|
|
$this->json = str_replace( |
102
|
|
|
'@attributes', |
103
|
|
|
'attributes', |
104
|
|
|
json_encode($sxml, JSON_PRETTY_PRINT) |
105
|
|
|
); |
106
|
|
|
return json_decode($this->json); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Retruns JSON string form XML |
111
|
|
|
* @param string $xml |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function toJson($xml = null) |
115
|
|
|
{ |
116
|
|
|
if (!empty($xml)) { |
117
|
|
|
$this->toStd($xml); |
118
|
|
|
} |
119
|
|
|
return $this->json; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns array from XML |
124
|
|
|
* @param string $xml |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function toArray($xml = null) |
128
|
|
|
{ |
129
|
|
|
if (!empty($xml)) { |
130
|
|
|
$this->toStd($xml); |
131
|
|
|
} |
132
|
|
|
return json_decode($this->json, true); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|