Skip to content

Commit f60f71a

Browse files
add CNS and CNS Temporary document
1 parent e4b1e49 commit f60f71a

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

src/Cns.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Brazanation\Documents;
4+
5+
use Brazanation\Documents\Cns\CnsCalculator;
6+
use Brazanation\Documents\Cns\Temporary;
7+
8+
final class Cns extends AbstractDocument
9+
{
10+
const LENGTH = 15;
11+
12+
const LABEL = 'CNS';
13+
14+
const REGEX = '/^([\d]{3})([\d]{4})([\d]{4})([\d]{4})$/';
15+
16+
const FORMAT = '$1 $2 $3 $4';
17+
18+
const DIGIT_COUNT = 1;
19+
20+
/**
21+
* @var DigitCalculable
22+
*/
23+
private $calculator;
24+
25+
/**
26+
* CNS constructor.
27+
*
28+
* @param string $number
29+
*/
30+
public function __construct($number)
31+
{
32+
$number = preg_replace('/\D/', '', $number);
33+
$this->defineCalculator($number);
34+
parent::__construct($number, self::LENGTH, self::DIGIT_COUNT, self::LABEL);
35+
}
36+
37+
public function defineCalculator($number)
38+
{
39+
$this->calculator = new CnsCalculator();
40+
if (in_array(substr($number, 0, 1), [7, 8, 9])) {
41+
$this->calculator = new Temporary($number);
42+
}
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function format()
49+
{
50+
return preg_replace(self::REGEX, self::FORMAT, "{$this}");
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function calculateDigit($baseNumber)
57+
{
58+
$digit = $this->calculator->calculateDigit($baseNumber);
59+
60+
return "{$digit}";
61+
}
62+
}

src/Cns/CnsCalculator.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Brazanation\Documents\Cns;
4+
5+
use Brazanation\Documents\DigitCalculable;
6+
use Brazanation\Documents\DigitCalculator;
7+
8+
final class CnsCalculator implements DigitCalculable
9+
{
10+
/**
11+
* {@inheritdoc}
12+
*/
13+
public function calculateDigit($baseNumber)
14+
{
15+
$pis = substr($baseNumber, 0, 11);
16+
17+
$calculator = new DigitCalculator($pis);
18+
$calculator->useComplementaryInsteadOfModule();
19+
$calculator->replaceWhen('8', 10);
20+
$calculator->replaceWhen('0', 11);
21+
$calculator->withMultipliersInterval(5, 15);
22+
23+
$digit = $calculator->calculate();
24+
25+
return "{$digit}";
26+
}
27+
}

src/Cns/Temporary.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Brazanation\Documents\Cns;
4+
5+
use Brazanation\Documents\DigitCalculable;
6+
use Brazanation\Documents\DigitCalculator;
7+
8+
class Temporary implements DigitCalculable
9+
{
10+
/**
11+
* {@inheritdoc}
12+
*/
13+
public function calculateDigit($baseNumber)
14+
{
15+
$calculator = new DigitCalculator($baseNumber);
16+
$calculator->withMultipliersInterval(1, 15);
17+
18+
$digit = $calculator->calculate();
19+
20+
return "{$digit}";
21+
}
22+
}

tests/CnsTest.php

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Brazanation\Documents\Tests;
4+
5+
use Brazanation\Documents\Cns;
6+
7+
class CnsTest extends DocumentTestCase
8+
{
9+
public function createDocument($number)
10+
{
11+
return new Cns($number);
12+
}
13+
14+
public function provideValidNumbers()
15+
{
16+
return [
17+
['242912018460005'],
18+
['202652786480004'],
19+
['130660713310009'],
20+
['161406744690005'],
21+
['131638083510018'],
22+
['153146911870018'],
23+
['126556169380018'],
24+
['112737911470018'],
25+
];
26+
}
27+
28+
public function provideValidNumbersAndExpectedFormat()
29+
{
30+
return [
31+
['242912018460005', '242 9120 1846 0005'],
32+
];
33+
}
34+
35+
public function provideEmptyData()
36+
{
37+
return [
38+
[Cns::LABEL, ''],
39+
[Cns::LABEL, null],
40+
[Cns::LABEL, 0],
41+
];
42+
}
43+
44+
public function provideInvalidNumber()
45+
{
46+
return [
47+
[Cns::LABEL, 1],
48+
[Cns::LABEL, '123123232323'],
49+
[Cns::LABEL, '861238979874'],
50+
];
51+
}
52+
}

0 commit comments

Comments
 (0)