Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tarea #1879 - Restringir creación de columnas #32

Merged
merged 1 commit into from Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions Columna.php
Expand Up @@ -20,6 +20,9 @@ final class Columna
/** @var string */
public $nombre = '';

/** bool */
public $primary = false;

/** bool */
public $requerido = false;

Expand Down Expand Up @@ -55,8 +58,8 @@ public function ask(array $previous): void

if ($type === 1) {
foreach ($previous as $column) {
if ($column->tipo === 'serial') {
echo "\nYa hay un campo de tipo serial.\n";
if ($column->tipo === 'serial' || $column->primary) {
echo "\nYa hay un campo de tipo serial o primary key.\n";
continue 2;
}
}
Expand Down Expand Up @@ -104,6 +107,8 @@ private function setType(int $type): void
switch ($type) {
case 1:
$this->tipo = 'serial';
$this->primary = true;
$this->requerido = true;
return;

case 2:
Expand Down
54 changes: 47 additions & 7 deletions fsmaker.php
Expand Up @@ -85,6 +85,7 @@ private function askFields(): array
$fields[] = new Columna([
'display' => 'none',
'nombre' => 'id',
'primary' => true,
'requerido' => true,
'tipo' => 'serial'
]);
Expand Down Expand Up @@ -120,9 +121,11 @@ private function askFields(): array
echo "\n";

while (true) {
$name = strtolower($this->prompt('Nombre del campo (vacío para terminar)'));
if (empty($name)) {
$name = $this->prompt('Nombre del campo (vacío para terminar)', '/^[a-z][a-z0-9_]*$/');
if (is_null($name)) {
break;
} elseif (empty($name)) {
continue;
}

if (in_array($name, explode(',', self::FORBIDDEN_WORDS))) {
Expand All @@ -142,9 +145,36 @@ private function askFields(): array
return strcmp($a->nombre, $b->nombre);
});

$this->askPrimaryKey($fields);
return $fields;
}

private function askPrimaryKey(array &$fields)
{
// si hay un campo serial o primary key, terminamos
foreach ($fields as $field) {
if ($field->tipo === 'serial' || $field->primary) {
return;
}
}

// indicamos que campo es la clave primaria
while (true) {
foreach ($fields as $index => $field) {
echo $index . " - " . $field->nombre . "\n";
}

$pos = $this->prompt('No estableció ninguna clave primaria, seleccione una de las anteriores', '/^[0-9]*$/');
if ($pos == '' || false === isset($fields[$pos])) {
continue;
}

$fields[$pos]->primary = true;
$fields[$pos]->requerido = true;
break;
}
}

private function createController()
{
$name = $this->prompt('Nombre del controlador', '/^[A-Z][a-zA-Z0-9_]*$/');
Expand Down Expand Up @@ -611,6 +641,10 @@ private function createModelByFields(string $fileName, string $tableName, array
break;
}

if ($field->primary) {
$primaryColumn = $field->nombre;
}

if (strpos($field->tipo, 'character varying') !== false) {
$typeProperty = 'string';
if (false === in_array($field->nombre, $testExclude)) {
Expand Down Expand Up @@ -765,13 +799,13 @@ private function createXMLTableByFields(string $tableFilename, string $tableName
. " <name>$field->nombre</name>\n"
. " <type>$field->tipo</type>\n";

if ($field->tipo === 'serial' || $field->requerido) {
if ($field->tipo === 'serial' || $field->primary || $field->requerido) {
$columns .= " <null>NO</null>\n";
}

$columns .= " </column>\n";

if ($field->tipo === 'serial') {
if ($field->tipo === 'serial' || $field->primary) {
$constraints .= " <constraint>\n"
. ' <name>' . $tableName . "_pkey</name>\n"
. ' <type>PRIMARY KEY (' . $field->nombre . ")</type>\n"
Expand Down Expand Up @@ -822,8 +856,8 @@ private function createXMLViewByFields(string $xmlFilename, array $fields, int $
continue;
}

// si la columna es de tipo serial, la ponemos al principio
if ($field->tipo === 'serial') {
// si la columna es de tipo serial o primary, la ponemos al principio
if ($field->tipo === 'serial' || $field->primary) {
$columns = $this->getWidget($field, $order, $tabForColums) . $columns;
$order += 10;
continue;
Expand Down Expand Up @@ -1042,11 +1076,17 @@ private function modifyInit(string $name, int $modelOrController)
echo '* ' . $fileName . self::OK;
}

private function prompt($label, $pattern = ''): string
private function prompt($label, $pattern = ''): ?string
{
echo $label . ': ';
$matches = [];
$value = trim(fgets(STDIN));

// si el valor esta vacío, devolvemos null
if ($value == '') {
return null;
}

if (!empty($pattern) && 1 !== preg_match($pattern, $value, $matches)) {
echo "Valor no válido. Debe cumplir: " . $pattern . "\n";
return '';
Expand Down