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