Test Failed
Push — master ( 1f5d45...b072a0 )
by JAIME ELMER
04:15
created

InputValidator::validateItem()   B

Complexity

Conditions 9
Paths 20

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 14
nc 20
nop 2
dl 0
loc 22
rs 8.0555
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\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
23
    public function __construct(array $data, $type) {
24
        $this->data = $data;
25
        $this->type = $type;
26
        $this->validate();
27
    }
28
29
    public function isValid() {
30
        return empty($this->errors);
31
    }
32
33
    public function getErrors() {
34
        return $this->errors;
35
    }
36
37
    private function validate() {
38
        $validations = $this->getValidations();
39
        foreach ($validations as $field => $item) {
40
            $defauls = [
41
                'required' => false,
42
                'type' => null,
43
                'incat' => null
44
            ];
45
            $validation = array_merge($defauls, $item);
46
            $this->validateItem($field, $validation);
47
        }
48
    }
49
50
    private function getValidations() {
51
        $validationFile = F72X::getSrcDir() . '/validations/' . $this->type . '.php';
52
        return require $validationFile;
53
    }
54
55
    private function validateItem($field, $validation) {
56
        $data = $this->data;
57
        $fieldExist = isset($data[$field]);
58
        $fieldValue = $fieldExist ? $data[$field] : null;
59
60
        $required = $validation['required'];
61
        $catNumber = $validation['incat'];
62
        $type = $validation['type'];
63
        // Required
64
        if ($required && !$fieldExist) {
65
            $this->errors[$field][] = "Requerido";
66
        }
67
        if (!$fieldExist) {
68
            return;
69
        }
70
        // Data type
71
        if ($type && !Validations::{'is' . $type}($fieldValue)) {
72
            $this->errors[$field][] = $this->getTypeErrorValidationMessage($field, $fieldValue, $type);
73
        }
74
        // In catalog
75
        if ($catNumber && !Catalogo::itemExist($catNumber, $fieldValue)) {
76
            $this->errors[$field][] = "El valor $fieldValue no existe en el Cátalogo N° $catNumber.";
77
        }
78
    }
79
80
    private function getTypeErrorValidationMessage($field, $value, $type) {
81
        switch ($type) {
82
            case 'Array':
83
                return $field == 'items' ?
84
                        'El campo items debe ser de tipo array.' : "Se espera que el campo $field sea un array.";
85
            case 'Dni':
86
                return "$value no es un DNI valido.";
87
            case 'Ruc':
88
                return "$value no es un DUC valido.";
89
            default:
90
                break;
91
        }
92
    }
93
94
}
95