Completed
Push — master ( a019bc...315213 )
by Roberto
16:25 queued 13s
created

Standardize::whichIs()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7.3387

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 17
cts 21
cp 0.8095
rs 8.5066
c 0
b 0
f 0
cc 7
nc 12
nop 1
crap 7.3387
1
<?php
2
3
namespace NFePHP\NFe\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-2019
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\NFe\Exception\DocumentsException;
21
use stdClass;
22
23
class Standardize
24
{
25
    /**
26
     * @var string
27
     */
28
    private $node = '';
29
    /**
30
     * @var string
31
     */
32
    private $json = '';
33
    /**
34
     * @var string
35
     */
36
    public $key = '';
37
    /**
38
     * @var object
39
     */
40
    private $sxml;
41
    /**
42
     * @var array
43
     */
44
    public $rootTagList = [
45
        'distDFeInt',
46
        'resNFe',
47
        'resEvento',
48
        'envEvento',
49
        'ConsCad',
50
        'consSitNFe',
51
        'consReciNFe',
52
        'downloadNFe',
53
        'enviNFe',
54
        'inutNFe',
55
        'admCscNFCe',
56
        'consStatServ',
57
        'retDistDFeInt',
58
        'retEnvEvento',
59
        'retConsCad',
60
        'retConsSitNFe',
61
        'retConsReciNFe',
62
        'retDownloadNFe',
63
        'retEnviNFe',
64
        'retInutNFe',
65
        'retAdmCscNFCe',
66
        'retConsStatServ',
67
        'procInutNFe',
68
        'procEventoNFe',
69
        'procNFe',
70
        'nfeProc',
71
        'NFe'
72
    ];
73
74
    /**
75
     * Constructor
76
     * @param string $xml
77
     */
78 9
    public function __construct($xml = null)
79
    {
80 9
        $this->toStd($xml);
81 9
    }
82
83
    /**
84
     * Identify node and extract from XML for convertion type
85
     * @param string $xml
86
     * @return string identificated node name
87
     * @throws \InvalidArgumentException
88
     */
89 9
    public function whichIs($xml)
90
    {
91 9
        if (!Validator::isXML($xml)) {
92
            //invalid document is not a XML
93 3
            throw DocumentsException::wrongDocument(6);
94
        }
95 6
        $dom = new \DOMDocument('1.0', 'UTF-8');
96 6
        $dom->preserveWhiteSpace = false;
97 6
        $dom->formatOutput = false;
98 6
        $dom->loadXML($xml);
99 6
        foreach ($this->rootTagList as $key) {
100 6
            $node = !empty($dom->getElementsByTagName($key)->item(0))
101 5
                ? $dom->getElementsByTagName($key)->item(0)
102 6
                : '';
103 6
            if (!empty($node)) {
104 5
                $this->node = $dom->saveXML($node);
105 6
                return $key;
106
            }
107
        }
108 1
        $result = $dom->getElementsByTagName('nfeResultMsg')->item(0);
109 1
        if (!empty($result)) {
110
            $cont = $result->textContent;
111
            if (empty($cont)) {
112
                throw new Exception('O retorno da SEFAZ veio em BRANCO, '
113
                    . 'ou seja devido a um erro ou instabilidade na própria SEFAZ.');
114
            }
115
        }
116
        //documento does not belong to the SPED-NFe project
117 1
        throw DocumentsException::wrongDocument(7);
118
    }
119
120
    /**
121
     * Returns extract node from XML
122
     * @return string
123
     */
124 1
    public function __toString()
125
    {
126 1
        return $this->node;
127
    }
128
129
    /**
130
     * Returns stdClass converted from xml
131
     * @param string $xml
132
     * @return stdClass
133
     */
134 9
    public function toStd($xml = null)
135
    {
136 9
        if (!empty($xml)) {
137 4
            $this->key = $this->whichIs($xml);
138
        }
139 9
        $this->sxml = simplexml_load_string($this->node);
140 9
        $this->json = str_replace(
141 9
            '@attributes',
142 9
            'attributes',
143 9
            json_encode($this->sxml, JSON_PRETTY_PRINT)
144
        );
145
146 9
        $std = json_decode($this->json);
147 9
        if (isset($std->infNFeSupl)) {
148
            $resp = $this->getQRCode();
149
            $std->infNFeSupl->qrCode = $resp['qrCode'];
150
            $std->infNFeSupl->urlChave = $resp['urlChave'];
151
            $this->json = json_encode($std);
152
        }
153 9
        return $std;
154
    }
155
156
    /**
157
     * Return QRCODE and urlChave from XML
158
     * @return array
159
     */
160
    private function getQRCode()
161
    {
162
        $dom = new \DOMDocument('1.0', 'UTF-8');
163
        $dom->preserveWhiteSpace = false;
164
        $dom->formatOutput = false;
165
        $dom->loadXML($this->node);
166
        $node = $dom->getElementsByTagName('infNFeSupl')->item(0);
167
        $resp = [
168
            'qrCode' => $node->getElementsByTagName('qrCode')->item(0)->nodeValue,
169
            'urlChave' => $node->getElementsByTagName('urlChave')->item(0)->nodeValue
170
        ];
171
        return $resp;
172
    }
173
174
    /**
175
     * Returns the SimpleXml Object
176
     * @param string $xml
177
     * @return object
178
     */
179
    public function simpleXml($xml = null)
180
    {
181
        if (!empty($xml)) {
182
            $this->toStd($xml);
183
        }
184
        return $this->sxml;
185
    }
186
187
    /**
188
     * Returns JSON string form XML
189
     * @param string $xml
190
     * @return string
191
     */
192 1
    public function toJson($xml = null)
193
    {
194 1
        if (!empty($xml)) {
195
            $this->toStd($xml);
196
        }
197 1
        return $this->json;
198
    }
199
200
    /**
201
     * Returns array from XML
202
     * @param string $xml
203
     * @return array
204
     */
205 1
    public function toArray($xml = null)
206
    {
207 1
        if (!empty($xml)) {
208
            $this->toStd($xml);
209
        }
210 1
        return json_decode($this->json, true);
211
    }
212
}
213