Completed
Push — master ( f6da78...86bb06 )
by Francimar
03:45
created

Normal::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
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\Imposto\ICMS;
29
30
use NFe\Common\Util;
31
32
/**
33
 * Classe base do ICMS normal, estende de ICMS\Base
34
 */
35
class Normal extends Base
36
{
37
38
    const MODALIDADE_AGREGADO = 'agregado';
39
    const MODALIDADE_PAUTA = 'pauta';
40
    const MODALIDADE_TABELADO = 'tabelado';
41
    const MODALIDADE_OPERACAO = 'operacao';
42
43
    private $modalidade;
44
45 52
    public function __construct($normal = array())
46
    {
47 52
        parent::__construct($normal);
48 52
    }
49
50 47
    public function getModalidade($normalize = false)
51
    {
52 47
        if (!$normalize) {
53 43
            return $this->modalidade;
54
        }
55 20
        switch ($this->modalidade) {
56 20
            case self::MODALIDADE_AGREGADO:
57
                return '0';
58 20
            case self::MODALIDADE_PAUTA:
59
                return '1';
60 20
            case self::MODALIDADE_TABELADO:
61
                return '2';
62 20
            case self::MODALIDADE_OPERACAO:
63 9
                return '3';
64 11
        }
65 11
        return $this->modalidade;
66
    }
67
68 52
    public function setModalidade($modalidade)
69
    {
70 52
        $this->modalidade = $modalidade;
71 52
        return $this;
72
    }
73
74 6
    public function toArray()
75
    {
76 6
        $normal = parent::toArray();
77 6
        $normal['modalidade'] = $this->getModalidade();
78 6
        return $normal;
79
    }
80
81 52
    public function fromArray($normal = array())
82
    {
83 52
        if ($normal instanceof Normal) {
84
            $normal = $normal->toArray();
85 52
        } elseif (!is_array($normal)) {
86
            return $this;
87
        }
88 52
        parent::fromArray($normal);
89 52
        if (isset($normal['modalidade'])) {
90 4
            $this->setModalidade($normal['modalidade']);
91 4
        } else {
92 52
            $this->setModalidade(null);
93
        }
94 52
        return $this;
95
    }
96
97 20
    public function getNode($name = null)
98
    {
99 20
        $dom = new \DOMDocument('1.0', 'UTF-8');
100 20
        $element = $dom->createElement(is_null($name)?'IMCS':$name);
101 20
        Util::appendNode($element, 'orig', $this->getOrigem(true));
102 20
        Util::appendNode($element, 'CST', $this->getTributacao(true));
103 20
        Util::appendNode($element, 'modBC', $this->getModalidade(true));
104 20
        Util::appendNode($element, 'vBC', $this->getBase(true));
105 20
        Util::appendNode($element, 'pICMS', $this->getAliquota(true));
106 20
        Util::appendNode($element, 'vICMS', $this->getValor(true));
107 20
        return $element;
108
    }
109
110
111 9
    public function loadNode($element, $name = null)
112
    {
113 9
        $name = is_null($name)?'IMCS':$name;
114 9
        if ($element->tagName != $name) {
115
            $_fields = $element->getElementsByTagName($name);
116
            if ($_fields->length == 0) {
117
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
118
            }
119
            $element = $_fields->item(0);
120
        }
121 9
        $this->setOrigem(
122 9
            Util::loadNode(
123 9
                $element,
124 9
                'orig',
125
                'Tag "orig" do campo "Origem" não encontrada'
126 9
            )
127 9
        );
128 9
        $this->setTributacao(
129 9
            Util::loadNode(
130 9
                $element,
131 9
                'CST',
132
                'Tag "CST" do campo "Tributacao" não encontrada'
133 9
            )
134 9
        );
135 9
        $this->setModalidade(
136 9
            Util::loadNode(
137 9
                $element,
138 9
                'modBC',
139
                'Tag "modBC" do campo "Modalidade" não encontrada'
140 9
            )
141 9
        );
142 9
        $this->setBase(
143 9
            Util::loadNode(
144 9
                $element,
145 9
                'vBC',
146
                'Tag "vBC" do campo "Base" não encontrada'
147 9
            )
148 9
        );
149 9
        $this->setAliquota(
150 9
            Util::loadNode(
151 9
                $element,
152 9
                'pICMS',
153
                'Tag "pICMS" do campo "Aliquota" não encontrada'
154 9
            )
155 9
        );
156 9
        return $element;
157
    }
158
}
159