-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathVariableAttributes.php
52 lines (45 loc) · 1.43 KB
/
VariableAttributes.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
<?php
namespace SPSS\Sav\Record\Info;
use SPSS\Buffer;
use SPSS\Sav\Record\Info;
class VariableAttributes extends Info
{
const SUBTYPE = 18;
public $data = [];
public function read(Buffer $buffer)
{
parent::read($buffer);
$data = $buffer->readString($this->dataSize * $this->dataCount);
foreach (explode('/', $data) as $item) {
list($var, $value) = explode(':', $item);
if (preg_match_all('#(.+)\((.+)\)#Uis', $value, $matches)) {
$this->data[$var] = [];
foreach ($matches[1] as $key => $val) {
$this->data[$var][$val] = trim(trim($matches[2][$key]), '\'');
}
} else {
$this->data[$var] = $value;
}
}
}
public function write(Buffer $buffer)
{
$lines = [];
foreach ($this->data as $var => $value) {
if (\is_array($value)) {
$_tmpString = '';
foreach ($value as $key => $val) {
$_tmpString .= sprintf("%s('%s'\n)", $key, $val);
}
$value = $_tmpString;
}
$lines[] = sprintf('%s:%s', $var, $value);
}
if ($lines !== []) {
$data = implode('/', $lines);
$this->dataCount = \strlen($data);
parent::write($buffer);
$buffer->writeString($data);
}
}
}