Passed
Push — main ( f2df15...8d7bb5 )
by Osvaldo
09:13
created

Donde::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
namespace src\pdodatabase\elementos;
3
4
use Exception;
5
6
class Donde
7
{
8
    private $_donde;
9
    private $_datos;
10
    private $_operadoresValidos = [
11
        '=',
12
        '>',
13
        '<',
14
        '>=',
15
        '<=',
16
        '<>',
17
        '!=',
18
        '!<',
19
        '!>',
20
    ];
21
22
    public function __construct(array $Donde)
23
    {
24
        $this->_donde = $this->setDonde($Donde);
25
    }
26
27
    public function donde(): string
28
    {
29
        return $this->_donde;
30
    }
31
32
    public function datos(): array
33
    {
34
        return $this->_datos;
35
    }
36
37
    private function setDonde(array $columnas): string
38
    {
39
        if(count($columnas) !== 3)
40
        {
41
            throw new Exception("Elementos faltantes o sobrantes en la sentencia WHERE", 1);
42
        }
43
        
44
        $this->alfaNumerico($columnas[0]);
45
        $this->operadorValido($columnas[1]);
46
        $this->alfaNumerico($columnas[2]);
47
        
48
        $this->_datos = $this->setDatos($columnas);
49
50
        return ' WHERE '.$columnas[0].' '.$columnas[1].' ? ';
51
    }
52
53
    private function alfaNumerico($string)
54
    {
55
        if(!ctype_alnum($string))
56
        {
57
            throw new Exception("Caracter invalido en el campo de la sentencia WHERE");
58
        }
59
    }
60
61
    private function operadorValido($string)
62
    {
63
        if (!in_array($string, $this->_operadoresValidos))
64
        {
65
            throw new Exception("Comparador lógico invalido en el campo de la sentencia WHERE");
66
        }
67
    }
68
69
    private function setDatos(array $columnas): array
70
    {
71
        return array($columnas[2]);
72
    }
73
}