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