Passed
Pull Request — master (#130)
by Roberto
05:08
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\Common;
4
5
/**
6
 * Class for identification and convertion of eletronic documents in xml
7
 * from documents used in sped-nfe, sped-esocial, sped-cte, sped-mdfe, etc.
8
 * This class converts XML to JSON, ARRAY, YAML or sdtClass
9
 * To use in projects this class will be extented and include all root Tag List
10
 *
11
 * @category  NFePHP
12
 * @package   NFePHP\Common\Standardize
13
 * @copyright NFePHP Copyright (c) 2008-2017
14
 * @license   http://www.gnu.org/licenses/lgpl.txt LGPLv3+
15
 * @license   https://opensource.org/licenses/MIT MIT
16
 * @license   http://www.gnu.org/licenses/gpl.txt GPLv3+
17
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
18
 * @link      http://github.com/nfephp-org/sped-nfe for the canonical source repository
19
 */
20
21
use NFePHP\Common\Validator;
22
use NFePHP\Common\Exception\DocumentsException;
23
use Symfony\Component\Yaml\Yaml;
24
use DOMDocument;
25
use stdClass;
26
27
class Standardize
28
{
29
    /**
30
     * @var string
31
     */
32
    public $node = '';
33
    /**
34
     * @var string
35
     */
36
    public $json = '';
37
    /**
38
     * @var string
39
     */
40
    public $key = '';
41
    /**
42
     * @var array
43
     */
44
    public $rootTagList = [];
45
    
46
    /**
47
     * Constructor
48
     * @param string $xml
49
     */
50
    public function __construct($xml = null)
51
    {
52
        $this->toStd($xml);
53
    }
54
    
55
    /**
56
     * Load tag list
57
     * This list must contain the root tags of all XML documents used by
58
     * the project whether NFe, CTe, MDFe, eSocial or any other of the SPED set,
59
     * it is important to note that the order of this list is relevant
60
     * to the result.
61
     * @param array $tagList
62
     */
63
    public function loadTagList($tagList)
64
    {
65
        $this->rootTagList = $tagList;
66
    }
67
    
68
    /**
69
     * Identify node and extract from XML for convertion type
70
     * @param string $xml
71
     * @return string identificated node name
72
     * @throws InvalidArgumentException
73
     */
74
    public function whichIs($xml)
75
    {
76
        if (!Validator::isXML($xml)) {
77
            //invalid document is not a XML
78
            throw DocumentsException::wrongDocument(6);
79
        }
80
        $dom = new \DOMDocument('1.0', 'UTF-8');
81
        $dom->preserveWhiteSpace = false;
82
        $dom->formatOutput = false;
83
        $dom->loadXML($xml);
84
        foreach ($this->rootTagList as $key) {
85
            $node = !empty($dom->getElementsByTagName($key)->item(0))
86
                ? $dom->getElementsByTagName($key)->item(0)
87
                : '';
88
            if (!empty($node)) {
89
                $this->node = $dom->saveXML($node);
90
                return $key;
91
            }
92
        }
93
        //documento does not belong to the SPED-NFe project
94
        throw DocumentsException::wrongDocument(7);
95
    }
96
    
97
    /**
98
     * Returns extract node from XML
99
     * @return string
100
     */
101
    public function __toString()
102
    {
103
        return $this->node;
104
    }
105
    
106
    /**
107
     * Returns stdClass converted from xml
108
     * @param string $xml
109
     * @return stdClass
110
     */
111
    public function toStd($xml = null)
112
    {
113
        if (!empty($xml)) {
114
            $this->key = $this->whichIs($xml);
115
        }
116
        
117
        $sxml = simplexml_load_string($this->node);
118
        $this->json = str_replace(
119
            '@attributes',
120
            'attributes',
121
            json_encode($sxml, JSON_PRETTY_PRINT)
122
        );
123
        return json_decode($this->json);
124
    }
125
    
126
    /**
127
     * Retruns JSON string form XML
128
     * @param string $xml
129
     * @return string
130
     */
131
    public function toJson($xml = null)
132
    {
133
        if (!empty($xml)) {
134
            $this->toStd($xml);
135
        }
136
        return $this->json;
137
    }
138
    
139
    /**
140
     * Returns array from XML
141
     * @param string $xml
142
     * @return array
143
     */
144
    public function toArray($xml = null)
145
    {
146
        if (!empty($xml)) {
147
            $this->toStd($xml);
148
        }
149
        return json_decode($this->json, true);
150
    }
151
    
152
    /**
153
     * Returns YAML from XML
154
     * @param string $xml
155
     * @return string
156
     */
157
    public function toYaml($xml = null)
158
    {
159
        if (!empty($xml)) {
160
            $this->toStd($xml);
161
        }
162
        $array = $this->toArray();
163
        return Yaml::dump($array, 6, 4);
164
    }
165
}
166