Skip to content

Commit

Permalink
Improve string type with NL normalization (#1036)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Jul 22, 2022
1 parent f4ebf2b commit f31722f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,21 @@ public function normalize($value)
switch ($this->type) {
case null:
case 'string':
$value = trim(str_replace(["\r", "\n"], '', $value)); // remove all line-ends and trim
$value = trim(preg_replace('~\r?\n|\r~', ' ', $value)); // remove all line-ends and trim

break;
case 'text':
$value = rtrim(str_replace(["\r\n", "\r"], "\n", $value)); // normalize line-ends to LF and rtrim
$value = rtrim(preg_replace('~\r?\n|\r~', "\n", $value)); // normalize line-ends to LF and rtrim

break;
case 'boolean':
case 'integer':
$value = preg_replace('/\s+|[,`\']/', '', $value);
$value = preg_replace('~\s+|[,`\']~', '', $value);

break;
case 'float':
case 'atk4_money':
$value = preg_replace('/\s+|[,`\'](?=.*\.)/', '', $value);
$value = preg_replace('~\s+|[`\']|,(?=.*\.)~', '', $value);

break;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,13 @@ public function testNormalize(): void

// string
$m->set('string', "Two\r\nLines ");
$this->assertSame('TwoLines', $m->get('string'));
$this->assertSame('Two Lines', $m->get('string'));

$m->set('string', "Two\rLines ");
$this->assertSame('TwoLines', $m->get('string'));
$this->assertSame('Two Lines', $m->get('string'));

$m->set('string', "Two\nLines ");
$this->assertSame('TwoLines', $m->get('string'));
$this->assertSame('Two Lines', $m->get('string'));

// text
$m->set('text', "Two\r\nLines ");
Expand Down Expand Up @@ -691,7 +691,7 @@ public function testToString(): void
$m->addField('object', ['type' => 'object']);
$m = $m->createEntity();

$this->assertSame('TwoLines', $m->getField('string')->toString("Two\r\nLines "));
$this->assertSame('Two Lines', $m->getField('string')->toString("Two\r\nLines "));
$this->assertSame("Two\nLines", $m->getField('text')->toString("Two\r\nLines "));
$this->assertSame('123', $m->getField('integer')->toString(123));
$this->assertSame('123.45', $m->getField('money')->toString(123.45));
Expand Down

0 comments on commit f31722f

Please sign in to comment.