Passed
Push — master ( 074426...6dac43 )
by Luis Andrés
23:20
created

EcuadorIdentification::validateIsJuridicalPerson()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
            return !is_null($this->{$method}($value));
65
        } catch (Exception $exception) {
66
            throw new BadMethodCallException(sprintf(
67
                'Method %s::%s does not exist.', static::class, $parameters[0]
68
            ));
69
        }
70
    }
71
72
    /**
73
     * Validates the Ecuadorian Final Consumer
74
     *
75
     * @param string $identification_number Final Consumer Identification
76
     * @return string|null
77
     */
78
    public function validateFinalConsumer(string $identification_number)
79
    {
80
        $this->setError(null);
81
82
        try {
83
            return (new FinalCustomer())->validate($identification_number);
84
        } catch (Exception $e) {
85
            $this->setError($e->getMessage());
86
            return null;
87
        }
88
    }
89
90
    /**
91
     * Validates the Ecuadorian Identification Card
92
     *
93
     * @param string $identification_number Number of Identification Card
94
     * @return string|null
95
     */
96
    public function validatePersonalIdentification(string $identification_number)
97
    {
98
        $this->setError(null);
99
100
        try {
101
            return (new PersonalIdentification())->validate($identification_number);
102
        } catch (Exception $e) {
103
            $this->setError($e->getMessage());
104
            return null;
105
        }
106
    }
107
108
    /**
109
     * Validates the Ecuadorian RUC of Natural Person
110
     *
111
     * @param string $identification_number Number of RUC Natural Person
112
     * @return string|null
113
     */
114
    public function validateNaturalRuc(string $identification_number)
115
    {
116
        $this->setError(null);
117
118
        try {
119
            return (new NaturalRuc())->validate($identification_number);
120
        } catch (Exception $e) {
121
            $this->setError($e->getMessage());
122
            return null;
123
        }
124
    }
125
126
    /**
127
     * Validates the Ecuadorian RUC of Public Companies
128
     *
129
     * @param string $identification_number Number of RUC Public Companies
130
     * @return string|null
131
     */
132
    public function validatePublicRuc(string $identification_number)
133
    {
134
        $this->setError(null);
135
136
        try {
137
            return (new PublicRuc())->validate($identification_number);
138
        } catch (Exception $e) {
139
            $this->setError($e->getMessage());
140
            return null;
141
        }
142
    }
143
144
    /**
145
     * Validates the Ecuadorian RUC of Private Companies
146
     *
147
     * @param string $identification_number Number of RUC Private Companies
148
     * @return string|null
149
     */
150
    public function validatePrivateRuc(string $identification_number)
151
    {
152
        $this->setError(null);
153
154
        try {
155
            return (new PrivateRuc())->validate($identification_number);
156
        } catch (Exception $e) {
157
            $this->setError($e->getMessage());
158
            return null;
159
        }
160
    }
161
162
    /**
163
     * Validates the Ecuadorian Ruc's
164
     *
165
     * @param string $identification_number Number of RUC
166
     * @return string|null
167
     */
168
    public function validateRuc(string $identification_number)
169
    {
170
        if (($result = $this->validatePrivateRuc($identification_number)) !== null) {
171
            return $result;
172
        }
173
174
        if (($result = $this->validatePublicRuc($identification_number)) !== null) {
175
            return $result;
176
        }
177
178
        return $this->validateNaturalRuc($identification_number);
179
    }
180
181
    /**
182
     * Validate that the number belongs to natural persons.
183
     *
184
     * @param string $identification_number Number of identification
185
     * @return string|null
186
     */
187
    public function validateIsNaturalPerson(string $identification_number)
188
    {
189
        return $this->validatePersonalIdentification($identification_number) ?: $this->validateNaturalRuc($identification_number);
190
    }
191
192
    /**
193
     * Validate that the number belongs to juridical persons.
194
     *
195
     * @param string $identification_number Number of identification
196
     * @return string|null
197
     */
198
    public function validateIsJuridicalPerson(string $identification_number)
199
    {
200
        return $this->validatePrivateRuc($identification_number) ?: $this->validatePublicRuc($identification_number);
201
    }
202
203
    /**
204
     * Validate the number with all types of documents.
205
     *
206
     * @param string $identification_number Number of identification
207
     * @return string|null
208
     */
209
    public function validateAllIdentifications(string $identification_number)
210
    {
211
        if (($result = $this->validateFinalConsumer($identification_number)) !== null) {
212
            return $result;
213
        }
214
215
        if (($result = $this->validateRuc($identification_number)) !== null) {
216
            return $result;
217
        }
218
219
        return $this->validatePersonalIdentification($identification_number);
220
    }
221
}
222