Standardize   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 128
ccs 0
cts 55
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A whichIs() 0 22 5
A __toString() 0 4 1
A toStd() 0 14 2
A toJson() 0 7 2
A toArray() 0 7 2
1
<?php
2
3
namespace NFePHP\MDFe\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
 * @author    Cleiton Perin <cperin20 at gmail dot com>
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
 * @category  NFePHP
16
 * @link      http://github.com/nfephp-org/sped-mdfe for the canonical source repository
17
 */
18
19
use NFePHP\Common\Validator;
20
use NFePHP\MDFe\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
        'enviMDFe',
42
        'retEnviMDFe',
43
        'retConsReciMDFe',
44
        'consSitMDFe',
45
        'retConsSitMDFe',
46
        'retConsMDFeNaoEnc',
47
        'retConsStatServMDFe',
48
        'retMDFe',
49
        'eventoMDFe',
50
        'retEventoMDFe',
51
        'evCancMDFe',
52
        'evEncMDFe',
53
        'evIncCondutorMDFe',
54
        'evIncDFeMDFe',
55
        'MDFe'
56
    ];
57
58
    /**
59
     * Constructor
60
     * @param string $xml
61
     */
62
    public function __construct($xml = null)
63
    {
64
        $this->toStd($xml);
65
    }
66
67
    /**
68
     * Identify node and extract from XML for convertion type
69
     * @param string $xml
70
     * @return string identificated node name
71
     * @throws InvalidArgumentException
72
     */
73
    public function whichIs($xml)
74
    {
75
        if (!Validator::isXML($xml)) {
76
            //invalid document is not a XML
77
            throw DocumentsException::wrongDocument(6);
78
        }
79
        $dom = new \DOMDocument('1.0', 'UTF-8');
80
        $dom->preserveWhiteSpace = false;
81
        $dom->formatOutput = false;
82
        $dom->loadXML($xml);
83
        foreach ($this->rootTagList as $key) {
84
            $node = !empty($dom->getElementsByTagName($key)->item(0))
85
                ? $dom->getElementsByTagName($key)->item(0)
86
                : '';
87
            if (!empty($node)) {
88
                $this->node = $dom->saveXML($node);
89
                return $key;
90
            }
91
        }
92
        //documento does not belong to the SPED-MDFe project
93
        throw DocumentsException::wrongDocument(7);
94
    }
95
96
    /**
97
     * Returns extract node from XML
98
     * @return string
99
     */
100
    public function __toString()
101
    {
102
        return $this->node;
103
    }
104
105
    /**
106
     * Returns stdClass converted from xml
107
     * @param string $xml
108
     * @return stdClass
109
     */
110
    public function toStd($xml = null)
111
    {
112
        if (!empty($xml)) {
113
            $this->key = $this->whichIs($xml);
114
        }
115
116
        $sxml = simplexml_load_string($this->node);
117
        $this->json = str_replace(
118
            '@attributes',
119
            'attributes',
120
            json_encode($sxml, JSON_PRETTY_PRINT)
121
        );
122
        return json_decode($this->json);
123
    }
124
125
    /**
126
     * Retruns JSON string form XML
127
     * @param string $xml
128
     * @return string
129
     */
130
    public function toJson($xml = null)
131
    {
132
        if (!empty($xml)) {
133
            $this->toStd($xml);
134
        }
135
        return $this->json;
136
    }
137
138
    /**
139
     * Returns array from XML
140
     * @param string $xml
141
     * @return array
142
     */
143
    public function toArray($xml = null)
144
    {
145
        if (!empty($xml)) {
146
            $this->toStd($xml);
147
        }
148
        return json_decode($this->json, true);
149
    }
150
}
151