Completed
Push — master ( 2aab01...227846 )
by Antonio Oertel
04:04
created

PisPasep::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Brazanation\Documents;
4
5
use Brazanation\Documents\Exception\InvalidArgument;
6
7
final class PisPasep implements DocumentInterface
8
{
9
    const REGEX = '/^([\d]{3})([\d]{5})([\d]{2})([\d]{1})$/';
10
11
    const FORMAT_REGEX = '/^[\d]{3}\.[\d]{5}\.[\d]{2}-[\d]{1}$/';
12
13
    private $pispasep;
14
15
    protected $mask = '000.00000.00-0';
16
17
    /**
18
     * PisPasep constructor.
19
     *
20
     * @param $pispasep
21
     */
22 9
    public function __construct($pispasep)
23
    {
24 9
        $this->validate($pispasep);
25 2
        $this->pispasep = $pispasep;
26 2
    }
27
28 9
    private function validate($pispasep)
29
    {
30 9
        if (empty($pispasep)) {
31 3
            throw InvalidArgument::notEmpty('PisPasep');
32
        }
33 6
        if (!$this->isValidCV($pispasep)) {
34 4
            throw InvalidArgument::isNotValidPispasep($pispasep);
35
        }
36 2
    }
37
38 6
    private function isValidCV($pispasep)
39
    {
40 6
        $c = preg_replace('/\D/', '', $pispasep);
41 6
        if (strlen($c) != 11 || preg_match("/^{$c[0]}{11}$/", $c)) {
42 2
            return false;
43
        }
44
45 4
        return (new Modulo11(1, 9))->validate($pispasep);
46
    }
47
48
    /**
49
     * Formats PIS/PASEP number
50
     *
51
     * @return string Returns formatted number, such as: 00.00000.00-0
52
     */
53 2
    public function format()
54
    {
55 2
        return preg_replace(static::REGEX, '$1.$2.$3-$4', $this->pispasep);
56
    }
57
58
    public function __toString()
59
    {
60
        return $this->pispasep;
61
    }
62
}