1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Luilliarcec\LaravelEcuadorIdentification\Validations; |
5
|
|
|
|
6
|
|
|
use Exception; |
7
|
|
|
use Illuminate\Validation\Validator; |
8
|
|
|
use Luilliarcec\LaravelEcuadorIdentification\Support\EcuadorIdentification; |
9
|
|
|
|
10
|
|
|
class EcuadorIdentificationValidator extends Validator |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Validation types |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $types = [ |
18
|
|
|
'final_customer' => 'validateFinalConsumer', |
19
|
|
|
'personal_identification' => 'validatePersonalIdentification', |
20
|
|
|
'natural_ruc' => 'validateNaturalRuc', |
21
|
|
|
'public_ruc' => 'validatePublicRuc', |
22
|
|
|
'private_ruc' => 'validatePrivateRuc', |
23
|
|
|
'ruc' => 'validateRuc', |
24
|
|
|
'is_natural_person' => 'validateIsNaturalPersons', |
25
|
|
|
'is_juridical_person' => 'validateIsJuridicalPersons', |
26
|
|
|
'all_identifications' => 'validateAllTypeIdentification', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Validate that the Ecuadorian identification is valid |
31
|
|
|
* |
32
|
|
|
* @param $attribute |
33
|
|
|
* @param $value |
34
|
|
|
* @param $parameters |
35
|
|
|
* @return bool |
36
|
|
|
* @throws \Exception |
37
|
|
|
*/ |
38
|
|
|
public function validateEcuador($attribute, $value, $parameters) |
39
|
|
|
{ |
40
|
|
|
$lowerRule = explode(':', strtolower($this->currentRule))[0]; |
41
|
|
|
|
42
|
|
|
try { |
43
|
|
|
$this->setCustomMessages([$lowerRule => $this->getMessageEcuador($attribute, $lowerRule)]); |
44
|
|
|
|
45
|
|
|
return $this->passesEcuador($parameters, $value); |
46
|
|
|
} catch (Exception $exception) { |
47
|
|
|
throw new Exception("Custom validation rule {$lowerRule}:{$parameters[0]} does not exist"); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the translated message or the default message. |
53
|
|
|
* |
54
|
|
|
* @param $attribute |
55
|
|
|
* @param $rule |
56
|
|
|
* @return mixed|string |
57
|
|
|
*/ |
58
|
|
|
protected function getMessageEcuador($attribute, $rule) |
59
|
|
|
{ |
60
|
|
|
$key = 'validation.' . $rule; |
61
|
|
|
|
62
|
|
|
return $this->getTranslator()->get($key) != $key ? |
63
|
|
|
$this->getTranslator()->get($key) : |
64
|
|
|
"The {$this->getAttributeEcuador($attribute)} field does not have the corresponding country format. (Ecuador)"; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the translated attribute or the default attribute. |
69
|
|
|
* |
70
|
|
|
* @param $attribute |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
protected function getAttributeEcuador($attribute) |
74
|
|
|
{ |
75
|
|
|
$key = 'validation.attributes.' . $attribute; |
76
|
|
|
|
77
|
|
|
return $this->getTranslator()->get($key) != $key ? |
78
|
|
|
$this->getTranslator()->get($key) : |
79
|
|
|
$attribute; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Determine if the data passes the validation rules. |
84
|
|
|
* |
85
|
|
|
* @param $parameters |
86
|
|
|
* @param $value |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
protected function passesEcuador($parameters, $value) |
90
|
|
|
{ |
91
|
|
|
$validator = new EcuadorIdentification(); |
92
|
|
|
|
93
|
|
|
return $validator->{$this->types[$parameters[0]]}($value) != null; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|