Completed
Push — master ( 6c8478...564414 )
by Luis Andrés
01:00
created

getAttributeEcuador()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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