Completed
Push — master ( 405d68...09c4bb )
by Beñat
02:21
created

TaxIdNumber::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Shared Kernel library.
5
 *
6
 * Copyright (c) 2016-present LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace LIN3S\SharedKernel\Domain\Model\TaxIdNumber;
15
16
use NifValidator\NifValidator;
17
18
/**
19
 * @author Beñat Espiña <[email protected]>
20
 */
21
class TaxIdNumber
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
22
{
23
    private $tin;
24
25
    private function __construct(string $tin)
26
    {
27
        $this->tin = $tin;
28
    }
29
30
    public static function fromSpain(string $nif) : self
31
    {
32
        if (!NifValidator::isValid($nif)) {
33
            throw TaxIdNumberInvalidException::fromSpain($nif);
34
        }
35
36
        return new self($nif);
37
    }
38
39
    public static function fromSpanishNie(string $nie) : self
40
    {
41
        if (!NifValidator::isValidNie($nie)) {
42
            throw TaxIdNumberInvalidException::fromSpanishNie($nie);
43
        }
44
45
        return new self($nie);
46
    }
47
48
    public static function fromSpanishCif(string $cif) : self
49
    {
50
        if (!NifValidator::isValidCif($cif)) {
51
            throw TaxIdNumberInvalidException::fromSpanishCif($cif);
52
        }
53
54
        return new self($cif);
55
    }
56
57
    public static function fromSpanishDni(string $dni) : self
58
    {
59
        if (!NifValidator::isValidDni($dni)) {
60
            throw TaxIdNumberInvalidException::fromSpanishDni($dni);
61
        }
62
63
        return new self($dni);
64
    }
65
66
    public function number() : string
67
    {
68
        return $this->tin;
69
    }
70
71
    public function equals(TaxIdNumber $tin) : bool
72
    {
73
        return $this->tin === $tin->number();
74
    }
75
76
    public function __toString() : string
77
    {
78
        return (string) $this->tin;
79
    }
80
}
81