Standardize::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace NFePHP\eFinanc\Common;
4
5
/**
6
 * Class for identification of eletronic documents in xml
7
 * used for Sped eFinanceira comunications
8
 *
9
 * @category  API
10
 * @package   NFePHP\eFinanc
11
 * @copyright Copyright (c) 2018
12
 * @license   http://www.gnu.org/licenses/lesser.html LGPL v3
13
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
14
 * @link      http://github.com/nfephp-org/sped-efinanceira for the canonical source repository
15
 */
16
use DOMDocument;
17
use stdClass;
18
use InvalidArgumentException;
19
use NFePHP\Common\Validator;
20
21
class Standardize
22
{
23
    /**
24
     * @var string
25
     */
26
    public $node = '';
27
    /**
28
     * @var string
29
     */
30
    public $json = '';
31
    /**
32
     * @var array
33
     */
34
    public $rootTagList = [
35
        'retornoLoteEventos',
36
        'retornoEvento',
37
        'retornoConsultaListaEFinanceira',
38
        'retornoConsultaInformacoesRerct',
39
        'retornoConsultaInformacoesPatrocinado',
40
        'retornoConsultaInformacoesMovimento',
41
        'retornoConsultaInformacoesIntermediario',
42
        'retornoConsultaInformacoesCadastrais',
43
        'evtRERCT',
44
        'evtMovOpFin',
45
        'evtFechamentoeFinanceira',
46
        'evtExclusaoeFinanceira',
47
        'evtExclusao',
48
        'evtCadPatrocinado',
49
        'evtCadIntermediario',
50
        'evtCadDeclarante',
51
        'evtAberturaeFinanceira',
52
        'loteEventos',
53
        'loteCriptografado'
54
    ];
55
    
56
    public function __construct($xml = null)
57
    {
58
        $this->toStd($xml);
59
    }
60
    
61
    /**
62
     * Identify node and extract from XML for convertion type
63
     * @param string $xml
64
     * @return string identificated node name
65
     * @throws InvalidArgumentException
66
     */
67
    public function whichIs($xml)
68
    {
69
        if (!Validator::isXML($xml)) {
70
            throw new InvalidArgumentException(
71
                "O argumento passado não é um XML válido."
72
            );
73
        }
74
        $dom = new \DOMDocument('1.0', 'UTF-8');
75
        $dom->preserveWhiteSpace = false;
76
        $dom->formatOutput = false;
77
        $dom->loadXML($xml);
78
        foreach ($this->rootTagList as $key) {
79
            $node = !empty($dom->getElementsByTagName($key)->item(0))
80
                ? $dom->getElementsByTagName($key)->item(0)
81
                : '';
82
            if (!empty($node)) {
83
                $this->node = $dom->saveXML($node);
84
                return $key;
85
            }
86
        }
87
        throw new InvalidArgumentException(
88
            "Este xml não pertence ao projeto eFinanceira."
89
        );
90
    }
91
    
92
    /**
93
     * Returns extract node from XML
94
     * @return string
95
     */
96
    public function __toString()
97
    {
98
        return $this->node;
99
    }
100
    
101
    /**
102
     * Returns stdClass converted from xml
103
     * @param string $xml
104
     * @return stdClass
105
     */
106
    public function toStd($xml = null)
107
    {
108
        if (!empty($xml)) {
109
            $this->whichIs($xml);
110
        }
111
        $sxml = simplexml_load_string($this->node);
112
        $this->json = str_replace(
113
            '@attributes',
114
            'attributes',
115
            json_encode($sxml, JSON_PRETTY_PRINT)
116
        );
117
        return json_decode($this->json);
118
    }
119
    
120
    /**
121
     * Retruns JSON string form XML
122
     * @param string $xml
123
     * @return string
124
     */
125
    public function toJson($xml = null)
126
    {
127
        if (!empty($xml)) {
128
            $this->toStd($xml);
129
        }
130
        return $this->json;
131
    }
132
    
133
    /**
134
     * Returns array from XML
135
     * @param string $xml
136
     * @return array
137
     */
138
    public function toArray($xml = null)
139
    {
140
        if (!empty($xml)) {
141
            $this->toStd($xml);
142
        }
143
        return json_decode($this->json, true);
144
    }
145
}
146