CatalogCacheIntervalObject.php
3.72 KB
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
<?php
class CatalogCacheIntervalObject extends TimeTableCacheIntervalObject
{
// for catalog
private $params = array();
public function toArray()
{
$result = parent::toArray();
foreach ($this->cacheObject->getParametersInfo() as $parameter)
{
$result[$parameter['id']] = $this->params[$parameter['id']];
}
return $result;
}
// for catalog
public function setParamValue($param_id, $param_value)
{
$this->params[$param_id] = $param_value;
}
public function getParams()
{
return $this->params;
}
public function modifyInterval($data) {
parent::modifyInterval($data);
foreach ($this->cacheObject->getParametersInfo() as $parameter) {
if (array_key_exists($parameter['id'], $data)) {
$this->setParamValue($parameter['id'], $data->{$parameter['id']});
$this->setIsModified(true);
}
}
return $this->isModified();
}
public function writeBin($handle)
{
parent::writeBin($handle);
foreach ($this->cacheObject->getParametersInfo() as $parameter)
{
$size = $parameter['size'];
$values = $this->params[$parameter['id']];
if ($size > 1) {
$values_array = explode(',',$values);
}
else {
$values_array = array($values);
}
for ($i = 0; $i < $size; ++$i) {
$value = ($i >= count($values_array)) ? "" : $values_array[$i];
switch ($parameter['type']) {
case 0: //double
if($value == "nan")
$floatValue = NAN;
else
$floatValue = floatval($value);
fwrite($handle,pack('d', $floatValue));
break;
case 1: //date (timestamp)
fwrite($handle,pack('d', TimeUtils::iso2stamp($value)));
break;
case 2: //string
fwrite($handle,pack('L', strlen($value)));
for ($j = 0; $j < strlen($value); ++$j) {
fwrite($handle,pack('C', ord($value[$j])));
}
break;
case 3: //int
fwrite($handle,pack('l', intval($value)));
break;
default: // not defined => string
fwrite($handle,pack('L', strlen($value)));
for ($j = 0; $j < strlen($value); ++$j) {
fwrite($handle,pack('C', ord($value[$j])));
}
}
}
}
}
public function loadBin($handle)
{
parent::loadBin($handle);
foreach ($this->cacheObject->getParametersInfo() as $parameter) {
$size = $parameter['size'];
$id = $parameter['id'];
$values = array();
for ($i = 0; $i < $size; $i++)
{
$val = "";
switch ($parameter['type']) {
case 0: //double
if (!$res = unpack('dval',fread($handle,8))) {
break;
}
$val = is_nan($res['val']) ? "nan" : $res['val'];
break;
case 1: //date (timestamp)
if (!$res = unpack('dval',fread($handle,8))) {
break;
}
$val = TimeUtils::stamp2iso($res['val']);
break;
case 2: //string
if (!$res = unpack('Llength',fread($handle,4))) {
break;
}
$length = $res['length'];
$val = "";
for ($j = 0; $j < $length; ++$j) {
if (!$res = unpack('Cval',fread($handle,1))) {
break;
}
$val .= chr($res['val']);
}
break;
case 3: //int
if (!$res = unpack('lval',fread($handle,4))) {
break;
}
$val = $res['val'];
break;
default: // not defined => string
if (!$res = unpack('Llength',fread($handle,4))) {
break;
}
$length = $res['length'];
$val = "";
for ($j = 0; $j < $length; ++$j) {
if (!$res = unpack('Cval',fread($handle,1))) {
break;
}
$val .= chr($res['val']);
}
}
$values[] = $val;
}
$this->params[$id] = implode(',',$values);
}
}
public function dump()
{
parent::dump();
echo " parameters = ";
foreach ($this->params as $param) {
echo $param." - ";
}
echo PHP_EOL;
}
}
?>