1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brazanation\Documents; |
4
|
|
|
|
5
|
|
|
final class PisPasep extends AbstractDocument |
6
|
|
|
{ |
7
|
|
|
const LENGTH = 11; |
8
|
|
|
|
9
|
|
|
const LABEL = 'PisPasep'; |
10
|
|
|
|
11
|
|
|
const REGEX = '/^([\d]{3})([\d]{5})([\d]{2})([\d]{1})$/'; |
12
|
|
|
|
13
|
|
|
const NUMBER_OF_DIGITS = 1; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* PisPasep constructor. |
17
|
|
|
* |
18
|
|
|
* @param $number |
19
|
|
|
*/ |
20
|
14 |
|
public function __construct(string $number) |
21
|
|
|
{ |
22
|
14 |
|
$number = preg_replace('/\D/', '', $number); |
23
|
14 |
|
parent::__construct($number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL); |
24
|
|
|
} |
25
|
|
|
|
26
|
7 |
|
public static function createFromString(string $number) |
27
|
|
|
{ |
28
|
7 |
|
return parent::tryCreateFromString(self::class, $number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string Returns formatted number, such as: 00.00000.00-0 |
33
|
|
|
*/ |
34
|
4 |
|
public function format() : string |
35
|
|
|
{ |
36
|
4 |
|
return preg_replace(self::REGEX, '$1.$2.$3-$4', "{$this}"); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
12 |
|
public function calculateDigit(string $baseNumber) : string |
43
|
|
|
{ |
44
|
12 |
|
$calculator = new DigitCalculator($baseNumber); |
45
|
12 |
|
$calculator->withMultipliersInterval(2, 9); |
46
|
12 |
|
$calculator->useComplementaryInsteadOfModule(); |
47
|
12 |
|
$calculator->replaceWhen('0', 10, 11); |
48
|
12 |
|
$calculator->withModule(DigitCalculator::MODULE_11); |
49
|
12 |
|
$digit = $calculator->calculate(); |
50
|
|
|
|
51
|
12 |
|
return "{$digit}"; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|