Skip to content

Commit

Permalink
Merge pull request #62 from lestcape/FixReturnValue
Browse files Browse the repository at this point in the history
Fix the return value of the isVeryLong function
  • Loading branch information
lestcape authored Jul 6, 2021
2 parents 04513e4 + d9d0960 commit a32a5cf
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Sav/Record/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ protected function readCaseData(Buffer $buffer, $compressed, $bias, $variables,
for ($index = 0; $index < $varCount; $index++) {
$var = $variables[$index];
$isNumeric = 0 === $var->width && \SPSS\Sav\Variable::isNumberFormat($var->write[1]);
$width = isset($var->write[2]) ? $var->write[2] : $var->width;
$width = (isset($var->write[2]) && ($var->write[2] !== 0)) ? $var->write[2] : $var->width;

// var_dump($var);
// exit;
Expand Down Expand Up @@ -463,7 +463,7 @@ protected function writeCaseData(Buffer $buffer, $row, $compressed, $bias, $vari

// $isNumeric = $var->width == 0;
$isNumeric = 0 === $var->width && \SPSS\Sav\Variable::isNumberFormat($var->write[1]);
$width = isset($var->write[2]) ? $var->write[2] : $var->width;
$width = (isset($var->write[2]) && ($var->write[2] !== 0)) ? $var->write[2] : $var->width;

if ($isNumeric) {
if (!$compressed) {
Expand Down
4 changes: 2 additions & 2 deletions src/Sav/Record/Info/LongStringValueLabels.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ public function write(Buffer $buffer)
}
$width = (int) $data['width'];
$localBuffer->writeInt(mb_strlen($varName));
$localBuffer->writeString($varName);
$localBuffer->writeString($varName, mb_strlen($varName));
$localBuffer->writeInt($width);
$localBuffer->writeInt(Utils::is_countable($data['values']) ? \count($data['values']) : 0);
foreach ($data['values'] as $value => $label) {
$localBuffer->writeInt($width);
$localBuffer->writeString($value, $width);
$localBuffer->writeInt(mb_strlen($label));
$localBuffer->writeString($label);
$localBuffer->writeString($label, mb_strlen($label));
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/Sav/Record/ValueLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,8 @@ public function read(Buffer $buffer)

public function write(Buffer $buffer)
{
$convertToDouble = false;
$varIndex = reset($this->indexes);
if (false !== $varIndex && isset($this->variables[$varIndex - 1])) {
$varWidth = $this->variables[$varIndex - 1]->width;
$convertToDouble = $varWidth > 0;
}
$var = (count($this->variables) > 0) ? $this->variables[count($this->variables) - 1] : null;
$convertToDouble = (isset($var) && ($var->width > 0));

// Value label record.
$buffer->writeInt(self::TYPE);
Expand Down
4 changes: 2 additions & 2 deletions src/Sav/Record/Variable.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ class Variable extends Record

/**
* Print format for this variable.
* [decimals, width, format, 0].
* [0, format, width, decimals].
*
* @var array
*/
public $print = [0, 0, 0, 0];

/**
* Write format for this variable.
* [decimals, width, format, 0].
* [0, format, width, decimals].
*
* @var array
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Sav/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ public function write($data)
$variable->print = [
0,
$var->format,
$var->width !== [] ? min($var->width, 255) : 8,
$var->width > 0 ? min($var->width, 255) : 8,
$var->decimals,
];
$variable->write = [
0,
$var->format,
$var->width !== [] ? min($var->width, 255) : 8,
$var->width > 0 ? min($var->width, 255) : 8,
$var->decimals,
];

Expand Down Expand Up @@ -188,7 +188,7 @@ public function write($data)

$this->info[Record\Info\LongVariableNames::SUBTYPE][$shortName] = $var->name;

if (Record\Variable::isVeryLong($var->width) !== 0) {
if (Record\Variable::isVeryLong($var->width) !== false) {
$this->info[Record\Info\VeryLongString::SUBTYPE][$shortName] = $var->width;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public static function widthToBytes($width)

if (0 === $width) {
$bytes = 8;
} elseif (Variable::isVeryLong($width) === 0) {
} elseif (Variable::isVeryLong($width) === false) {
$bytes = $width;
} else {
$chunks = $width / Variable::EFFECTIVE_VLS_CHUNK;
Expand Down Expand Up @@ -202,7 +202,7 @@ public static function widthToOcts($width)
*/
public static function widthToSegments($width)
{
return Variable::isVeryLong($width) !== 0 ? ceil($width / Variable::EFFECTIVE_VLS_CHUNK) : 1;
return Variable::isVeryLong($width) !== false ? ceil($width / Variable::EFFECTIVE_VLS_CHUNK) : 1;
}

/**
Expand Down Expand Up @@ -233,7 +233,7 @@ public static function segmentAllocWidth($width, $segment = 0)
$segmentCount = self::widthToSegments($width);
// assert($segment < $segmentCount);

if (Variable::isVeryLong($width) === 0) {
if (Variable::isVeryLong($width) === false) {
return $width;
}

Expand Down

0 comments on commit a32a5cf

Please sign in to comment.