1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Olssonm\IdentityNumber; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Handles the identity number formatting |
7
|
|
|
*/ |
8
|
|
|
class IdentityNumberFormatter |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Set value |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $value; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Number of characters |
20
|
|
|
* |
21
|
|
|
* @var integer |
22
|
|
|
*/ |
23
|
|
|
private $characters; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* If hyphens is allowed |
27
|
|
|
* |
28
|
|
|
* @var boolean |
29
|
|
|
*/ |
30
|
|
|
private $useHyphen; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* What type of hyphen to use ("-"/+) |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $typeHyphen = '-'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Format the personal identity number to make validation easier |
41
|
|
|
* |
42
|
|
|
* @param string $value the input value |
43
|
|
|
* @param int $characters number of characters |
44
|
|
|
* @param bool $useHyphen if hyphens should be used |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function __construct($value, $characters, $useHyphen = false) |
48
|
|
|
{ |
49
|
|
|
$this->value = $value; |
50
|
|
|
$this->characters = $characters; |
51
|
|
|
$this->useHyphen = $useHyphen; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Clean the value-string from unwanted characters |
56
|
|
|
* |
57
|
|
|
* @return \Olssonm\IdentityNumber\IdentityNumberFormatter |
58
|
|
|
*/ |
59
|
|
|
public function clean() |
60
|
|
|
{ |
61
|
|
|
// Clean string from invalid values |
62
|
|
|
$pattern = '0123456789-+'; |
63
|
|
|
$this->value = preg_replace("/[^" . preg_quote($pattern, "/") . "]/", '', $this->value); |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Retrieve the formatted personal identity number |
70
|
|
|
* |
71
|
|
|
* @return string the personal identity number |
72
|
|
|
*/ |
73
|
|
|
public function getFormatted() |
74
|
|
|
{ |
75
|
|
|
$value = $this->value; |
76
|
|
|
$characters = $this->characters; |
77
|
|
|
$useHyphen = $this->useHyphen; |
78
|
|
|
|
79
|
|
|
// Check if "+" is present, then use it as a hyphen |
80
|
|
|
if (strpos($value, '+') !== false) { |
81
|
|
|
$this->typeHyphen = '+'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Remove hyphen |
85
|
|
|
$value = str_replace(['-', '+'], '', $value); |
86
|
|
|
|
87
|
|
|
if (strlen($value) != 12 && strlen($value) != 10) { |
88
|
|
|
return false; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($characters == 12 && strlen($value) == 10) { |
92
|
|
|
$value = 19 . $value; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($characters == 10 && strlen($value) == 12) { |
96
|
|
|
$newNumber = null; |
97
|
|
|
for ($i = 0; $i < strlen($value); $i++) { |
98
|
|
|
if ($i > 1) { |
99
|
|
|
$newNumber .= $value[$i]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
$value = $newNumber; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Insert hyphen if you need to |
106
|
|
|
if ($useHyphen == true) { |
|
|
|
|
107
|
|
|
$newNumber = null; |
108
|
|
|
for ($i = 0; $i < strlen($value); $i++) { |
109
|
|
|
$newNumber .= $value[$i]; |
110
|
|
|
if ($i == strlen($value) - 5) { |
111
|
|
|
$newNumber .= $this->typeHyphen; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
$value = $newNumber; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $value; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|