1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Luilliarcec\LaravelEcuadorIdentification\Validations; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use BadMethodCallException; |
7
|
|
|
use Luilliarcec\LaravelEcuadorIdentification\Support\Identifications\FinalCustomer; |
8
|
|
|
use Luilliarcec\LaravelEcuadorIdentification\Support\Identifications\NaturalRuc; |
9
|
|
|
use Luilliarcec\LaravelEcuadorIdentification\Support\Identifications\PersonalIdentification; |
10
|
|
|
use Luilliarcec\LaravelEcuadorIdentification\Support\Identifications\PrivateRuc; |
11
|
|
|
use Luilliarcec\LaravelEcuadorIdentification\Support\Identifications\PublicRuc; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class to validate Ecuadorian identity card, Natural ruc, Private ruc and Public ruc |
15
|
|
|
* |
16
|
|
|
* @link https://www.sri.gob.ec/web/guest/RUC#%C2%BFc%C3%B3mo-se |
17
|
|
|
* @link http://www.sri.gob.ec/DocumentosAlfrescoPortlet/descargar/1ee224e6-b84b-4a8f-8127-59f8cd99ae58/LOGARITMO_VALIDA_RUC.docx |
18
|
|
|
* @package Luilliarcec\LaravelEcuadorIdentification\Support |
19
|
|
|
*/ |
20
|
|
|
class EcuadorIdentification |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Error encapsulator variable |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $error; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Validation types |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private const TYPES = [ |
35
|
|
|
'final_customer' => 'validateFinalConsumer', |
36
|
|
|
'personal_identification' => 'validatePersonalIdentification', |
37
|
|
|
'natural_ruc' => 'validateNaturalRuc', |
38
|
|
|
'public_ruc' => 'validatePublicRuc', |
39
|
|
|
'private_ruc' => 'validatePrivateRuc', |
40
|
|
|
'ruc' => 'validateRuc', |
41
|
|
|
'is_natural_person' => 'validateIsNaturalPersons', |
42
|
|
|
'is_juridical_person' => 'validateIsJuridicalPersons', |
43
|
|
|
'all_identifications' => 'validateAllTypeIdentification', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set Error |
48
|
|
|
* |
49
|
|
|
* @param string|null $error |
50
|
|
|
*/ |
51
|
|
|
protected function setError(?string $error): void |
52
|
|
|
{ |
53
|
|
|
$this->error = $error; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get Error |
58
|
|
|
* |
59
|
|
|
* @return string|null |
60
|
|
|
*/ |
61
|
|
|
public function getError() |
62
|
|
|
{ |
63
|
|
|
return $this->error; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Validate that the Ecuadorian identification is valid |
68
|
|
|
* |
69
|
|
|
* @param $attribute |
70
|
|
|
* @param $value |
71
|
|
|
* @param $parameters |
72
|
|
|
* @param $validator |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function validate($attribute, $value, $parameters, $validator) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
try { |
78
|
|
|
return !is_null($this->{self::TYPES[$parameters[0]]}($value)); |
79
|
|
|
} catch (Exception $exception) { |
80
|
|
|
throw new BadMethodCallException(sprintf( |
81
|
|
|
'Method %s::%s does not exist.', static::class, $parameters[0] |
82
|
|
|
)); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Validates the Ecuadorian Final Consumer |
88
|
|
|
* |
89
|
|
|
* @param string $identification_number Final Consumer Identification |
90
|
|
|
* @return string|null |
91
|
|
|
*/ |
92
|
|
|
public function validateFinalConsumer(string $identification_number) |
93
|
|
|
{ |
94
|
|
|
$this->setError(null); |
95
|
|
|
|
96
|
|
|
try { |
97
|
|
|
return (new FinalCustomer())->validate($identification_number); |
98
|
|
|
} catch (Exception $e) { |
99
|
|
|
$this->setError($e->getMessage()); |
100
|
|
|
return null; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Validates the Ecuadorian Identification Card |
106
|
|
|
* |
107
|
|
|
* @param string $identification_number Number of Identification Card |
108
|
|
|
* @return string|null |
109
|
|
|
*/ |
110
|
|
|
public function validatePersonalIdentification(string $identification_number) |
111
|
|
|
{ |
112
|
|
|
$this->setError(null); |
113
|
|
|
|
114
|
|
|
try { |
115
|
|
|
return (new PersonalIdentification())->validate($identification_number); |
116
|
|
|
} catch (Exception $e) { |
117
|
|
|
$this->setError($e->getMessage()); |
118
|
|
|
return null; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Validates the Ecuadorian RUC of Natural Person |
124
|
|
|
* |
125
|
|
|
* @param string $identification_number Number of RUC Natural Person |
126
|
|
|
* @return string|null |
127
|
|
|
*/ |
128
|
|
|
public function validateNaturalRuc(string $identification_number) |
129
|
|
|
{ |
130
|
|
|
$this->setError(null); |
131
|
|
|
|
132
|
|
|
try { |
133
|
|
|
return (new NaturalRuc())->validate($identification_number); |
134
|
|
|
} catch (Exception $e) { |
135
|
|
|
$this->setError($e->getMessage()); |
136
|
|
|
return null; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Validates the Ecuadorian RUC of Public Companies |
142
|
|
|
* |
143
|
|
|
* @param string $identification_number Number of RUC Public Companies |
144
|
|
|
* @return string|null |
145
|
|
|
*/ |
146
|
|
|
public function validatePublicRuc(string $identification_number) |
147
|
|
|
{ |
148
|
|
|
$this->setError(null); |
149
|
|
|
|
150
|
|
|
try { |
151
|
|
|
return (new PublicRuc())->validate($identification_number); |
152
|
|
|
} catch (Exception $e) { |
153
|
|
|
$this->setError($e->getMessage()); |
154
|
|
|
return null; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Validates the Ecuadorian RUC of Private Companies |
160
|
|
|
* |
161
|
|
|
* @param string $identification_number Number of RUC Private Companies |
162
|
|
|
* @return string|null |
163
|
|
|
*/ |
164
|
|
|
public function validatePrivateRuc(string $identification_number) |
165
|
|
|
{ |
166
|
|
|
$this->setError(null); |
167
|
|
|
|
168
|
|
|
try { |
169
|
|
|
return (new PrivateRuc())->validate($identification_number); |
170
|
|
|
} catch (Exception $e) { |
171
|
|
|
$this->setError($e->getMessage()); |
172
|
|
|
return null; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Validates the Ecuadorian Ruc's |
178
|
|
|
* |
179
|
|
|
* @param string $identification_number Number of RUC |
180
|
|
|
* @return string|null |
181
|
|
|
*/ |
182
|
|
|
public function validateRuc(string $identification_number) |
183
|
|
|
{ |
184
|
|
|
if (($result = $this->validatePrivateRuc($identification_number)) !== null) { |
185
|
|
|
return $result; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if (($result = $this->validatePublicRuc($identification_number)) !== null) { |
189
|
|
|
return $result; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $this->validateNaturalRuc($identification_number); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Validate that the number belongs to natural persons. |
197
|
|
|
* |
198
|
|
|
* @param string $identification_number Number of identification |
199
|
|
|
* @return string|null |
200
|
|
|
*/ |
201
|
|
|
public function validateIsNaturalPersons(string $identification_number) |
202
|
|
|
{ |
203
|
|
|
return $this->validatePersonalIdentification($identification_number) ?: $this->validateNaturalRuc($identification_number); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Validate that the number belongs to juridical persons. |
208
|
|
|
* |
209
|
|
|
* @param string $identification_number Number of identification |
210
|
|
|
* @return string|null |
211
|
|
|
*/ |
212
|
|
|
public function validateIsJuridicalPersons(string $identification_number) |
213
|
|
|
{ |
214
|
|
|
return $this->validatePrivateRuc($identification_number) ?: $this->validatePublicRuc($identification_number); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Validate the number with all types of documents. |
219
|
|
|
* |
220
|
|
|
* @param string $identification_number Number of identification |
221
|
|
|
* @return string|null |
222
|
|
|
*/ |
223
|
|
|
public function validateAllTypeIdentification(string $identification_number) |
224
|
|
|
{ |
225
|
|
|
if (($result = $this->validateFinalConsumer($identification_number)) !== null) { |
226
|
|
|
return $result; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
if (($result = $this->validateRuc($identification_number)) !== null) { |
230
|
|
|
return $result; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $this->validatePersonalIdentification($identification_number); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.