Created
April 15, 2019 11:28
-
-
Save SamMousa/ff877ca87d07e86a7e436c9c1eb0ab73 to your computer and use it in GitHub Desktop.
Multiple response sets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class InfoRecordSet extends \ArrayObject | |
{ | |
public function getIterator() | |
{ | |
$this->ksort(); | |
return parent::getIterator(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MultipleResponseSets extends Info | |
{ | |
public const SUBTYPE = 19; | |
public const TYPE_CATEGORIES = 'C'; | |
public const TYPE_DICHOTOMIES_VAR_LABELS = 'D'; | |
public const TYPE_DICHOTOMIES_COUNTED_LABELS = 'E'; | |
/** | |
* @param string $name | |
* @param string $type | |
* @param Variable[] $variables | |
* @param string|null $label | |
*/ | |
public function addSet( | |
string $name, | |
string $type, | |
array $variables, | |
?string $label = null | |
) { | |
$this->data[] = [ | |
'name' => $name, | |
'type' => $type, | |
'label' => $label, | |
'variables' => $variables | |
]; | |
} | |
public function write(Buffer $buffer) | |
{ | |
$data = ''; | |
foreach($this->data as [ | |
'name' => $name, | |
'type' => $type, | |
'label' => $label, | |
'variables' => $variables | |
]) { | |
$countedValue = 1; | |
$countedValueLength = strlen($countedValue); | |
$labelLength = $label ? strlen($label) : 0; | |
$data .= '$' . $name . "={$type} 1 $countedValueLength $countedValue $labelLength $label"; | |
foreach($variables as $variable) { | |
$data .= ' ' . $variable->name; | |
} | |
$data .= "\n"; | |
} | |
$this->dataCount = strlen($data); | |
parent::write($buffer); | |
$buffer->write($data); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$writer = new Writer(); | |
// Since the writer puts data into the info record set, but SPSS file format spec requires info records to be ordered I implemented a class that enforces ordered iteration. | |
$writer->info = new InfoRecordSet(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment