Passed
Branch main (274970)
by Antonio Oertel
03:05
created

JudiciaryProcess::createFromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
    const NUMBER_OF_DIGITS = 2;
26
27
    /**
28
     * Identify sequential number of process by origin unit.
29
     *
30
     * @var string
31
     */
32
    private $sequentialNumber;
33
34
    /**
35
     * Identifies the year the process is filed.
36
     *
37
     * @var string
38
     */
39
    private $year;
40
41
    /**
42
     * Identifies segment of the Judiciary.
43
     *
44
     * @var string
45
     */
46
    private $judiciary;
47
48
    /**
49
     * Identifies of court from Judiciary.
50
     *
51
     * @var string
52
     */
53
    private $court;
54
55
    /**
56
     * unit of origin of the process.
57
     *
58
     * @var string
59
     */
60
    private $origin;
61
62 14
    public function __construct(string $number)
63
    {
64 14
        $number = preg_replace('/\D/', '', $number);
65 14
        $this->extractNumbers($number);
66 14
        parent::__construct($number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
67
    }
68
69 7
    public static function createFromString(string $number)
70
    {
71 7
        return parent::tryCreateFromString(self::class, $number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
72
    }
73
74
    /**
75
     * Extract identification numbers.
76
     *
77
     * @param string $number
78
     */
79 14
    private function extractNumbers(string $number)
80
    {
81 14
        $number = str_pad($number, self::LENGTH, '0', STR_PAD_RIGHT);
82
83 14
        preg_match(self::REGEX, $number, $matches);
84 14
        $this->sequentialNumber = $matches[1];
85 14
        $this->year = $matches[3];
86 14
        $this->judiciary = $matches[4];
87 14
        $this->court = $matches[5];
88 14
        $this->origin = $matches[6];
89
    }
90
91 4
    public function format() : string
92
    {
93 4
        $number = str_pad($this->number, self::LENGTH, '0', STR_PAD_RIGHT);
94
95 4
        return preg_replace(self::REGEX, '$1-$2.$3.$4.$5.$6', "{$number}");
96
    }
97
98 14
    protected function extractCheckerDigit(string $number) : string
99
    {
100 14
        return substr($number, 7, 2);
101
    }
102
103 14
    protected function extractBaseNumber(string $number) : string
104
    {
105 14
        return "{$this->sequentialNumber}{$this->year}{$this->judiciary}{$this->court}{$this->origin}";
106
    }
107
108
    /**
109
     * Calculate check digit Algoritm Module 97 Base 10 (ISO 7064)
110
     * Anexo VIII da Resolução CNJ no 65, de 16 de dezembro de 2008.
111
     *
112
     * @see http://www.cnj.jus.br/busca-atos-adm?documento=2748
113
     * @see http://www.cnj.jus.br/images/stories/docs_cnj/resolucao/anexorescnj_65.pdf
114
     *
115
     * @param string $input
116
     *
117
     * @return string
118
     */
119 14
    public function calculateDigit(string $input) : string
120
    {
121 14
        $remainderNumber = (int) $this->sequentialNumber % 97;
122
123 14
        $judiciaryNumber = "{$remainderNumber}{$this->year}{$this->judiciary}{$this->court}";
124
125 14
        $remainderJudiciary = (int) $judiciaryNumber % 97;
126 14
        $originNumber = "{$remainderJudiciary}{$this->origin}00";
127
128 14
        $remainder = (float) $originNumber % 97;
129 14
        $digit = 98 - $remainder;
130
131 14
        return str_pad($digit, 2, '0', STR_PAD_LEFT);
132
    }
133
}
134