Passed
Branch develop (5fc816)
by JAIME ELMER
01:58
created

InputValidator::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
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;
0 ignored issues
show
introduced by
The condition $doctypeExist is always true.
Loading history...
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