Skip to content

Commit

Permalink
Merge pull request #16 from mennodekker/master
Browse files Browse the repository at this point in the history
Partial fix #15 - labels no longer trimmed for multibyte chars
  • Loading branch information
tiamo authored Dec 14, 2018
2 parents d21727c + 3fec102 commit cc710d5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/Sav/Record/Variable.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,17 @@ public function write(Buffer $buffer)
$buffer->writeString($this->name, 8);

if ($hasLabel) {
// Maxlength is 255 bytes, since we write utf8 a char can be multiple bytes
$labelLength = min(mb_strlen($this->label), 255);
$buffer->writeInt($labelLength);
$buffer->writeString($this->label, Utils::roundUp($labelLength, 4));
$label = mb_substr($this->label, 0, $labelLength);
$labelLengthBytes = mb_strlen($label, '8bit');
while ($labelLengthBytes > 255) {
// Strip one char, can be multiple bytes
$label = mb_substr($this->label, 0, -1);
$labelLengthBytes = mb_strlen($label, '8bit');
}
$buffer->writeInt($labelLengthBytes);
$buffer->writeString($label, Utils::roundUp($labelLengthBytes, 4));
}

// TODO: test
Expand All @@ -156,7 +164,8 @@ public function write(Buffer $buffer)
}
}

$this->writeBlank($buffer, $seg0width);
// I think we don't need an empty record
//$this->writeBlank($buffer, $seg0width);

// Write additional segments for very long string variables.
if (self::isVeryLong($this->width)) {
Expand Down
3 changes: 2 additions & 1 deletion tests/SavRandomReadWriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ public function testWriteRead($data)
// )
// );
// }
$index += $var['width'] > 0 ? Utils::widthToOcts($var['width']) : 1;
//$index += $var['width'] > 0 ? Utils::widthToOcts($var['width']) : 1;
$index++;
}

// TODO: valueLabels
Expand Down
51 changes: 51 additions & 0 deletions tests/WriteMultibyteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace SPSS\Tests;

use SPSS\Sav\Reader;
use SPSS\Sav\Writer;

class WriteMultibyteTest extends TestCase
{

public function testMultiByteLabel()
{
$data = [
'header' => [
'prodName' => '@(#) IBM SPSS STATISTICS',
'layoutCode' => 2,
'creationDate' => date('d M y'),
'creationTime' => date('H:i:s'),
],
'variables' => [
[
'name' => 'longname_longerthanexpected',
'label' => 'Data zákończenia',
'width' => 16,
'format' => 1,
],
[
'name' => 'ccc',
'label' => '12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901233á',
'format' => 5,
'values' => [
1 => 'Panel',
],
],
],
];
$writer = new Writer($data);

$buffer = $writer->getBuffer();
$buffer->rewind();

$reader = Reader::fromString($buffer->getStream())->read();

// Sort name
$this->assertEquals($data['variables'][0]['label'], $reader->variables[0]->label);

// Long name
$this->assertEquals(mb_substr($data['variables'][1]['label'], 0, -1), $reader->variables[1]->label);
}

}

0 comments on commit cc710d5

Please sign in to comment.