forked from tiamo/spss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPSSWriter.php
441 lines (394 loc) · 10.7 KB
/
SPSSWriter.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
/**
* SPSS Php Writer
*
* @package fom.spss
* @author [email protected]
*/
require_once 'SPSSVariable.php';
class SPSSException extends Exception {}
class SPSSWriter
{
const FILE_SIGNATURE = '$FL2';
const RECORD_TYPE_VARIABLE = 2;
const RECORD_TYPE_VALUE_LABELS = 3;
const RECORD_TYPE_VALUE_LABELS_INDEX = 4;
const RECORD_TYPE_DOCUMENTS = 6;
const RECORD_TYPE_ADDITIONAL = 7;
const RECORD_TYPE_FINAL = 999;
// public $writer = '@(#) SPSS DATA FILE MS Windows 17.0.0';
public $writer = '@(#) FOM SPSS 1.0.0'; // 60 bytes max
public $fileLabel = ''; // 64 bytes max
public $release = array(1,0,0); // 64 bytes max
public $charset = 'windows-1251';
public $compression = 0; // compression bias
public $numberOfCases = 1;
public $machineCode = 720;
public $sysmis = -0x1fffffffffffff * 2.0**971;
// public $sysmis = -1.79769313486E+308; // sysmis
public $variables = array();
public $documents = array();
private $bytes = ''; // bytes raw
public function make()
{
if (!$this->variables) {
throw new SPSSException('Variables data is empty');
}
// header
$this->bytes .= $this->_header();
// variables record
foreach($this->variables as $var) {
$this->bytes .= $this->_variableRecord($var);
}
// value labels record
foreach($this->variables as $var) {
$this->bytes .= $this->_valueLabelRecord($var);
}
// documents record
$this->bytes .= $this->_documentsRecord();
// additional record
$this->bytes .= $this->_additionalRecord();
// final record
$this->bytes .= $this->_finalRecord();
// data
$this->bytes .= $this->_data();
}
/**
* Save bytes to file
*
* @param string $filename
* @return string
*/
public function save($filename=null)
{
if (!$filename) {
$filename = time() . '.sav';
}
if ($fp = fopen($filename, 'wb+')) {
fwrite($fp, $this->bytes);
fclose($fp);
}
}
/**
* Get bytes raw data
*
* @return string
*/
public function getBytes()
{
return $this->bytes;
}
/**
* Make header information
* @return string
*/
private function _header()
{
$bytes = '';
$bytes .= pack('a4', self::FILE_SIGNATURE);
$bytes .= pack('A60', $this->writer); // identification
$bytes .= pack('i', 2); // layoutCode
$bytes .= pack('i', count($this->variables)); // numberOfVariables
$bytes .= pack('i', (int) !empty($this->compression)); // compressionSwitch
$bytes .= pack('i', 0); // caseWeightVariable
$bytes .= pack('i', $this->numberOfCases); // numberOfCases
$bytes .= pack('d', $this->compression); // compressionBias
$bytes .= pack('a9', date('d M y')); // creation date
$bytes .= pack('a8', date('H:i:s')); // creation time
$bytes .= pack('A64', $this->fixstr($this->fileLabel)); // file label
$bytes .= pack('x3'); // padding
return $bytes;
}
/**
* Make variable (Record type 2)
* @return string
*/
private function _variableRecord(SPSSVariable $var)
{
$hasLabel = (int) !empty($var->label);
$bytes = '';
$bytes .= pack('i', self::RECORD_TYPE_VARIABLE); // type variable
$bytes .= pack('i', $var->typeCode); // typeCode
$bytes .= pack('i', $hasLabel); // hasLabel
$bytes .= pack('i', $var->missingValueFormatCode); // missingValueFormatCode (-3 to 3)
$bytes .= pack('i', $var->printFormatCode); // printFormatCode
$bytes .= pack('i', $var->writeFormatCode); // writeFormatCode
$bytes .= pack('A8', $this->fixstr($var->shortName)); // shortName
// has labels
if ($hasLabel) {
$label = $this->fixstr($var->label);
$labelLength = strlen($var->label);
// variableRecord labels are stored in chunks of 4-bytes
// --> we need to skip unused bytes in the last chunk
$skipBytes = 0;
if ($labelLength % 4 != 0) {
$skipBytes = 4 - ($labelLength % 4);
}
$bytes .= pack('i', $labelLength); // label length
$bytes .= pack('A'.($labelLength+$skipBytes), $label); // label
}
// missing values
if ($var->missingValues) {
foreach($var->missingValues as $key => $val) {
$bytes .= pack('d', $val);
}
}
// padding
if ($var->typeCode > 0) {
for($k = 8; $k < $var->typeCode; $k += 8) {
$fcode = self::toInt(array(1, 29, 1, 0));
$bytes .= pack('i6', 2, -1, 0, 0, $fcode, $fcode);
$bytes .= ' ';
}
}
return $bytes;
}
/**
* Make value labels (Record type 3,4)
* @return string
*/
private function _valueLabelRecord(SPSSVariable $var)
{
$index = $this->getVarIndex($var);
if (!$var->valueLabels) {
return;
}
$bytes = '';
$bytes .= pack('i', self::RECORD_TYPE_VALUE_LABELS);
$bytes .= pack('i', count($var->valueLabels)); // number of labels
foreach($var->valueLabels as $key => $val) {
$label = $this->fixstr($val);
$labelLength = strlen($val);
if ($labelLength>255) {
$labelLength = 255;
}
$skipBytes = 0;
if (($labelLength+1) % 8){
$skipBytes = 8 - (($labelLength+1) % 8);
}
$bytes .= pack('d', $key);
$bytes .= chr($labelLength);
$bytes .= pack('A'.($labelLength+$skipBytes), $label);
}
// record type 4 (value labels index)
$bytes .= pack('i', self::RECORD_TYPE_VALUE_LABELS_INDEX);
$bytes .= pack('i', 1);
$bytes .= pack('i', $index);
return $bytes;
}
/**
* @param SPSSVariable $var
* @return integer
*/
public function getVarIndex($var)
{
static $index;
// todo: get index by other typecodes
if ($var->typeCode>0) {
$index += ceil($var->getWidth()/8);
}
else {
$index++;
}
return $index;
}
/**
* Make documents (Record type 6)
* @return string bytes
*/
private function _documentsRecord()
{
if (!$this->documents) {
return;
}
$bytes = pack('i', self::RECORD_TYPE_DOCUMENTS);
$bytes .= pack('i', count($this->documents));
foreach($this->documents as $line) {
$bytes .= pack('A80', $this->fixstr($line)); // document line 80 bytes max
}
return $bytes;
}
/**
* Make additional (Record type 7)
* @return string bytes
*/
private function _additionalRecord()
{
$bytes = $this->_additional7_3(); // source system characteristics
$bytes .= $this->_additional7_4(); // machine specific "float" type information.
$bytes .= $this->_additional7_11(); // variable params
$bytes .= $this->_additional7_13(); // extended names
$bytes .= $this->_additional7_16(); // number of cases
$bytes .= $this->_additional7_20(); // charset
return $bytes;
}
/**
* SPSS Record Type 7 Subtype 3 - Source system characteristics
* @return string bytes
*/
private function _additional7_3()
{
$bytes = '';
$bytes .= pack('i', self::RECORD_TYPE_ADDITIONAL);
$bytes .= pack('i', 3);
$bytes .= pack('i', 4);
$bytes .= pack('i', 8);
$bytes .= pack('i', $this->release[0]); // releaseMajor
$bytes .= pack('i', $this->release[1]); // releaseMinor
$bytes .= pack('i', $this->release[2]); // releaseSpecial
$bytes .= pack('i', $this->machineCode); // machineCode
$bytes .= pack('i', 1); // floatRepresentation
$bytes .= pack('i', 1); // compressionScheme
$bytes .= pack('i', 2); // endianCode (Little-endian)
$bytes .= pack('i', 1251); // characterRepresentation (7-bit ASCII)
return $bytes;
}
/**
* SPSS Record Type 7 Subtype 4 - Release and machine specific "float" type information. Added in SPSS release 4.0
* @return string bytes
*/
private function _additional7_4()
{
$bytes = '';
$bytes .= pack('i', self::RECORD_TYPE_ADDITIONAL);
$bytes .= pack('i', 4);
$bytes .= pack('i', 8);
$bytes .= pack('i', 3);
$bytes .= pack('d', $this->sysmis); // system missing value
$bytes .= pack('d', -$this->sysmis); // value for HIGHEST in missing values and recode
$bytes .= pack('d', $this->sysmis); // value for LOWEST in missing values and recode
return $bytes;
}
/**
* SPSS Record Type 7 Subtype 11 - Variable params
* @return string bytes
*/
private function _additional7_11()
{
$bytes = '';
$bytes .= pack('i', self::RECORD_TYPE_ADDITIONAL);
$bytes .= pack('i', 11);
$bytes .= pack('i', 4); // size
$bytes .= pack('i', count($this->variables) * 3); // count
foreach($this->variables as $var) {
$bytes .= pack('i', $var->measure);
$bytes .= pack('i', $var->width);
$bytes .= pack('i', $var->alignment);
}
return $bytes;
}
/**
* SPSS Record Type 7 Subtype 13 - Variable extended names
* @return string bytes
*/
private function _additional7_13()
{
$bytes = '';
$bytes .= pack('i', self::RECORD_TYPE_ADDITIONAL);
$bytes .= pack('i', 13);
$bytes .= pack('i', 1); // size
$data = array();
foreach($this->variables as $var) {
$data[] = $var->shortName.'='.(!empty($var->name) ? $var->name : $var->shortName);
}
$data = implode("\t", $data);
$datalen = strlen($data);
$bytes .= pack('i', $datalen); // count
$bytes .= pack('a'.$datalen, $this->fixstr($data));
return $bytes;
}
/**
* SPSS Record Type 7 Subtype 16 - Number of cases
* @return string bytes
*/
private function _additional7_16()
{
$bytes = '';
$bytes .= pack('i', self::RECORD_TYPE_ADDITIONAL);
$bytes .= pack('i', 16);
$bytes .= pack('i', 8); // size
$bytes .= pack('i', 2); // count
$bytes .= pack('i', 1); // byte order
$bytes .= pack('i', 0);
$bytes .= pack('d', 0); // count
return $bytes;
}
/**
* SPSS Record Type 7 Subtype 20 - Charset
* @return string bytes
*/
private function _additional7_20()
{
$bytes = '';
$bytes .= pack('i', self::RECORD_TYPE_ADDITIONAL);
$bytes .= pack('i', 20);
$bytes .= pack('i', 1); // size
$bytes .= pack('i', strlen($this->charset)); // count
$bytes .= pack('a*', $this->charset);
return $bytes;
}
/**
* Make final (Record type 999)
* @return string bytes
*/
private function _finalRecord()
{
$bytes = pack('i', self::RECORD_TYPE_FINAL);
$bytes .= pack('i', 0);
return $bytes;
}
/**
* Make data
* @return string bytes
*/
private function _data()
{
$bytes = '';
for($i=0;$i<$this->numberOfCases;$i++) {
foreach($this->variables as $key => $var) {
$varType = $var->getType();
$value = isset($var->data[$i]) ? $var->data[$i] : null;
if ($this->compression) {
// @todo
}
else {
if ($varType==SPSSVariable::TYPE_NUMERIC) {
$bytes .= pack('d', $value);
}
else {
$bytes .= pack('a'.$var->typeCode, $this->fixstr($value));
$skipBytes = 0;
if ($var->typeCode % 8){
$skipBytes = 8 - ($var->typeCode % 8);
}
$bytes .= pack('x'.$skipBytes);
}
}
}
}
return $bytes;
}
/**
* Fix string charset
*
* @params string $str
* @return string
*/
private function fixstr($str)
{
if ($this->charset !='utf-8') {
$str = iconv('utf-8', $this->charset, $str);
}
return $str;
}
/**
* Convert bytes array to integer
*
* @params array $bytes
* @return integer
*/
public static function toInt($bytes)
{
return $bytes[3]<<24 | $bytes[2]<<16 | $bytes[1]<<8 | $bytes[0]<<0;
}
}