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