Completed
Pull Request — master (#83)
by
unknown
11:11
created

Standardize::toJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
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
use Symfony\Component\Yaml\Yaml;
23
24
class Standardize
25
{
26
    /**
27
     * @var string
28
     */
29
    public $node = '';
30
    /**
31
     * @var string
32
     */
33
    public $json = '';
34
    /**
35
     * @var string
36
     */
37
    public $key = '';
38
    /**
39
     * @var array
40
     */
41
    public $rootTagList = [
42
        'enviMDFe',
43
        'retEnviMDFe',
44
        'retMDFe',
45
        'protMDFe',
46
        'consReciMDFe',
47
        'retConsReciMDFe',
48
        'consSitMDFe',
49
        'retConsSitMDFe',
50
        'retConsMDFeNaoEnc',
51
        'consStatServMDFe',
52
        'retConsStatServMDFe',
53
        'eventoMDFe',
54
        'retEventoMDFe',
55
        'evCancMDFe',
56
        'evEncMDFe',
57
        'evIncCondutorMDFe',
58
        'evIncDFeMDFe'
59
    ];
60
61
    /**
62
     * Constructor
63
     * @param string $xml
64
     */
65
    public function __construct($xml = null)
66
    {
67
        $this->toStd($xml);
68
    }
69
70
    /**
71
     * Identify node and extract from XML for convertion type
72
     * @param string $xml
73
     * @return string identificated node name
74
     * @throws InvalidArgumentException
75
     */
76
    public function whichIs($xml)
77
    {
78
        if (!Validator::isXML($xml)) {
79
            //invalid document is not a XML
80
            throw DocumentsException::wrongDocument(6);
81
        }
82
        $dom = new \DOMDocument('1.0', 'UTF-8');
83
        $dom->preserveWhiteSpace = false;
84
        $dom->formatOutput = false;
85
        $dom->loadXML($xml);
86
        foreach ($this->rootTagList as $key) {
87
            $node = !empty($dom->getElementsByTagName($key)->item(0))
88
                ? $dom->getElementsByTagName($key)->item(0)
89
                : '';
90
            if (!empty($node)) {
91
                $this->node = $dom->saveXML($node);
92
                return $key;
93
            }
94
        }
95
        //documento does not belong to the SPED-MDFe project
96
        throw DocumentsException::wrongDocument(7);
97
    }
98
99
    /**
100
     * Returns extract node from XML
101
     * @return string
102
     */
103
    public function __toString()
104
    {
105
        return $this->node;
106
    }
107
108
    /**
109
     * Returns stdClass converted from xml
110
     * @param string $xml
111
     * @return stdClass
112
     */
113
    public function toStd($xml = null)
114
    {
115
        if (!empty($xml)) {
116
            $this->key = $this->whichIs($xml);
117
        }
118
119
        $sxml = simplexml_load_string($this->node);
120
        $this->json = str_replace(
121
            '@attributes',
122
            'attributes',
123
            json_encode($sxml, JSON_PRETTY_PRINT)
124
        );
125
        return json_decode($this->json);
126
    }
127
128
    /**
129
     * Retruns JSON string form XML
130
     * @param string $xml
131
     * @return string
132
     */
133
    public function toJson($xml = null)
134
    {
135
        if (!empty($xml)) {
136
            $this->toStd($xml);
137
        }
138
        return $this->json;
139
    }
140
141
    /**
142
     * Returns array from XML
143
     * @param string $xml
144
     * @return array
145
     */
146
    public function toArray($xml = null)
147
    {
148
        if (!empty($xml)) {
149
            $this->toStd($xml);
150
        }
151
        return json_decode($this->json, true);
152
    }
153
154
    /**
155
     * Returns YAML from XML
156
     * @param string $xml
157
     * @return string
158
     */
159
    public function toYaml($xml = null)
160
    {
161
        if (!empty($xml)) {
162
            $this->toStd($xml);
163
        }
164
        $array = $this->toArray();
165
        return Yaml::dump($array, 6, 4);
166
    }
167
}
168