EFD::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace NFePHP\EFD;
4
5
use NFePHP\EFD\Common\BlockInterface;
6
7
abstract class EFD
8
{
9
    protected $possibles = [];
10
11
    public function __construct()
12
    {
13
        //todo
14
    }
15
    
16
    /**
17
     * Add
18
     * @param BlockInterface $block
19
     */
20
    public function add(BlockInterface $block = null)
21
    {
22
        if (empty($block)) {
23
            return;
24
        }
25
        $name = strtolower((new \ReflectionClass($block))->getShortName());
26
        if (key_exists($name, $this->possibles)) {
27
            $this->{$name} = $block->get();
28
        }
29
    }
30
    
31
    /**
32
     * Create a EFD string
33
     */
34
    public function get()
35
    {
36
        $efd = '';
37
        $keys = array_keys($this->possibles);
38
        foreach ($keys as $key) {
39
            if (isset($this->$key)) {
40
                $efd .= $this->$key;
41
            }
42
        }
43
        $efd .= $this->totalize($efd);
44
        return $efd;
45
    }
46
    
47
    /**
48
     * Totals blocks contents
49
     * @param string $efd
50
     * @return string
51
     */
52
    protected function totalize($efd)
53
    {
54
        $tot = '';
55
        $keys = [];
56
        $aefd = explode("\n", $efd);
57
        foreach ($aefd as $element) {
58
            $param = explode("|", $element);
59
            if (!empty($param[1])) {
60
                $key = $param[1];
61
                if (!empty($keys[$key])) {
62
                    $keys[$key] += 1;
63
                } else {
64
                    $keys[$key] = 1;
65
                }
66
            }
67
        }
68
        //Inicializa o bloco 9
69
        $tot .= "|9001|0|\n";
70
        $n = 0;
71
        foreach ($keys as $key => $value) {
72
            if (!empty($key)) {
73
                $tot .= "|9900|$key|$value|\n";
74
                $n++;
75
            }
76
        }
77
        $n++;
78
        $tot .= "|9900|9001|1|\n";
79
        $tot .= "|9900|9900|". ($n+3)."|\n";
80
        $tot .= "|9900|9990|1|\n";
81
        $tot .= "|9900|9999|1|\n";
82
        $tot .= "|9990|". ($n+6) ."|\n";
83
        $efd .= $tot;
84
        $n = count(explode("\n", $efd));
85
        $tot .= "|9999|$n|\n";
86
        return $tot;
87
    }
88
}
89