1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brazanation\Documents; |
4
|
|
|
|
5
|
|
|
final class Cpf extends AbstractDocument |
6
|
|
|
{ |
7
|
|
|
const LENGTH = 11; |
8
|
|
|
|
9
|
|
|
const LABEL = 'CPF'; |
10
|
|
|
|
11
|
|
|
const REGEX = '/^([\d]{3})([\d]{3})([\d]{3})([\d]{2})$/'; |
12
|
|
|
|
13
|
|
|
const NUMBER_OF_DIGITS = 2; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Cpf constructor. |
17
|
|
|
* |
18
|
|
|
* @param string $number Only accept numbers |
19
|
|
|
*/ |
20
|
22 |
|
public function __construct($number) |
21
|
|
|
{ |
22
|
22 |
|
$number = preg_replace('/\D/', '', $number); |
23
|
22 |
|
parent::__construct($number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL); |
24
|
8 |
|
} |
25
|
|
|
|
26
|
11 |
|
public static function createFromString($number) |
27
|
|
|
{ |
28
|
11 |
|
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: 000.000.000-00 |
33
|
|
|
*/ |
34
|
4 |
|
public function format() |
35
|
|
|
{ |
36
|
4 |
|
return preg_replace(self::REGEX, '$1.$2.$3-$4', "{$this}"); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
12 |
|
public function calculateDigit($baseNumber) |
43
|
|
|
{ |
44
|
12 |
|
$calculator = new DigitCalculator($baseNumber); |
45
|
12 |
|
$calculator->withMultipliersInterval(2, 11); |
46
|
12 |
|
$calculator->useComplementaryInsteadOfModule(); |
47
|
12 |
|
$calculator->replaceWhen('0', 10, 11); |
48
|
12 |
|
$calculator->withModule(DigitCalculator::MODULE_11); |
49
|
12 |
|
$firstDigit = $calculator->calculate(); |
50
|
12 |
|
$calculator->addDigit($firstDigit); |
51
|
12 |
|
$secondDigit = $calculator->calculate(); |
52
|
|
|
|
53
|
12 |
|
return "{$firstDigit}{$secondDigit}"; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.