Skip to content

Commit

Permalink
Dealing with broken charsets
Browse files Browse the repository at this point in the history
  • Loading branch information
hanneskod committed Oct 13, 2018
1 parent f4dd366 commit 9583885
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 13 deletions.
13 changes: 13 additions & 0 deletions spec/Visitor/AmountVisitorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,17 @@ function it_creates_valid_amounts(Node $node)

$this->beforeAmount($node);
}

function it_creates_amounts_from_strings_with_broken_charset(Node $node)
{
$node->getLineNr()->willReturn(1);
$node->hasChild('Object')->willReturn(false);
$node->getValueFrom('Text')->willReturn('1230¤');

$node->addChild(Argument::that(function (Obj $obj) {
return (new SEK('-123.00'))->equals($obj->getValue());
}))->shouldBeCalled();

$this->beforeAmount($node);
}
}
4 changes: 2 additions & 2 deletions src/Parser/Grammar.peg
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,11 @@ grammar Grammar extends MultibyteHack
return new AutogiroFile('AutogiroAmendmentResponseFile', $open, ...$recs);
};

AMENDMENT_OPENING = '01AUTOGIRO ' S10 date:DATE8 S10 ' MAKULERING/ÄNDRING ' ^ bgcNr:BGC6 bg:BG10 EOR {
AMENDMENT_OPENING = '01AUTOGIRO ' S10 date:DATE8 S10 ' MAKULERING/'.'NDRING ' ^ bgcNr:BGC6 bg:BG10 EOR {
return new Record('Opening', $date, $bgcNr, $bg);
};

OLD_AMENDMENT_OPENING = '01' date:DATE8 'AUTOGIRO9900' 'MAK/ÄNDRINGSLISTA ' ^ S20 bgcNr:BGC6 bg:BG10 EOR {
OLD_AMENDMENT_OPENING = '01' date:DATE8 'AUTOGIRO9900' 'MAK/'.'NDRINGSLISTA ' ^ S20 bgcNr:BGC6 bg:BG10 EOR {
return new Record('Opening', $date, $bgcNr, $bg);
};

Expand Down
68 changes: 60 additions & 8 deletions src/Parser/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5836,14 +5836,40 @@ protected function parseAMENDMENT_OPENING()
if ($_success) {
$_value148[] = $this->value;

if (substr($this->string, $this->position, strlen(' MAKULERING/ÄNDRING ')) === ' MAKULERING/ÄNDRING ') {
if (substr($this->string, $this->position, strlen(' MAKULERING/')) === ' MAKULERING/') {
$_success = true;
$this->value = substr($this->string, $this->position, strlen(' MAKULERING/ÄNDRING '));
$this->position += strlen(' MAKULERING/ÄNDRING ');
$this->value = substr($this->string, $this->position, strlen(' MAKULERING/'));
$this->position += strlen(' MAKULERING/');
} else {
$_success = false;

$this->report($this->position, '\' MAKULERING/ÄNDRING \'');
$this->report($this->position, '\' MAKULERING/\'');
}
}

if ($_success) {
$_value148[] = $this->value;

if ($this->position < strlen($this->string)) {
$_success = true;
$this->value = substr($this->string, $this->position, 1);
$this->position += 1;
} else {
$_success = false;
}
}

if ($_success) {
$_value148[] = $this->value;

if (substr($this->string, $this->position, strlen('NDRING ')) === 'NDRING ') {
$_success = true;
$this->value = substr($this->string, $this->position, strlen('NDRING '));
$this->position += strlen('NDRING ');
} else {
$_success = false;

$this->report($this->position, '\'NDRING \'');
}
}

Expand Down Expand Up @@ -5958,14 +5984,40 @@ protected function parseOLD_AMENDMENT_OPENING()
if ($_success) {
$_value149[] = $this->value;

if (substr($this->string, $this->position, strlen('MAK/ÄNDRINGSLISTA ')) === 'MAK/ÄNDRINGSLISTA ') {
if (substr($this->string, $this->position, strlen('MAK/')) === 'MAK/') {
$_success = true;
$this->value = substr($this->string, $this->position, strlen('MAK/'));
$this->position += strlen('MAK/');
} else {
$_success = false;

$this->report($this->position, '\'MAK/\'');
}
}

if ($_success) {
$_value149[] = $this->value;

if ($this->position < strlen($this->string)) {
$_success = true;
$this->value = substr($this->string, $this->position, 1);
$this->position += 1;
} else {
$_success = false;
}
}

if ($_success) {
$_value149[] = $this->value;

if (substr($this->string, $this->position, strlen('NDRINGSLISTA ')) === 'NDRINGSLISTA ') {
$_success = true;
$this->value = substr($this->string, $this->position, strlen('MAK/ÄNDRINGSLISTA '));
$this->position += strlen('MAK/ÄNDRINGSLISTA ');
$this->value = substr($this->string, $this->position, strlen('NDRINGSLISTA '));
$this->position += strlen('NDRINGSLISTA ');
} else {
$_success = false;

$this->report($this->position, '\'MAK/ÄNDRINGSLISTA \'');
$this->report($this->position, '\'NDRINGSLISTA \'');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use byrokrat\autogiro\Visitor\VisitorInterface;
use byrokrat\autogiro\Tree\Node;
use byrokrat\autogiro\Exception\ParserException;
use \ForceUTF8\Encoding;
use ForceUTF8\Encoding;

final class Parser implements ParserInterface
{
Expand Down
18 changes: 16 additions & 2 deletions src/Visitor/AmountVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,25 @@ public function beforeAmount(Node $node): void
}

try {
$node->addChild(new Obj($node->getLineNr(), SEK::createFromSignalString($signalStr)));
$invertSign = false;

// due to charset issues unknown trailing signal chars are treated as 'å'
if (!preg_match('/^[0-9åJKLMNOPQR]$/', mb_substr($signalStr, -1))) {
$signalStr = mb_substr($signalStr, 0, -1) . '0';
$invertSign = true;
}

$object = SEK::createFromSignalString($signalStr);

if ($invertSign) {
$object = $object->getInverted();
}

$node->addChild(new Obj($node->getLineNr(), $object));
} catch (AmountException $e) {
$this->getErrorObject()->addError(
"Invalid signaled amount %s on line %s",
$signalStr,
(string)$node->getValueFrom('Text'),
(string)$node->getLineNr()
);
}
Expand Down

0 comments on commit 9583885

Please sign in to comment.