Commit 84a6b81e authored by Karamel's avatar Karamel
Browse files

Truncate long string values for varchar fields

parent b65935f9
......@@ -33,7 +33,8 @@ use \Pasteque\Server\Model\Field\StringField;
class Archive
{
protected static function getDirectFieldNames() {
return [new IntField('number'), new StringField('info'),
return [new IntField('number'),
new StringField('info', ['length' => null]),
new StringField('content_hash'), 'content',
new StringField('signature')];
}
......
......@@ -55,7 +55,7 @@ class Customer extends DoctrineMainModel
new StringField('city'),
new StringField('region'),
new StringField('country'),
new StringField('note'),
new StringField('note', ['length' => null]),
new BoolField('visible'),
'hasImage',
new DateField('expireDate', ['nullable' => true])];
......
......@@ -29,13 +29,21 @@ class StringField extends Field
public const TYPE = 'string';
public const CONVERT_CSTR = 0;
protected $length = 255;
/**
* @Override From Field::__construct
* StringField has no option 'nullable'. Null is converted to ''.
* It has an dedicated option 'length' (default 255) to specify a
* maximum length. The value will be truncated. Set to null to disable
* the length check.
*/
public function __construct($name, $options = []) {
parent::__construct($name, $options);
$this->nullable = false;
if (array_key_exists('length', $options)) {
$this->length = $options['length'];
}
}
/**
......@@ -53,6 +61,10 @@ class StringField extends Field
if ($input === true) {
return 'true';
}
return strval($input);
$val = strval($input);
if ($this->length !== null) {
$val = substr($val, 0, $this->length);
}
return $val;
}
}
......@@ -47,7 +47,7 @@ class FiscalTicket extends DoctrineMainModel
new IntField('sequence'),
new IntField('number'),
new DateField('date'),
new StringField('content'),
new StringField('content', ['length' => null]),
new StringField('signature')];
}
protected static function getAssociationFields() {
......
......@@ -40,7 +40,7 @@ class Option extends DoctrineMainModel
return [
new StringField('name'),
new BoolField('system'),
new StringField('content')];
new StringField('content', ['length' => null])];
}
protected static function getAssociationFields() {
return [];
......
......@@ -40,4 +40,19 @@ class StringFieldTest extends TestCase {
$this->assertEquals('true', $field->convert(true));
$this->assertEquals('3', $field->convert(3));
}
public function testConvertLength() {
$field = new StringField('test', ['length' => 3]);
$this->assertEquals('123', $field->convert('1234567890'));
}
public function testConvertNoLength() {
$field = new StringField('test', ['length' => null]);
$pattern = '1234567890';
$val = $pattern;
for ($i = 1; $i < 30; $i++) {
$val .= $pattern;
}
$this->assertEquals($val, $field->convert($val));
}
}
......@@ -22,6 +22,7 @@
namespace Pasteque\Server;
use \Pasteque\Server\Model\FiscalTicket;
use \Pasteque\Server\System\DAO\DAOFactory;
use \PHPUnit\Framework\TestCase;
require_once(dirname(dirname(__FILE__)) . "/common_load.php");
......@@ -94,4 +95,19 @@ class FiscalTicketTest extends TestCase
$this->assertFalse($fiscalTicket2->checkSignature(null));
}
public function testUntruncatedContent() {
$pattern = '1234567890';
$content = $pattern;
for ($i = 1; $i < 30; $i++) {
$content .= $pattern;
}
$struct = ['sequence' => '00001', 'number' => 1, 'date' => '2021-06-10',
'content' => $content, 'signature' => 'sig'];
$fiscalTicket = new FiscalTicket();
global $dbInfo;
$dao = DAOFactory::getDAO($dbInfo, ['debug' => true]);
$fiscalTicket->merge($struct, $dao);
$dao->close();
$this->assertEquals($content, $fiscalTicket->getContent());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment