|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SPSS\Sav; |
|
4
|
|
|
|
|
5
|
|
|
use SPSS\Buffer; |
|
6
|
|
|
|
|
7
|
|
|
class Reader |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var \SPSS\Sav\Record\Header |
|
11
|
|
|
*/ |
|
12
|
|
|
public $header; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var \SPSS\Sav\Record\Variable[] |
|
16
|
|
|
*/ |
|
17
|
|
|
public $variables = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var \SPSS\Sav\Record\ValueLabel[] |
|
21
|
|
|
*/ |
|
22
|
|
|
public $valueLabels = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
public $documents = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var \SPSS\Sav\Record\Info[] |
|
31
|
|
|
*/ |
|
32
|
|
|
public $info = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
public $data = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var \SPSS\Buffer |
|
41
|
|
|
*/ |
|
42
|
|
|
public $_buffer; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Reader constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param \SPSS\Buffer $buffer |
|
48
|
|
|
*/ |
|
49
|
|
|
private function __construct(Buffer $buffer) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->_buffer = $buffer; |
|
52
|
|
|
$this->_buffer->context = $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $file |
|
57
|
|
|
* @return \SPSS\Sav\Reader |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function fromFile($file) |
|
60
|
|
|
{ |
|
61
|
|
|
return new self(Buffer::factory(fopen($file, 'r'))); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $str |
|
66
|
|
|
* @return \SPSS\Sav\Reader |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function fromString($str) |
|
69
|
|
|
{ |
|
70
|
|
|
return new self(Buffer::factory($str)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return $this |
|
75
|
|
|
*/ |
|
76
|
|
|
public function read() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->readHeader()->readBody()->readData(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return $this |
|
83
|
|
|
*/ |
|
84
|
|
|
public function readHeader() |
|
85
|
|
|
{ |
|
86
|
|
|
$this->header = Record\Header::fill($this->_buffer); |
|
87
|
|
|
|
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return $this |
|
93
|
|
|
*/ |
|
94
|
|
|
public function readBody() |
|
95
|
|
|
{ |
|
96
|
|
|
if (! $this->header) { |
|
97
|
|
|
$this->readHeader(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// TODO: refactory |
|
101
|
|
|
$infoCollection = new Record\InfoCollection(); |
|
102
|
|
|
|
|
103
|
|
|
do { |
|
104
|
|
|
$recType = $this->_buffer->readInt(); |
|
105
|
|
|
switch ($recType) { |
|
106
|
|
|
case Record\Variable::TYPE: |
|
107
|
|
|
$this->variables[] = Record\Variable::fill($this->_buffer); |
|
108
|
|
|
break; |
|
109
|
|
|
case Record\ValueLabel::TYPE: |
|
110
|
|
|
$this->valueLabels[] = Record\ValueLabel::fill($this->_buffer, [ |
|
111
|
|
|
// TODO: refactory |
|
112
|
|
|
'variables' => $this->variables, |
|
113
|
|
|
]); |
|
114
|
|
|
break; |
|
115
|
|
|
case Record\Info::TYPE: |
|
116
|
|
|
$this->info = $infoCollection->fill($this->_buffer); |
|
117
|
|
|
break; |
|
118
|
|
|
case Record\Document::TYPE: |
|
119
|
|
|
$this->documents = Record\Document::fill($this->_buffer)->toArray(); |
|
120
|
|
|
break; |
|
121
|
|
|
} |
|
122
|
|
|
} while ($recType != Record\Data::TYPE); |
|
123
|
|
|
|
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return $this |
|
129
|
|
|
*/ |
|
130
|
|
|
public function readData() |
|
131
|
|
|
{ |
|
132
|
|
|
$this->data = Record\Data::fill($this->_buffer)->toArray(); |
|
133
|
|
|
|
|
134
|
|
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|