-
Notifications
You must be signed in to change notification settings - Fork 3
/
Columna.php
182 lines (148 loc) · 4.7 KB
/
Columna.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* @author Daniel Fernández Giménez <hola@danielfg.es>
*/
final class Columna
{
/** @var string */
public $display = '';
/** @var int */
public $longitud = 0;
/** @var int */
public $maximo = 100;
/** @var int */
public $minimo = 0;
/** @var string */
public $nombre = '';
/** bool */
public $primary = false;
/** bool */
public $requerido = false;
/** @var float */
public $step = 1;
/** @var string */
public $tipo = 'integer';
public function __construct(array $propiedades = [])
{
foreach ($propiedades as $property => $value) {
if (property_exists($this, $property)) {
$this->{$property} = $value;
}
}
}
public function ask(array $previous): void
{
while (true) {
echo "\n";
$type = (int)$this->prompt("Elija el tipo de campo\n"
. "1 = serial (autonumérico, ideal para ids)\n"
. "2 = integer\n"
. "3 = float\n"
. "4 = boolean\n"
. "5 = character varying\n"
. "6 = text\n"
. "7 = timestamp\n"
. "8 = date\n"
. "9 = time\n");
if ($type === 1) {
foreach ($previous as $column) {
if ($column->tipo === 'serial' || $column->primary) {
echo "\nYa hay un campo de tipo serial o primary key.\n";
continue 2;
}
}
}
if ($type >= 1 && $type <= 9) {
$this->setType($type);
break;
}
echo "\nOpción incorrecta.\n";
}
}
public function askLongitud(): void
{
$this->longitud = (int)$this->prompt("Longitud caracteres") ?? 30;
}
public function askMaximo(): void
{
$max = (float)$this->prompt("¿Valor máximo permitido?, dejar en blanco para no establecer valor.");
$this->maximo = empty($max) && $max != 0 || false === is_numeric($max) ? null : $max;
}
public function askMinimo(): void
{
$min = (float)$this->prompt("¿Valor mínimo permitido?, dejar en blanco para no establecer valor.");
$this->minimo = empty($min) && $min != 0 || false === is_numeric($min) ? null : $min;
}
public function askStep(): void
{
$step = (float)$this->prompt("¿Valor de incremento?, dejar en blanco para no establecer valor.");
$this->step = empty($step) && $step != 0 || false === is_numeric($step) ? null : $step;
}
private function prompt(string $label): string
{
echo $label . ': ';
return trim(fgets(STDIN));
}
private function setType(int $type): void
{
switch ($type) {
case 1:
$this->tipo = 'serial';
$this->primary = true;
$this->requerido = true;
return;
case 2:
$this->tipo = 'integer';
$this->askMaximo();
$this->askMinimo();
$this->askStep();
break;
case 3:
$this->tipo = 'double precision';
$this->askMaximo();
$this->askMinimo();
$this->askStep();
break;
case 4:
$this->tipo = 'boolean';
break;
case 5:
$this->tipo = 'character varying';
$this->askLongitud();
break;
case 6:
$this->tipo = 'text';
break;
case 7:
$this->tipo = 'timestamp';
break;
case 8:
$this->tipo = 'date';
break;
case 9:
$this->tipo = 'time';
break;
}
do {
$display = (int)$this->prompt("¿Cual es la alineación del campo {$this->nombre}? 0=Izquierda, 1=Derecha, 2=Centro, 3=Ocultar");
} while ($display < 0 || $display > 3);
switch ($display) {
case 0:
$this->display = 'left';
break;
case 1:
$this->display = 'right';
break;
case 2:
$this->display = 'center';
break;
case 3:
$this->display = 'none';
break;
}
do {
$requerido = $this->prompt("¿El campo {$this->nombre} es obligatorio? 1=Si, 0=No");
$this->requerido = $requerido === '1';
} while ($requerido !== '1' && $requerido !== '0');
}
}