Test Failed
Push — master ( 5f3052...29edbd )
by Francimar
08:45
created

Transportador::fromArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * MIT License
4
 *
5
 * Copyright (c) 2016 MZ 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\Transporte;
29
30
use NFe\Common\Util;
31
use NFe\Entity\Destinatario;
32
33
/**
34
 * Dados da transportadora
35
 */
36
class Transportador extends Destinatario
37
{
38
39 45
    public function __construct($transportador = array())
40
    {
41 45
        parent::__construct($transportador);
42 45
    }
43
44 1
    public function toArray($recursive = false)
45
    {
46 1
        $transportador = parent::toArray($recursive);
47 1
        return $transportador;
48
    }
49
50 45
    public function fromArray($transportador = array())
51
    {
52 45
        if ($transportador instanceof Transportador) {
53 1
            $transportador = $transportador->toArray();
54 45
        } elseif (!is_array($transportador)) {
55 1
            return $this;
56
        }
57 45
        parent::fromArray($transportador);
58 45
        return $this;
59
    }
60
61 10
    public function getNode($name = null)
62
    {
63 10
        $dom = new \DOMDocument('1.0', 'UTF-8');
64 10
        $element = $dom->createElement(is_null($name)?'transporta':$name);
65 10
        if (!is_null($this->getCNPJ())) {
66 10
            Util::appendNode($element, 'CNPJ', $this->getCNPJ(true));
67
        } else {
68
            Util::appendNode($element, 'CPF', $this->getCPF(true));
69
        }
70 10
        if (!is_null($this->getCNPJ())) {
71 10
            Util::appendNode($element, 'xNome', $this->getRazaoSocial(true));
72
        } else {
73
            Util::appendNode($element, 'xNome', $this->getNome(true));
74
        }
75 10
        if (!is_null($this->getCNPJ())) {
76 10
            Util::appendNode($element, 'IE', $this->getIE(true));
77
        }
78 10
        if (!is_null($this->getEndereco())) {
79 8
            $endereco = $this->getEndereco();
80 8
            Util::appendNode($element, 'xEnder', $endereco->getDescricao(true));
81 8
            Util::appendNode($element, 'xMun', $endereco->getMunicipio()->getNome(true));
82 8
            Util::appendNode($element, 'UF', $endereco->getMunicipio()->getEstado()->getUF(true));
83
        }
84 10
        return $element;
85
    }
86
87 5
    public function loadNode($element, $name = null)
88
    {
89 5
        $name = is_null($name)?'transporta':$name;
90 5
        if ($element->nodeName != $name) {
91
            $_fields = $element->getElementsByTagName($name);
92
            if ($_fields->length == 0) {
93
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
94
            }
95
            $element = $_fields->item(0);
96
        }
97 5
        $cnpj = Util::loadNode($element, 'CNPJ');
98 5
        $cpf = Util::loadNode($element, 'CPF');
99 5
        if (is_null($cnpj) && is_null($cpf)) {
100
            throw new \Exception('Tag "CNPJ" ou "CPF" não encontrada no Transportador', 404);
101
        }
102 5
        $this->setCNPJ($cnpj);
103 5
        $this->setCPF($cpf);
104 5
        if (!is_null($this->getCNPJ())) {
105 5
            $this->setRazaoSocial(
106 5
                Util::loadNode(
107
                    $element,
108 5
                    'xNome',
109 5
                    'Tag "xNome" do campo "RazaoSocial" não encontrada'
110
                )
111
            );
112
        } else {
113
            $this->setNome(
114
                Util::loadNode(
115
                    $element,
116
                    'xNome',
117
                    'Tag "xNome" do campo "Nome" não encontrada'
118
                )
119
            );
120
        }
121 5
        $this->setIE(
122 5
            Util::loadNode(
123
                $element,
124 5
                'IE',
125 5
                'Tag "IE" do campo "IE" não encontrada'
126
            )
127
        );
128 5
        $this->setIM(null);
129 5
        $descricao = Util::loadNode($element, 'xEnder');
130 5
        if (is_null($descricao)) {
131 1
            $this->setEndereco(null);
132 1
            return $element;
133
        }
134 4
        $endereco = new \NFe\Entity\Endereco();
135 4
        $endereco->parseDescricao($descricao);
136 4
        $endereco->getMunicipio()->setNome(
137 4
            Util::loadNode(
138
                $element,
139 4
                'xMun',
140 4
                'Tag "xMun" do nome do município não encontrada'
141
            )
142
        );
143 4
        $endereco->getMunicipio()->getEstado()->setUF(
144 4
            Util::loadNode(
145
                $element,
146 4
                'UF',
147 4
                'Tag "UF" da UF do estado não encontrada'
148
            )
149
        );
150 4
        $this->setEndereco($endereco);
151 4
        return $element;
152
    }
153
}
154