1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MÓDULO DE EMISIÓN ELECTRÓNICA F72X |
5
|
|
|
* UBL 2.1 |
6
|
|
|
* Version 1.1 |
7
|
|
|
* |
8
|
|
|
* Copyright 2018, Jaime Cruz |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace F72X\Sunat; |
12
|
|
|
|
13
|
|
|
use F72X\Sunat\Catalogo; |
14
|
|
|
use F72X\Tools\Validations; |
15
|
|
|
|
16
|
|
|
class InputValidator { |
17
|
|
|
|
18
|
|
|
private $data; |
19
|
|
|
private $type; |
20
|
|
|
private static $validations = [ |
21
|
|
|
'operationType' => [ |
22
|
|
|
'required' => true, |
23
|
|
|
'inCat' => Catalogo::CAT_FACTURA_TYPE |
24
|
|
|
], |
25
|
|
|
'voucherSeries' => [ |
26
|
|
|
'required' => true |
27
|
|
|
], |
28
|
|
|
'voucherNumber' => [ |
29
|
|
|
'required' => true |
30
|
|
|
], |
31
|
|
|
'customerDocType' => [ |
32
|
|
|
'required' => true, |
33
|
|
|
'inCat' => Catalogo::CAT_IDENT_DOCUMENT_TYPE |
34
|
|
|
], |
35
|
|
|
'customerDocNumber' => [ |
36
|
|
|
'required' => true |
37
|
|
|
], |
38
|
|
|
'customerRegName' => [ |
39
|
|
|
'required' => true |
40
|
|
|
], |
41
|
|
|
'items' => [ |
42
|
|
|
'required' => true, |
43
|
|
|
'type' => 'Array' |
44
|
|
|
] |
45
|
|
|
]; |
46
|
|
|
private $errors = []; |
47
|
|
|
|
48
|
|
|
public function __construct(array $data, $type) { |
49
|
|
|
$this->data = $data; |
50
|
|
|
$this->type = $type; |
51
|
|
|
$this->validate(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function isValid() { |
55
|
|
|
return empty($this->errors); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getErrors() { |
59
|
|
|
return $this->errors; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function validate() { |
63
|
|
|
|
64
|
|
|
foreach (self::$validations as $field => $item) { |
65
|
|
|
$defauls = [ |
66
|
|
|
'required' => false, |
67
|
|
|
'type' => null, |
68
|
|
|
'incat' => null |
69
|
|
|
]; |
70
|
|
|
$validation = array_merge($defauls, $item); |
71
|
|
|
if ($field == 'customerDocNumber') { |
72
|
|
|
$validation['type'] = $this->getDocTypeValidator(); |
73
|
|
|
} |
74
|
|
|
$this->validateItem($field, $validation, $this->data); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function validateItem($field, $validation, $data) { |
79
|
|
|
$fieldExist = isset($data[$field]); |
80
|
|
|
$fieldValue = $fieldExist ? $data[$field] : null; |
81
|
|
|
|
82
|
|
|
$required = $validation['required']; |
83
|
|
|
$catNumber = $validation['incat']; |
84
|
|
|
$type = $validation['type']; |
85
|
|
|
// Required |
86
|
|
|
if ($required && !$fieldExist) { |
87
|
|
|
$this->errors = "$field es requerido."; |
88
|
|
|
} |
89
|
|
|
if (!$fieldExist) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
// Data type |
93
|
|
|
if ($type && !Validations::{'is' . $type}($fieldValue)) { |
94
|
|
|
$this->errors = $this->getTypeErrorValidationMessage($field, $fieldValue, $type); |
95
|
|
|
} |
96
|
|
|
// In catalog |
97
|
|
|
if ($catNumber && !Catalogo::itemExist($catNumber, $fieldValue)) { |
98
|
|
|
$this->errors = "El valor $fieldValue en el campo $field no existe en el Cátalogo N° $catNumber."; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function getTypeErrorValidationMessage($field, $value, $type) { |
103
|
|
|
switch ($type) { |
104
|
|
|
case 'Array': |
105
|
|
|
return $field == 'items' ? |
106
|
|
|
'El campo items debe ser de tipo array.' : |
107
|
|
|
"Se espera que el campo $field sea un array."; |
108
|
|
|
case 'Dni': |
109
|
|
|
return "$value no es un DNI valido."; |
110
|
|
|
case 'Ruc': |
111
|
|
|
return "$value no es un DUC valido."; |
112
|
|
|
default: |
113
|
|
|
break; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function getDocTypeValidator() { |
118
|
|
|
$doctypeExist = isset($this->data['customerDocType']); |
119
|
|
|
$doctypeValue = $doctypeExist ? $this->data['customerDocType'] : null; |
|
|
|
|
120
|
|
|
if ($doctypeExist && Catalogo::itemExist(Catalogo::CAT_IDENT_DOCUMENT_TYPE, $doctypeValue)) { |
121
|
|
|
//@CAT6 |
122
|
|
|
switch ($doctypeValue) { |
123
|
|
|
case '1': return 'Dni'; |
124
|
|
|
case '6': return 'Ruc'; |
125
|
|
|
case '0': |
126
|
|
|
case '7': |
127
|
|
|
case 'A': |
128
|
|
|
case 'B': |
129
|
|
|
case 'C': |
130
|
|
|
case 'D': |
131
|
|
|
case 'E': return null; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|