1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brazanation\Documents; |
4
|
|
|
|
5
|
|
|
use Brazanation\Documents\Exception\InvalidDocument; |
6
|
|
|
|
7
|
|
|
final class PisPasep implements DocumentInterface |
8
|
|
|
{ |
9
|
|
|
const LENGTH = 11; |
10
|
|
|
|
11
|
|
|
const LABEL = 'PisPasep'; |
12
|
|
|
|
13
|
|
|
const REGEX = '/^([\d]{3})([\d]{5})([\d]{2})([\d]{1})$/'; |
14
|
|
|
|
15
|
|
|
const FORMAT_REGEX = '/^[\d]{3}\.[\d]{5}\.[\d]{2}-[\d]{1}$/'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $pispasep; |
21
|
|
|
|
22
|
|
|
protected $mask = '000.00000.00-0'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* PisPasep constructor. |
26
|
|
|
* |
27
|
|
|
* @param $number |
28
|
|
|
*/ |
29
|
11 |
|
public function __construct($number) |
30
|
|
|
{ |
31
|
11 |
|
$number = preg_replace('/\D/', '', $number); |
32
|
11 |
|
$this->validate($number); |
33
|
4 |
|
$this->pispasep = $number; |
34
|
4 |
|
} |
35
|
|
|
|
36
|
11 |
|
private function validate($number) |
37
|
|
|
{ |
38
|
11 |
|
if (empty($number)) { |
39
|
3 |
|
throw InvalidDocument::notEmpty(static::LABEL); |
40
|
|
|
} |
41
|
8 |
|
if (!$this->isValidCV($number)) { |
42
|
4 |
|
throw InvalidDocument::isNotValid(static::LABEL, $number); |
43
|
|
|
} |
44
|
4 |
|
} |
45
|
|
|
|
46
|
8 |
|
private function isValidCV($number) |
47
|
|
|
{ |
48
|
8 |
|
if (strlen($number) != static::LENGTH) { |
49
|
1 |
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
7 |
|
if (preg_match("/^{$number[0]}{" . static::LENGTH . '}$/', $number)) { |
53
|
1 |
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
6 |
|
$digits = $this->calculateDigits(substr($number, 0, -1)); |
57
|
|
|
|
58
|
6 |
|
return $digits === substr($number, -1); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Formats PIS/PASEP number |
63
|
|
|
* |
64
|
|
|
* @return string Returns formatted number, such as: 00.00000.00-0 |
65
|
|
|
*/ |
66
|
2 |
|
public function format() |
67
|
|
|
{ |
68
|
2 |
|
return preg_replace(static::REGEX, '$1.$2.$3-$4', $this->pispasep); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
public function __toString() |
72
|
|
|
{ |
73
|
2 |
|
return (string) $this->pispasep; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Calculate check digits from base number. |
78
|
|
|
* |
79
|
|
|
* @param string $number Base numeric section to be calculate your digit. |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
6 |
|
private function calculateDigits($number) |
84
|
|
|
{ |
85
|
6 |
|
$calculator = new DigitCalculator($number); |
86
|
6 |
|
$calculator->withMultipliersInterval(2, 9); |
87
|
6 |
|
$calculator->useComplementaryInsteadOfModule(); |
88
|
6 |
|
$calculator->replaceWhen('0', 10, 11); |
89
|
6 |
|
$calculator->withModule(DigitCalculator::MODULE_11); |
90
|
6 |
|
$digit = $calculator->calculate(); |
91
|
|
|
|
92
|
6 |
|
return "{$digit}"; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|