Passed
Push — master ( c1d1cf...f48ccd )
by Francimar
03:58
created

Aliquota::loadNode()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 4.1016

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 22
cts 27
cp 0.8148
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 23
nc 6
nop 2
crap 4.1016
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\PIS;
29
30
use NFe\Common\Util;
31
use NFe\Entity\Imposto;
32
33
class Aliquota extends Imposto
34
{
35
36
    const TRIBUTACAO_NORMAL = 'normal';
37
    const TRIBUTACAO_DIFERENCIADA = 'diferenciada';
38
39 30
    public function __construct($pis = array())
40
    {
41 30
        parent::__construct($pis);
42 30
        $this->setGrupo(self::GRUPO_PIS);
43 30
    }
44
45 30
    public function getTributacao($normalize = false)
46
    {
47 30
        if (!$normalize) {
48 30
            return parent::getTributacao();
49
        }
50 29
        switch (parent::getTributacao()) {
51 29
            case self::TRIBUTACAO_NORMAL:
52 5
                return '01';
53 24
            case self::TRIBUTACAO_DIFERENCIADA:
54
                return '02';
55 24
        }
56 24
        return parent::getTributacao($normalize);
57
    }
58
59 5
    public function toArray($recursive = false)
60
    {
61 5
        $pis = parent::toArray($recursive);
62 5
        return $pis;
63
    }
64
65 30
    public function fromArray($pis = array())
66
    {
67 30
        if ($pis instanceof Aliquota) {
68 3
            $pis = $pis->toArray();
69 30
        } elseif (!is_array($pis)) {
70 3
            return $this;
71
        }
72 30
        parent::fromArray($pis);
73 30
        if (is_null($this->getTributacao())) {
74 30
            $this->setTributacao(self::TRIBUTACAO_NORMAL);
75 30
        }
76 30
        return $this;
77
    }
78
79 29
    public function getNode($name = null)
80
    {
81 29
        $dom = new \DOMDocument('1.0', 'UTF-8');
82 29
        $element = $dom->createElement(is_null($name)?'PISAliq':$name);
83 29
        Util::appendNode($element, 'CST', $this->getTributacao(true));
84 29
        Util::appendNode($element, 'vBC', $this->getBase(true));
85 29
        Util::appendNode($element, 'pPIS', $this->getAliquota(true));
86 29
        Util::appendNode($element, 'vPIS', $this->getValor(true));
87 29
        return $element;
88
    }
89
90 24
    public function loadNode($element, $name = null)
91
    {
92 24
        $name = is_null($name)?'PISAliq':$name;
93 24
        if ($element->nodeName != $name) {
94
            $_fields = $element->getElementsByTagName($name);
95
            if ($_fields->length == 0) {
96
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
97
            }
98
            $element = $_fields->item(0);
99
        }
100 24
        $this->setTributacao(
101 24
            Util::loadNode(
102 24
                $element,
103 24
                'CST',
104
                'Tag "CST" do campo "Tributacao" não encontrada'
105 24
            )
106 24
        );
107 24
        $this->setBase(
108 24
            Util::loadNode(
109 24
                $element,
110 24
                'vBC',
111
                'Tag "vBC" do campo "Base" não encontrada'
112 24
            )
113 24
        );
114 24
        $this->setAliquota(
115 24
            Util::loadNode(
116 24
                $element,
117 24
                'pPIS',
118
                'Tag "pPIS" do campo "Aliquota" não encontrada'
119 24
            )
120 24
        );
121 24
        return $element;
122
    }
123
}
124