|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
namespace src\preguntas; |
|
3
|
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
|
5
|
|
|
use src\interfaces\PreguntaInterface; |
|
6
|
|
|
|
|
7
|
|
|
class Pregunta implements PreguntaInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* |
|
11
|
|
|
* @var array |
|
12
|
|
|
*/ |
|
13
|
|
|
private array $_valores; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private string $_valor; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private string $_texto; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private string $_numero; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* |
|
35
|
|
|
* @var Consume |
|
36
|
|
|
*/ |
|
37
|
|
|
private Consume $_consume; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $valor |
|
42
|
|
|
* @param array $valoresPermitidos |
|
43
|
|
|
* @param Consume $consume |
|
44
|
|
|
*/ |
|
45
|
11 |
|
public function __construct(string $valor, array $valoresPermitidos, Consume $consume) |
|
46
|
|
|
{ |
|
47
|
11 |
|
$this->_consume = $consume; |
|
48
|
11 |
|
$this->_valores = $valoresPermitidos; |
|
49
|
11 |
|
$this->_valor = $this->setValor($valor); |
|
50
|
11 |
|
$this->_texto = $this->valorTexto(); |
|
51
|
11 |
|
$this->_numero = $this->valorNumero(); |
|
52
|
11 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function texto(): string |
|
59
|
|
|
{ |
|
60
|
2 |
|
return $this->_texto; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
3 |
|
public function numero(): string |
|
68
|
|
|
{ |
|
69
|
3 |
|
return $this->_numero; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
11 |
|
private function valorTexto(): string |
|
77
|
|
|
{ |
|
78
|
11 |
|
if (is_numeric($this->_valor)) { |
|
79
|
11 |
|
return $this->_valores[$this->_valor]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
return $this->_valor; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
11 |
|
private function valorNumero(): string |
|
90
|
|
|
{ |
|
91
|
11 |
|
if (!is_numeric($this->_valor)) { |
|
92
|
1 |
|
return (string) array_search($this->_valor, $this->_valores); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
11 |
|
return $this->_valor; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Define si lo que recibió la clase es el valor número aceptado o texto |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $string |
|
102
|
|
|
* |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
11 |
|
private function setValor(string $string): string |
|
106
|
|
|
{ |
|
107
|
11 |
|
if (!$this->_consume->consume()) { |
|
108
|
11 |
|
return '0'; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
11 |
|
if (array_key_exists($string, $this->_valores)) { |
|
112
|
11 |
|
return $string; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
2 |
|
if (in_array($string, $this->_valores)) { |
|
116
|
1 |
|
return $string; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
1 |
|
throw new InvalidArgumentException("El tipo de valor para la pregunta es incorrecto"); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|