Skip to content

Commit

Permalink
code refactory
Browse files Browse the repository at this point in the history
  • Loading branch information
tiamo committed Jun 27, 2020
1 parent 2a0127f commit 8250307
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
1 change: 0 additions & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# These are supported funding model platforms

github: tiamo
# patreon: tiamo
43 changes: 19 additions & 24 deletions src/Sav/Record/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Data extends Record
public function readCase(Buffer $buffer, $case)
{
/* check if this is the first time */
if (-1 === $this->startData) {
if ($this->startData === -1) {
$this->opcodeIndex = 8;
$this->opcodes = [];

Expand Down Expand Up @@ -117,9 +117,6 @@ public function readCase(Buffer $buffer, $case)
}
}

/**
* @param Buffer $buffer
*/
public function read(Buffer $buffer)
{
if ($this->startData === -1) {
Expand Down Expand Up @@ -177,9 +174,6 @@ public function read(Buffer $buffer)
}
}

/**
* @param array $row
*/
public function writeCase(Buffer $buffer, $row)
{
if (!isset($buffer->context->variables)) {
Expand Down Expand Up @@ -380,9 +374,9 @@ protected function readCaseData(Buffer $buffer, $compressed, $bias, $variables,
$varNum = 0;

for ($index = 0; $index < $varCount; $index++) {
$var = $variables[$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] : $var->width;

// var_dump($var);
// exit;
Expand Down Expand Up @@ -410,19 +404,17 @@ protected function readCaseData(Buffer $buffer, $compressed, $bias, $variables,
}
}
} else {
$width = isset($veryLongStrings[$var->name]) ? $veryLongStrings[$var->name] : $width;
$width = isset($veryLongStrings[$var->name]) ? $veryLongStrings[$var->name] : $width;
$result[$varNum] = '';
$segmentsCount = Utils::widthToSegments($width);
$opcode = self::OPCODE_RAW_DATA;
$segmentsCount = Utils::widthToSegments($width);
$opcode = self::OPCODE_RAW_DATA;
for ($s = 0; $s < $segmentsCount; $s++) {
$segWidth = Utils::segmentAllocWidth($width, $s);
if (self::OPCODE_NOP === $opcode || self::OPCODE_EOF === $opcode) {
// If next segments are empty too, skip
continue;
}
for ($i = $segWidth; $i > 0; $i -= 8) {
$chunkSize = ($segWidth = 255) !== 0 ? min($i, 8) : 8;

$val = '';
if (!$compressed) {
$val = $buffer->readString(8);
Expand Down Expand Up @@ -462,6 +454,7 @@ protected function readCaseData(Buffer $buffer, $compressed, $bias, $variables,
* @param array $veryLongStrings
* @param int $sysmis
*
* @return void
*/
protected function writeCaseData(Buffer $buffer, $row, $compressed, $bias, $variables, $veryLongStrings, $sysmis)
{
Expand All @@ -470,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] : $var->width;

if ($isNumeric) {
if (!$compressed) {
Expand All @@ -483,21 +476,23 @@ protected function writeCaseData(Buffer $buffer, $row, $compressed, $bias, $vari
$this->writeOpcode($buffer, self::OPCODE_RAW_DATA);
$this->dataBuffer->writeDouble($value);
}
} elseif (!$compressed) {
$buffer->writeString($value, Utils::roundUp($width, 8));
} else {
$offset = 0;
$width = isset($veryLongStrings[$var->name]) ? $veryLongStrings[$var->name] : $width;
$offset = 0;
$width = isset($veryLongStrings[$var->name]) ? $veryLongStrings[$var->name] : $width;
$segmentsCount = Utils::widthToSegments($width);
for ($s = 0; $s < $segmentsCount; $s++) {
$segWidth = Utils::segmentAllocWidth($width, $s);
for ($i = $segWidth; $i > 0; $i -= 8) {
$chunkSize = ($segWidth = 255) !== 0 ? min($i, 8) : 8;
$val = substr($value, $offset, $chunkSize); // Read 8 byte segements, don't use mbsubstr here
if ('' === $val) {
$this->writeOpcode($buffer, self::OPCODE_WHITESPACES);
$chunkSize = $segWidth === 255 ? min($i, 8) : 8;
$val = substr($value, $offset, $chunkSize); // Read 8 byte segments, don't use mbsubstr here
if ($compressed) {
if ('' === $val) {
$this->writeOpcode($buffer, self::OPCODE_WHITESPACES);
} else {
$this->writeOpcode($buffer, self::OPCODE_RAW_DATA);
$this->dataBuffer->writeString($val, 8);
}
} else {
$this->writeOpcode($buffer, self::OPCODE_RAW_DATA);
$this->dataBuffer->writeString($val, 8);
}
$offset += $chunkSize;
Expand Down

0 comments on commit 8250307

Please sign in to comment.