Completed
Push — master ( c523cc...f6da78 )
by Francimar
05:08
created

Aliquota::fromArray()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

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