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

limitar caracteres en campos al guardar en base de datos #1637

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Core/Model/Base/ModelClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ protected function saveInsert(array $values = []): bool
$fieldName = $field['name'];
$fieldValue = $values[$fieldName] ?? $this->{$fieldName};

$fieldValue = $this->ajustarLongitudCampo($field['type'], $fieldValue);

$insertFields[] = self::$dataBase->escapeColumn($fieldName);
$insertValues[] = self::$dataBase->var2str($fieldValue);
}
Expand Down Expand Up @@ -451,6 +453,9 @@ protected function saveUpdate(array $values = []): bool
if ($field['name'] !== static::primaryColumn()) {
$fieldName = $field['name'];
$fieldValue = $values[$fieldName] ?? $this->{$fieldName};

$fieldValue = $this->ajustarLongitudCampo($field['type'], $fieldValue);

$sql .= $coma . ' ' . self::$dataBase->escapeColumn($fieldName) . ' = ' . self::$dataBase->var2str($fieldValue);
$coma = ', ';
}
Expand Down Expand Up @@ -511,4 +516,30 @@ private function getRecord($code, array $where = [], array $order = []): array
$sql = 'SELECT * FROM ' . static::tableName() . $sqlWhere . self::getOrderBy($order);
return empty($code) && empty($where) ? [] : self::$dataBase->selectLimit($sql, 1);
}

/**
* Corta un texto a una longitud específica.
*
* @param string $fieldType
* @param mixed $fieldValue
*
* @return mixed
*/
private function ajustarLongitudCampo(string $fieldType, $fieldValue)
{
if (strpos($fieldType, 'varchar') !== false || strpos($fieldType, 'varying') !== false) {
$patron = '/\(\s*(\d+)\s*\)/';
preg_match($patron, $fieldType, $coincidencias);
$longitudMaxima = $coincidencias[1] ?? null;

if ($longitudMaxima && is_string($fieldValue)) {
$longitudActual = mb_strlen($fieldValue);
if ($longitudActual > $longitudMaxima) {
return substr($fieldValue, 0, $longitudMaxima);
}
}
}

return $fieldValue;
}
}
21 changes: 21 additions & 0 deletions Test/Core/Model/ClienteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use FacturaScripts\Core\Lib\Vies;
use FacturaScripts\Core\Model\Cliente;
use FacturaScripts\Core\Tools;
use FacturaScripts\Test\Traits\LogErrorsTrait;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -210,6 +211,26 @@ public function testVies(): void
$this->assertTrue($cliente->delete());
}

public function testLimitarCamposSegunTabla()
{
$texto = Tools::randomString(200);

$cliente = new Cliente();
$cliente->nombre = $texto;
$cliente->cifnif = '12345678A';
$this->assertTrue($cliente->save(), 'cliente-cant-save');

$cliente->loadFromCode($cliente->codcliente);

// comprobamos que se ha limitado el nombre a 100 caracteres ya que en la tabla es varchar(100)
$this->assertEquals(substr($texto, 0, 100), $cliente->nombre);
$this->assertNotEquals($texto, $cliente->nombre);

// eliminamos
$this->assertTrue($cliente->getDefaultAddress()->delete(), 'contacto-cant-delete');
$this->assertTrue($cliente->delete(), 'cliente-cant-delete');
}

protected function tearDown(): void
{
$this->logErrors();
Expand Down