Completed
Pull Request — master (#41)
by Antonio Oertel
06:39
created

JudiciaryProcess::extractNumbers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Brazanation\Documents;
4
5
/**
6
 * Formats and validates the numbers of legal proceedings
7
 * related to Judiciary assessments.
8
 * This numbers are used in some SPED projects like EfdReinf and eSocial, from
9
 * Braziliam IRS (Receita Federal)
10
 *
11
 * @property string $sequentialNumber Identify sequential number of process by origin unit.
12
 * @property string $year Identifies the year the process is filed.
13
 * @property string $judiciary Identifies segment of the Judiciary.
14
 * @property string $court Identifies of court from Judiciary.
15
 * @property string $origin Unit of origin of the process.
16
 */
17
final class JudiciaryProcess extends AbstractDocument
18
{
19
    const LENGTH = 20;
20
21
    const LABEL = 'PROCESSO_JUDICIAL';
22
23
    const REGEX = '/^([\d]{7})([\d]{2})([\d]{4})([\d]{1})([\d]{2})([\d]{0,4})$/';
24
25
    /**
26
     * Identify sequential number of process by origin unit.
27
     *
28
     * @var string
29
     */
30
    private $sequentialNumber;
31
32
    /**
33
     * Identifies the year the process is filed.
34
     *
35
     * @var string
36
     */
37
    private $year;
38
39
    /**
40
     * Identifies segment of the Judiciary.
41
     *
42
     * @var string
43
     */
44
    private $judiciary;
45
46
    /**
47
     * Identifies of court from Judiciary.
48
     *
49
     * @var string
50
     */
51
    private $court;
52
53
    /**
54
     * unit of origin of the process.
55
     *
56
     * @var string
57
     */
58
    private $origin;
59
60 11
    public function __construct($number)
61
    {
62 11
        $number = preg_replace('/\D/', '', $number);
63 11
        $this->extractNumbers($number);
64 11
        parent::__construct($number, self::LENGTH, 2, self::LABEL);
65 4
    }
66
67
    /**
68
     * Extract identification numbers.
69
     *
70
     * @param string $number
71
     */
72 11
    private function extractNumbers($number)
73
    {
74 11
        $number = str_pad($number, self::LENGTH, '0', STR_PAD_RIGHT);
75
76 11
        preg_match(self::REGEX, $number, $matches);
77 11
        $this->sequentialNumber = $matches[1];
78 11
        $this->year = $matches[3];
79 11
        $this->judiciary = $matches[4];
80 11
        $this->court = $matches[5];
81 11
        $this->origin = $matches[6];
82 11
    }
83
84 2
    public function format()
85
    {
86 2
        $number = str_pad($this->number, self::LENGTH, '0', STR_PAD_RIGHT);
87 2
        return preg_replace(self::REGEX, '$1-$2.$3.$4.$5.$6', "{$number}");
88
    }
89
90 11
    protected function extractCheckerDigit($number)
91
    {
92 11
        return substr($number, 7, 2);
93
    }
94
95 8
    protected function extractBaseNumber($number)
96
    {
97 8
        return "{$this->sequentialNumber}{$this->year}{$this->judiciary}{$this->court}{$this->origin}";
98
    }
99
100
    /**
101
     * Calculate check digit Algoritm Module 97 Base 10 (ISO 7064)
102
     * Anexo VIII da Resolução CNJ no 65, de 16 de dezembro de 2008.
103
     *
104
     * @see http://www.cnj.jus.br/busca-atos-adm?documento=2748
105
     * @see http://www.cnj.jus.br/images/stories/docs_cnj/resolucao/anexorescnj_65.pdf
106
     *
107
     * @param string $input
108
     *
109
     * @return string
110
     */
111 8
    public function calculateDigit($input)
112
    {
113 8
        $remainderNumber = intval($this->sequentialNumber) % 97;
114
115 8
        $judiciaryNumber = "{$remainderNumber}{$this->year}{$this->judiciary}{$this->court}";
116
117 8
        $remainderJudiciary = intval($judiciaryNumber) % 97;
118 8
        $originNumber = "{$remainderJudiciary}{$this->origin}00";
119
120 8
        $remainder = (float) $originNumber % 97;
121 8
        $digit = 98 - $remainder;
122
123 8
        return str_pad($digit, 2, '0', STR_PAD_LEFT);
124
    }
125
}
126