Passed
Push — master ( 0522e2...cd53d2 )
by Francimar
13:18
created

Intermediador::fromArray()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.3222
c 0
b 0
f 0
cc 5
nc 9
nop 1
1
<?php
2
/**
3
 * MIT License
4
 *
5
 * Copyright (c) 2016 GrandChef Desenvolvimento de Sistemas LTDA
6
 *
7
 * @author Francimar Alves <[email protected]>
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
namespace NFe\Entity;
29
30
use DOMElement;
31
use NFe\Common\Util;
32
use NFe\Common\Node;
33
34
/**
35
 * Informações do Intermediador da Transação
36
 */
37
class Intermediador implements Node
38
{
39
    /**
40
     * CNPJ do Intermediador da Transação (agenciador, plataforma de delivery,
41
     * marketplace e similar) de serviços e de negócios.
42
     *
43
     * @var string
44
     */
45
    private $cnpj;
46
47
    /**
48
     * Identificador cadastrado no intermediador
49
     *
50
     * @var string
51
     */
52
    private $identificador;
53
54
    /**
55
     * Constroi uma instância de Intermediador vazia
56
     * @param array $intermediador Array contendo dados do Intermediador
57
     */
58
    public function __construct($intermediador = [])
59
    {
60
        $this->fromArray($intermediador);
61
    }
62
63
    /**
64
     * CNPJ do Intermediador da Transação (agenciador, plataforma de delivery,
65
     * marketplace e similar) de serviços e de negócios.
66
     * @param boolean $normalize informa se o cnpj deve estar no formato do XML
67
     * @return string cnpj of Intermediador
68
     */
69
    public function getCNPJ($normalize = false)
70
    {
71
        if (!$normalize) {
72
            return $this->cnpj;
73
        }
74
        return $this->cnpj;
75
    }
76
    
77
    /**
78
     * Altera o valor do CNPJ para o informado no parâmetro
79
     * @param mixed $cnpj novo valor para CNPJ
80
     * @param string $cnpj Novo cnpj para Intermediador
81
     * @return self A própria instância da classe
82
     */
83
    public function setCNPJ($cnpj)
84
    {
85
        $this->cnpj = $cnpj;
86
        return $this;
87
    }
88
89
    /**
90
     * Identificador cadastrado no intermediador
91
     * @param boolean $normalize informa se o identificador deve estar no formato do XML
92
     * @return string identificador of Intermediador
93
     */
94
    public function getIdentificador($normalize = false)
95
    {
96
        if (!$normalize) {
97
            return $this->identificador;
98
        }
99
        return $this->identificador;
100
    }
101
    
102
    /**
103
     * Altera o valor do Identificador para o informado no parâmetro
104
     * @param mixed $identificador novo valor para Identificador
105
     * @param string $identificador Novo identificador para Intermediador
106
     * @return self A própria instância da classe
107
     */
108
    public function setIdentificador($identificador)
109
    {
110
        $this->identificador = $identificador;
111
        return $this;
112
    }
113
114
    /**
115
     * Converte a instância da classe para um array de campos com valores
116
     * @return array Array contendo todos os campos e valores da instância
117
     */
118
    public function toArray($recursive = false)
0 ignored issues
show
Unused Code introduced by
The parameter $recursive is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
    {
120
        $intermediador = [];
121
        $intermediador['cnpj'] = $this->getCNPJ();
122
        $intermediador['identificador'] = $this->getIdentificador();
123
        return $intermediador;
124
    }
125
126
    /**
127
     * Atribui os valores do array para a instância atual
128
     * @param mixed $intermediador Array ou instância de Intermediador, para copiar os valores
129
     * @return self A própria instância da classe
130
     */
131
    public function fromArray($intermediador = [])
132
    {
133
        if ($intermediador instanceof Intermediador) {
134
            $intermediador = $intermediador->toArray();
135
        } elseif (!is_array($intermediador)) {
136
            return $this;
137
        }
138
        if (!isset($intermediador['cnpj'])) {
139
            $this->setCNPJ(null);
140
        } else {
141
            $this->setCNPJ($intermediador['cnpj']);
142
        }
143
        if (!isset($intermediador['identificador'])) {
144
            $this->setIdentificador(null);
145
        } else {
146
            $this->setIdentificador($intermediador['identificador']);
147
        }
148
        return $this;
149
    }
150
151
    /**
152
     * Cria um nó XML do intermediador de acordo com o leiaute da NFe
153
     * @param string $name Nome do nó que será criado
154
     * @return DOMElement Nó que contém todos os campos da classe
155
     */
156
    public function getNode($name = null)
157
    {
158
        $dom = new \DOMDocument('1.0', 'UTF-8');
159
        $element = $dom->createElement(is_null($name) ? 'infIntermed' : $name);
160
        Util::appendNode($element, 'cnpj', $this->getCNPJ(true));
161
        Util::appendNode($element, 'idCadIntTran', $this->getIdentificador(true));
162
        return $element;
163
    }
164
165
    /**
166
     * Carrega as informações do nó e preenche a instância da classe
167
     * @param DOMElement $element Nó do xml com todos as tags dos campos
168
     * @param string $name Nome do nó que será carregado
169
     * @return DOMElement Instância do nó que foi carregado
170
     */
171
    public function loadNode($element, $name = null)
172
    {
173
        $name = is_null($name) ? 'infIntermed' : $name;
174
        if ($element->nodeName != $name) {
175
            $_fields = $element->getElementsByTagName($name);
176
            if ($_fields->length == 0) {
177
                throw new \Exception("Tag \"$name\" do Intermediador não encontrada", 404);
178
            }
179
            $element = $_fields->item(0);
180
        }
181
        $this->setCNPJ(
182
            Util::loadNode(
183
                $element,
184
                'cnpj',
185
                'Tag "cnpj" não encontrada no Intermediador'
186
            )
187
        );
188
        $this->setIdentificador(
189
            Util::loadNode(
190
                $element,
191
                'idCadIntTran',
192
                'Tag "idCadIntTran" não encontrada no Intermediador'
193
            )
194
        );
195
        return $element;
196
    }
197
}
198