1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Luilliarcec\LaravelEcuadorIdentification\Support; |
4
|
|
|
|
5
|
|
|
use Luilliarcec\LaravelEcuadorIdentification\Exceptions\EcuadorIdentificationException; |
6
|
|
|
use Luilliarcec\LaravelEcuadorIdentification\Support\Identifications\FinalCustomer; |
7
|
|
|
use Luilliarcec\LaravelEcuadorIdentification\Support\Identifications\NaturalRuc; |
8
|
|
|
use Luilliarcec\LaravelEcuadorIdentification\Support\Identifications\PersonalIdentification; |
9
|
|
|
use Luilliarcec\LaravelEcuadorIdentification\Support\Identifications\PrivateRuc; |
10
|
|
|
use Luilliarcec\LaravelEcuadorIdentification\Support\Identifications\PublicRuc; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class to validate Ecuadorian identity card, Natural ruc, Private ruc and Public ruc |
14
|
|
|
* |
15
|
|
|
* @link https://www.sri.gob.ec/web/guest/RUC#%C2%BFc%C3%B3mo-se |
16
|
|
|
* @link http://www.sri.gob.ec/DocumentosAlfrescoPortlet/descargar/1ee224e6-b84b-4a8f-8127-59f8cd99ae58/LOGARITMO_VALIDA_RUC.docx |
17
|
|
|
* @package Luilliarcec\LaravelEcuadorIdentification\Support |
18
|
|
|
*/ |
19
|
|
|
class EcuadorIdentification |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Error encapsulator variable |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $error; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Set Error |
30
|
|
|
* |
31
|
|
|
* @param string $error |
32
|
|
|
*/ |
33
|
|
|
protected function setError(string $error): void |
34
|
|
|
{ |
35
|
|
|
$this->error = $error; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get Error |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getError(): string |
44
|
|
|
{ |
45
|
|
|
return $this->error; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Validates the Ecuadorian Identification Card |
50
|
|
|
* |
51
|
|
|
* @param string $number Number of Identification Card |
52
|
|
|
* @return string|null |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function validatePersonalIdentification($number) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$identification = new PersonalIdentification(); |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
return $identification->validate($number); |
60
|
|
|
} catch (EcuadorIdentificationException $e) { |
61
|
|
|
$this->setError($e->getMessage()); |
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Validates the Ecuadorian RUC of Natural Person |
68
|
|
|
* |
69
|
|
|
* @param string $number Number of RUC Natural Person |
70
|
|
|
* @return string|null |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function validateNaturalPersonRuc($number) |
73
|
|
|
{ |
74
|
|
|
$identification = new NaturalRuc(); |
75
|
|
|
|
76
|
|
|
try { |
77
|
|
|
return $identification->validate($number); |
78
|
|
|
} catch (EcuadorIdentificationException $e) { |
79
|
|
|
$this->setError($e->getMessage()); |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Validates the Ecuadorian RUC of Private Companies |
86
|
|
|
* |
87
|
|
|
* @param string $number Number of RUC Private Companies |
88
|
|
|
* @return string|null |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function validatePrivateCompanyRuc($number) |
91
|
|
|
{ |
92
|
|
|
$identification = new PrivateRuc(); |
93
|
|
|
|
94
|
|
|
try { |
95
|
|
|
return $identification->validate($number); |
96
|
|
|
} catch (EcuadorIdentificationException $e) { |
97
|
|
|
$this->setError($e->getMessage()); |
98
|
|
|
return null; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Validates the Ecuadorian RUC of Public Companies |
104
|
|
|
* |
105
|
|
|
* @param string $number Number of RUC Public Companies |
106
|
|
|
* @return string|null |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function validatePublicCompanyRuc($number) |
109
|
|
|
{ |
110
|
|
|
$identification = new PublicRuc(); |
111
|
|
|
|
112
|
|
|
try { |
113
|
|
|
return $identification->validate($number); |
114
|
|
|
} catch (EcuadorIdentificationException $e) { |
115
|
|
|
$this->setError($e->getMessage()); |
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Validates the Ecuadorian Final Consumer |
122
|
|
|
* |
123
|
|
|
* @param $number |
124
|
|
|
* @return string|null |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function validateFinalConsumer($number) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$identification = new FinalCustomer(); |
129
|
|
|
|
130
|
|
|
try { |
131
|
|
|
return $identification->validate($number); |
132
|
|
|
} catch (EcuadorIdentificationException $e) { |
133
|
|
|
$this->setError($e->getMessage()); |
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Validate that the number belongs to natural persons. |
140
|
|
|
* |
141
|
|
|
* @param $number |
142
|
|
|
* @return string|null |
143
|
|
|
*/ |
144
|
|
|
public function validateIsNaturalPersons($number) |
145
|
|
|
{ |
146
|
|
|
return $this->validatePersonalIdentification($number) !== null ? |
147
|
|
|
$this->validatePersonalIdentification($number) : $this->validateNaturalPersonRuc($number); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Validate that the number belongs to juridical persons. |
152
|
|
|
* |
153
|
|
|
* @param $number |
154
|
|
|
* @return string|null |
155
|
|
|
*/ |
156
|
|
|
public function validateIsJuridicalPersons($number) |
157
|
|
|
{ |
158
|
|
|
return $this->validatePrivateCompanyRuc($number) !== null ? |
159
|
|
|
$this->validatePrivateCompanyRuc($number) : $this->validatePublicCompanyRuc($number); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Validate the number with all types of documents. |
164
|
|
|
* |
165
|
|
|
* @param $number |
166
|
|
|
* @return string|null |
167
|
|
|
*/ |
168
|
|
|
public function validateAllIdentificatons($number) |
169
|
|
|
{ |
170
|
|
|
if (($result = $this->validateFinalConsumer($number)) !== null) { |
171
|
|
|
return $result; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (($result = $this->validatePersonalIdentification($number)) !== null) { |
175
|
|
|
return $result; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if (($result = $this->validateNaturalPersonRuc($number)) !== null) { |
179
|
|
|
return $result; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if (($result = $this->validatePrivateCompanyRuc($number)) !== null) { |
183
|
|
|
return $result; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $this->validatePublicCompanyRuc($number); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.