1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brazanation\Documents; |
4
|
|
|
|
5
|
|
|
final class Cnpj extends AbstractDocument |
6
|
|
|
{ |
7
|
|
|
const LENGTH = 14; |
8
|
|
|
|
9
|
|
|
const LABEL = 'CNPJ'; |
10
|
|
|
|
11
|
|
|
const REGEX = '/^([\d]{2})([\d]{3})([\d]{3})([\d]{4})([\d]{2})$/'; |
12
|
|
|
|
13
|
|
|
const NUMBER_OF_DIGITS = 2; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Cnpj constructor. |
17
|
|
|
* |
18
|
|
|
* @param string $cnpj Only accept numbers |
19
|
|
|
*/ |
20
|
43 |
|
public function __construct(string $cnpj) |
21
|
|
|
{ |
22
|
43 |
|
$cnpj = preg_replace('/\D/', '', $cnpj); |
23
|
43 |
|
parent::__construct($cnpj, 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
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
4 |
|
public function format() : string |
35
|
|
|
{ |
36
|
4 |
|
return preg_replace(self::REGEX, '$1.$2.$3/$4-$5', "{$this}"); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
41 |
|
public function calculateDigit(string $baseNumber) : string |
43
|
|
|
{ |
44
|
41 |
|
$calculator = new DigitCalculator($baseNumber); |
45
|
41 |
|
$calculator->useComplementaryInsteadOfModule(); |
46
|
41 |
|
$calculator->replaceWhen('0', 10, 11); |
47
|
41 |
|
$calculator->withModule(DigitCalculator::MODULE_11); |
48
|
41 |
|
$firstDigit = $calculator->calculate(); |
49
|
41 |
|
$calculator->addDigit($firstDigit); |
50
|
41 |
|
$secondDigit = $calculator->calculate(); |
51
|
|
|
|
52
|
41 |
|
return "{$firstDigit}{$secondDigit}"; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|