1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brazanation\Documents; |
4
|
|
|
|
5
|
|
|
use Brazanation\Documents\Exception\InvalidDocument as InvalidDocumentException; |
6
|
|
|
|
7
|
|
|
abstract class AbstractDocument implements DocumentInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
protected $number; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $digit; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
protected $length; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
protected $numberOfDigits; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $type; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* AbstractDocument constructor. |
36
|
|
|
* |
37
|
|
|
* @param string $number Numeric section with checker digit. |
38
|
|
|
* @param int $length Max length of document. |
39
|
|
|
* @param int $numberOfDigits Max length of checker digits. |
40
|
|
|
* @param string $type Document name/type. |
41
|
|
|
*/ |
42
|
72 |
|
public function __construct($number, $length, $numberOfDigits, $type) |
43
|
|
|
{ |
44
|
72 |
|
$this->type = (string) $type; |
45
|
72 |
|
$this->numberOfDigits = (int) $numberOfDigits; |
46
|
72 |
|
$this->length = (int) $length; |
47
|
72 |
|
$this->digit = substr($number, -($numberOfDigits)); |
48
|
72 |
|
$this->validate($number); |
49
|
26 |
|
$this->number = substr($number, 0, -($numberOfDigits)); |
50
|
26 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Check if document number is valid. |
54
|
|
|
* |
55
|
|
|
* @param string $number Numeric section with checker digit. |
56
|
|
|
* |
57
|
|
|
* @throws InvalidDocumentException when number is empty |
58
|
|
|
* @throws InvalidDocumentException when number is not valid |
59
|
|
|
*/ |
60
|
72 |
|
protected function validate($number) |
61
|
|
|
{ |
62
|
72 |
|
if (empty($number)) { |
63
|
18 |
|
throw InvalidDocumentException::notEmpty($this->type); |
64
|
|
|
} |
65
|
54 |
|
if (!$this->isValid($number)) { |
66
|
28 |
|
throw InvalidDocumentException::isNotValid($this->type, $number); |
67
|
|
|
} |
68
|
26 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Validates number is a valid. |
72
|
|
|
* |
73
|
|
|
* @param string $number Numeric section with checker digit. |
74
|
|
|
* |
75
|
|
|
* @return bool Returns true if it is a valid number, otherwise false. |
76
|
|
|
*/ |
77
|
54 |
|
protected function isValid($number) |
78
|
|
|
{ |
79
|
54 |
|
$isRepeated = preg_match("/^{$number[0]}{" . $this->length . '}$/', $number); |
80
|
|
|
|
81
|
54 |
|
if (strlen($number) != $this->length || $isRepeated) { |
82
|
10 |
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
44 |
|
$digit = $this->calculateDigit(substr($number, 0, -($this->numberOfDigits))); |
86
|
|
|
|
87
|
44 |
|
return "$digit" === "{$this->digit}"; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Handle number to string. |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
26 |
|
public function __toString() |
96
|
|
|
{ |
97
|
26 |
|
return "{$this->number}{$this->digit}"; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|