|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* MÓDULO DE EMISIÓN ELECTRÓNICA F72X |
|
5
|
|
|
* UBL 2.1 |
|
6
|
|
|
* Version 1.0 |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2019, Jaime Cruz |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace F72X\Sunat; |
|
12
|
|
|
|
|
13
|
|
|
use F72X\F72X; |
|
14
|
|
|
use F72X\Sunat\Catalogo; |
|
15
|
|
|
use F72X\Tools\Validations; |
|
16
|
|
|
|
|
17
|
|
|
class InputValidator { |
|
18
|
|
|
|
|
19
|
|
|
private $data; |
|
20
|
|
|
private $type; |
|
21
|
|
|
private $errors = []; |
|
22
|
|
|
private $validations = null; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(array $data, $type) { |
|
25
|
|
|
$this->data = $data; |
|
26
|
|
|
$this->type = $type; |
|
27
|
|
|
if(is_array($type)){ |
|
28
|
|
|
$this->validations = $type; |
|
29
|
|
|
} |
|
30
|
|
|
$this->validate(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function isValid() { |
|
34
|
|
|
return empty($this->errors); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getErrors() { |
|
38
|
|
|
return $this->errors; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
private function validate() { |
|
42
|
|
|
if(!$this->validations){ |
|
43
|
|
|
$this->validations = $this->getValidations(); |
|
44
|
|
|
} |
|
45
|
|
|
foreach ($this->validations as $field => $item) { |
|
46
|
|
|
$defauls = [ |
|
47
|
|
|
'required' => false, |
|
48
|
|
|
'type' => null, |
|
49
|
|
|
'incat' => null |
|
50
|
|
|
]; |
|
51
|
|
|
$validation = array_merge($defauls, $item); |
|
52
|
|
|
$this->validateItem($field, $validation); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function getValidations() { |
|
57
|
|
|
$validationFile = F72X::getSrcDir() . '/validations/' . $this->type . '.php'; |
|
58
|
|
|
return require $validationFile; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function validateItem($field, $validation) { |
|
62
|
|
|
$data = $this->data; |
|
63
|
|
|
$fieldExist = isset($data[$field]); |
|
64
|
|
|
$fieldValue = $fieldExist ? $data[$field] : null; |
|
65
|
|
|
|
|
66
|
|
|
$required = $validation['required']; |
|
67
|
|
|
$catNumber = $validation['incat']; |
|
68
|
|
|
$type = $validation['type']; |
|
69
|
|
|
// Required |
|
70
|
|
|
if ($required && !$fieldExist) { |
|
71
|
|
|
$this->errors[$field][] = "Requerido"; |
|
72
|
|
|
} |
|
73
|
|
|
if (!$fieldExist) { |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
// Data type |
|
77
|
|
|
if ($type && !Validations::{'is' . $type}($fieldValue)) { |
|
78
|
|
|
$this->errors[$field][] = $this->getTypeErrorValidationMessage($field, $fieldValue, $type); |
|
79
|
|
|
} |
|
80
|
|
|
// In catalog |
|
81
|
|
|
if ($catNumber && !Catalogo::itemExist($catNumber, $fieldValue)) { |
|
82
|
|
|
$this->errors[$field][] = "El valor $fieldValue no existe en el Cátalogo N° $catNumber."; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private function getTypeErrorValidationMessage($field, $value, $type) { |
|
87
|
|
|
switch ($type) { |
|
88
|
|
|
case 'Array': |
|
89
|
|
|
return $field == 'items' ? |
|
90
|
|
|
'El campo items debe ser de tipo array.' : "Se espera que el campo $field sea un array."; |
|
91
|
|
|
case 'Dni': |
|
92
|
|
|
return "$value no es un DNI valido."; |
|
93
|
|
|
case 'Ruc': |
|
94
|
|
|
return "$value no es un DUC valido."; |
|
95
|
|
|
default: |
|
96
|
|
|
break; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function throwExc() { |
|
101
|
|
|
throw new \F72X\Exception\InvalidInputException($this->getErrosString(), $this->getErrors()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getErrosString() { |
|
105
|
|
|
$errs = []; |
|
106
|
|
|
foreach ($this->errors as $field => $fErrors) { |
|
107
|
|
|
$errs[] = $field . ': (' . implode(',', $fErrors) . ')'; |
|
108
|
|
|
} |
|
109
|
|
|
return implode(',', $errs); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|