Skip to content
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 3.2.1 (2026-12-01)

- Fixed the weight variable index written by `Writer` for typed datasets. SPSS expects the 1-based 8-byte slot of the weight variable, but the writer used `VariableMetadata::$dictionaryIndex` or the logical position, so a weight placed after a string wider than 8 bytes pointed at the wrong slot and was lost on reading. The writer now counts slots from the written dictionary and no longer reads `dictionaryIndex` for this.
- No runtime API changes.

## 3.2.0 (2026-11-09)

- Restored PHP 8.3 support. The minimum had been raised to 8.4.1 with the v3 tooling update, but the library only used three PHP 8.4 constructs (`new` without parentheses in two places and `array_all()`); these are replaced with PHP 8.3 equivalents without behaviour changes.
Expand Down
8 changes: 6 additions & 2 deletions src/Sav/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,11 +506,15 @@ private function datasetToWriterData(Dataset $dataset): array

$weightIndex = 0;
if (null !== $dataset->metadata->weightVariableName) {
foreach ($dataset->variables() as $index => $variable) {
// SPSS stores the weight as the 1-based 8-byte slot of the written dictionary, so
// count slots here; dictionaryIndex describes the source file and may be stale.
$slot = 1;
foreach ($dataset->variables() as $variable) {
if ($variable->name === $dataset->metadata->weightVariableName) {
$weightIndex = $variable->dictionaryIndex ?? ($index + 1);
$weightIndex = $slot;
break;
}
$slot += VariableType::STRING === $variable->type ? Utils::widthToOcts($variable->width) : 1;
}
if (0 === $weightIndex) {
throw new \InvalidArgumentException(sprintf(
Expand Down
51 changes: 51 additions & 0 deletions tests/DatasetRoundTripTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,55 @@ public function testTypedZsavRoundTripPreservesCompressionBoundariesAndMetadataD
[null],
], $actual->rows());
}

public function testWeightIndexCountsStringSlotsAndIgnoresStaleDictionaryIndex(): void
{
$numeric = new VariableFormat(Variable::FORMAT_TYPE_F, 8, 2);
$string = static fn(int $width): VariableMetadata => new VariableMetadata(
name: 'text' . $width,
type: VariableType::STRING,
width: $width,
printFormat: new VariableFormat(Variable::FORMAT_TYPE_A, min($width, 255)),
writeFormat: new VariableFormat(Variable::FORMAT_TYPE_A, min($width, 255)),
);
$source = new Dataset(
dictionary: new VariableDictionary([
$string(20),
new VariableMetadata('count', VariableType::NUMERIC, 0, $numeric, $numeric),
$string(300),
// Logical ordinal, not the physical slot: the writer must not trust it.
new VariableMetadata('weight', VariableType::NUMERIC, 0, $numeric, $numeric, dictionaryIndex: 4),
]),
rows: [['abc', 2.0, 'long', 1.5]],
metadata: new FileMetadata(weightVariableName: 'weight'),
);

$buffer = (new Writer($source))->getBuffer();
$buffer->rewind();

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

// 20 bytes take 3 slots, a number 1, 300 bytes 32 + 6, so the weight is slot 43.
self::assertSame(43, $reader->header->weightIndex);
self::assertSame('weight', $actual->metadata->weightVariableName);
self::assertSame([['abc', 2.0, 'long', 1.5]], $actual->rows());
}

public function testWriterRejectsWeightVariableMissingFromDictionary(): void
{
$numeric = new VariableFormat(Variable::FORMAT_TYPE_F, 8, 2);
$source = new Dataset(
dictionary: new VariableDictionary([
new VariableMetadata('count', VariableType::NUMERIC, 0, $numeric, $numeric),
]),
rows: [[1.0]],
metadata: new FileMetadata(weightVariableName: 'weight'),
);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Weight variable "weight" is not present in the dictionary.');

new Writer($source);
}
}
Loading