Skip to content

Commit

Permalink
fix #12, code refactory
Browse files Browse the repository at this point in the history
  • Loading branch information
tiamo committed Oct 5, 2018
1 parent f193ca0 commit d3ce30e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
Binary file removed examples/test2.sav
Binary file not shown.
34 changes: 34 additions & 0 deletions examples/write12.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* ISSUE: https://github.com/tiamo/spss/issues/12
*/

require __DIR__ . '/../vendor/autoload.php';

$file = __DIR__ . '/data12.sav';

$writer = new \SPSS\Sav\Writer([
'header' => [
'prodName' => '@(#) IBM SPSS STATISTICS',
'layoutCode' => 2,
'creationDate' => date('d M y'),
'creationTime' => date('H:i:s'),
],
'variables' => [
[
'name' => 'aaa',
'width' => 16,
'format' => 1,
],
[
'name' => 'ccc',
'format' => 5,
'values' => [
1 => 'Panel',
],
],
],
]);

$writer->save($file);
5 changes: 3 additions & 2 deletions src/Sav/Record/Variable.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ public function writeBlank(Buffer $buffer, $width)
$buffer->writeInt(-1);
$buffer->writeInt(0);
$buffer->writeInt(0);
$buffer->writeInt(0);
$buffer->writeInt(0);
$buffer->writeInt(0x011d01);
$buffer->writeInt(0x011d01);
$buffer->write(' ');
}
}
Expand All @@ -207,6 +207,7 @@ public function getSegmentName($seg = 0)
$name = $this->name;
$name = mb_substr($name, 0, 8);
$name = mb_substr($name, 0, -mb_strlen($seg)) . $seg;

return mb_strtoupper($name);
}
}
2 changes: 1 addition & 1 deletion src/Sav/Variable.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Variable
const ROLE_SPLIT = 5;

public $name;
public $width = 0;
public $width = 8;
public $decimals = 0;
public $format = 0;
public $columns;
Expand Down
27 changes: 20 additions & 7 deletions src/Sav/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,33 @@ public function write($data)

$this->data = new Record\Data();

$nominalIdx = 0;

/** @var Variable $var */
foreach ($data['variables'] as $idx => $var) {
// for ($idx = 0; $idx <= $variablesCount; $idx++) {
foreach (array_values($data['variables']) as $idx => $var) {

if (is_array($var)) {
$var = new Variable($var);
}

if (! preg_match('/^[A-Za-z0-9_]+$/', $var->name)) {
throw new \Exception(
throw new \InvalidArgumentException(
sprintf('Variable name `%s` contains an illegal character.', $var->name)
);
}

if (empty($var->width)) {
throw new \InvalidArgumentException(
sprintf('Invalid field width. Should be an integer number greater than zero.')
);
}

$variable = new Record\Variable();

// TODO: refactory
$variable->name = 'V' . str_pad($idx + 1, 7, 0, STR_PAD_LEFT);
// $variable->name = $var->name;
// $variable->name = strtoupper($var->name);

// TODO: test
if ($var->format == Variable::FORMAT_TYPE_A) {
Expand Down Expand Up @@ -153,7 +164,7 @@ public function write($data)
}
}

$this->variables[] = $variable;
$this->variables[$idx] = $variable;

if ($var->values) {
if ($variable->width > 8) {
Expand All @@ -170,7 +181,7 @@ public function write($data)
'value' => $key,
'label' => $value,
];
$valueLabel->indexes = [$idx + 1];
$valueLabel->indexes = [$nominalIdx + 1];
}
$this->valueLabels[] = $valueLabel;
}
Expand All @@ -183,7 +194,6 @@ public function write($data)
}

$segmentCount = Utils::widthToSegments($var->width);

for ($i = 0; $i < $segmentCount; $i++) {
$this->info[Record\Info\VariableDisplayParam::SUBTYPE][$idx] = [
$var->getMeasure(),
Expand All @@ -192,6 +202,7 @@ public function write($data)
];
}

// TODO: refactory
$dataCount = count($var->data);
if ($dataCount > $this->header->casesCount) {
$this->header->casesCount = $dataCount;
Expand All @@ -201,9 +212,11 @@ public function write($data)
$this->data->matrix[$case][$idx] = $value;
}

$this->header->nominalCaseSize += Utils::widthToOcts($var->width);
$nominalIdx += Utils::widthToOcts($var->width);
}

$this->header->nominalCaseSize = $nominalIdx;

// write header
$this->header->write($this->buffer);

Expand Down

0 comments on commit d3ce30e

Please sign in to comment.