-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathLongStringValueLabels.php
70 lines (63 loc) · 2.67 KB
/
LongStringValueLabels.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace SPSS\Sav\Record\Info;
use SPSS\Buffer;
use SPSS\Sav\Record\Info;
use SPSS\Utils;
class LongStringValueLabels extends Info
{
const SUBTYPE = 21;
public $data = [];
public function read(Buffer $buffer)
{
parent::read($buffer);
$buffer = $buffer->allocate($this->dataCount * $this->dataSize);
while ($varNameLength = $buffer->readInt()) {
$varName = $buffer->readString($varNameLength);
$varWidth = $buffer->readInt(); // The width of the variable, in bytes, which will be between 9 and 32767
$valuesCount = $buffer->readInt();
$this->data[$varName] = [
'width' => $varWidth,
'values' => [],
];
for ($i = 0; $i < $valuesCount; $i++) {
$valueLength = $buffer->readInt();
$value = rtrim($buffer->readString($valueLength));
$labelLength = $buffer->readInt();
$label = rtrim($buffer->readString($labelLength));
$this->data[$varName]['values'][$value] = $label;
}
}
}
public function write(Buffer $buffer)
{
$localBuffer = Buffer::factory('', ['memory' => true]);
foreach ($this->data as $varName => $data) {
if (!isset($data['width'])) {
throw new \InvalidArgumentException('width required');
}
if (!isset($data['values'])) {
throw new \InvalidArgumentException('values required');
}
$width = (int) $data['width'];
$varLengthBytes = $buffer->lengthBytes($varName);
$localBuffer->writeInt($varLengthBytes);
$localBuffer->writeString($varName, $varLengthBytes);
$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);
$labelLengthBytes = $buffer->lengthBytes($label);
$localBuffer->writeInt($labelLengthBytes);
$localBuffer->writeString($label, $labelLengthBytes);
}
}
// retrieve bytes count
$this->dataCount = $localBuffer->position();
if ($this->dataCount > 0) {
parent::write($buffer);
$localBuffer->rewind();
$buffer->writeStream($localBuffer->getStream());
}
}
}