Completed
Push — master ( 6ab1ea...97ee98 )
by Antonio Oertel
04:40
created

JudiciaryProcess   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 55
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A extractCheckerDigit() 0 4 1
A calculateDigit() 0 17 1
A __construct() 0 5 1
A format() 0 5 1
A extractBaseNumber() 0 5 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
final class JudiciaryProcess extends AbstractDocument
12
{
13
    const LENGTH = 20;
14
15
    const LABEL = 'PROCESSO_JUDICIAL';
16
17
    const REGEX = '/^([\d]{7})([\d]{2})([\d]{4})([\d]{1})([\d]{2})([\d]{0,4})$/';
18
19 11
    public function __construct($number)
20
    {
21 11
        $number = preg_replace('/\D/', '', $number);
22 11
        parent::__construct($number, self::LENGTH, 2, self::LABEL);
23 4
    }
24
    
25 2
    public function format()
26
    {
27 2
        $number = str_pad($this->number, self::LENGTH, '0', STR_PAD_RIGHT);
28 2
        return preg_replace(self::REGEX, '$1-$2.$3.$4.$5.$6', "{$number}");
29
    }
30
    
31 11
    protected function extractCheckerDigit($number)
32
    {
33 11
        return substr($number, 7, 2);
34
    }
35
    
36 8
    protected function extractBaseNumber($number)
37
    {
38 8
        return str_pad(substr($number, 0, 7)
39 8
            . substr($number, 9, strlen($number)-1), self::LENGTH, '0', STR_PAD_RIGHT);
40
    }
41
    
42
    /**
43
     * Calculate check digit Algoritm Module 97 Base 10 (ISO 7064)
44
     * Anexo VIII da Resolução CNJ no 65, de 16 de dezembro de 2008.
45
     * @param string $input
46
     * @return string
47
     */
48 8
    public function calculateDigit($input)
49
    {
50 8
        $n = (int) substr($input, 0, 7);
51 8
        $a = substr($input, 7, 4);
52 8
        $jtr = substr($input, 11, 3);
53 8
        $o = substr($input, 14, 6);
54 8
        $r1 = $n % 97;
55 8
        $v2 = str_pad("$r1", 2, '0', STR_PAD_LEFT)
56 8
            . str_pad($a, 4, '0', STR_PAD_LEFT)
57 8
            . str_pad($jtr, 3, '0', STR_PAD_LEFT);
58 8
        $r2 = $v2 % 97;
59 8
        $v3 = str_pad("$r2", 2, '0', STR_PAD_LEFT)
60 8
            . str_pad($o, 6, '0', STR_PAD_LEFT);
61 8
        $r3 = (float) $v3 % 97;
62 8
        $result = (string) (98 - $r3);
63 8
        return str_pad($result, 2, '0', STR_PAD_LEFT);
64
    }
65
}
66