Passed
Push — master ( feaf84...03ed34 )
by Roberto
06:49 queued 18s
created

Standardize::toYaml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace NFePHP\eSocial\Common;
4
5
/**
6
 * Class for identification of eletronic documents in xml
7
 * used for Sped eSocial comunications
8
 *
9
 * @category  library
10
 * @package   NFePHP\eSocial
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-nfe for the canonical source repository
17
 */
18
19
use InvalidArgumentException;
20
use NFePHP\Common\Validator;
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 array
35
     */
36
    public $rootTagList = [
37
        'retornoEnvioLoteEventos', //extrai os dados no retorno do envio de eventos
38
        'retornoProcessamentoLoteEventos', //extrai os dados no retorno da consulta
39
        'envioLoteEventos', //extrai os dados no xml de envio do lote de eventos
40
        'consultaLoteEventos', //extrai os dados no xml da consulta
41
        'eSocial' //extrai os dados dos eventos
42
    ];
43
    
44
    /**
45
     * Constructor
46
     * @param string $xml
47
     */
48
    public function __construct($xml = null)
49
    {
50
        $this->toStd($xml);
51
    }
52
53
    /**
54
     * Returns stdClass converted from xml
55
     * @param  string $xml
56
     * @return stdClass
57
     */
58
    public function toStd($xml = null)
59
    {
60
        if (! empty($xml)) {
61
            $this->whichIs($xml);
62
        }
63
        $sxml = simplexml_load_string($this->node);
64
        $this->json = str_replace(
65
            '@attributes',
66
            'attributes',
67
            json_encode($sxml, JSON_PRETTY_PRINT)
68
        );
69
        return json_decode($this->json);
70
    }
71
72
    /**
73
     * Identify node and extract from XML for convertion type
74
     * @param  string $xml
75
     * @return string identificated node name
76
     * @throws InvalidArgumentException
77
     */
78
    public function whichIs($xml)
79
    {
80
        if (! Validator::isXML($xml)) {
81
            throw new InvalidArgumentException(
82
                "O argumento passado não é um XML válido."
83
            );
84
        }
85
        $dom = new \DOMDocument('1.0', 'UTF-8');
86
        $dom->preserveWhiteSpace = false;
87
        $dom->formatOutput = false;
88
        $dom->loadXML($xml);
89
        foreach ($this->rootTagList as $key) {
90
            $node = ! empty($dom->getElementsByTagName($key)->item(0))
91
                ? $dom->getElementsByTagName($key)->item(0)
92
                : '';
93
            if (! empty($node)) {
94
                $this->node = $dom->saveXML($node);
95
                return $key;
96
            }
97
        }
98
        throw new InvalidArgumentException(
99
            "Este xml não pertence ao projeto eSocial."
100
        );
101
    }
102
103
    /**
104
     * Returns extract node from XML
105
     * @return string
106
     */
107
    public function __toString()
108
    {
109
        return $this->node;
110
    }
111
112
    /**
113
     * Retruns JSON string form XML
114
     * @param  string $xml
115
     * @return string
116
     */
117
    public function toJson($xml = null)
118
    {
119
        if (! empty($xml)) {
120
            $this->toStd($xml);
121
        }
122
        return $this->json;
123
    }
124
125
    /**
126
     * Returns array from XML
127
     * @param  string $xml
128
     * @return array
129
     */
130
    public function toArray($xml = null)
131
    {
132
        if (! empty($xml)) {
133
            $this->toStd($xml);
134
        }
135
        return json_decode($this->json, true);
136
    }
137
}
138