Blame view

php/classes/TimeTableCacheFilterObject.php 6.71 KB
2a24088c   Benjamin Renard   Split some source...
1
2
<?php

2f1cd444   Benjamin Renard   Apply convention ...
3
class TimeTableCacheFilterPartObject
2a24088c   Benjamin Renard   Split some source...
4
5
6
7
8
9
10
11
12
13
14
15
16
{
	public static $TYPE_UNKNOWN       = 0;
	public static $TYPE_START         = 1;
	public static $TYPE_STOP          = 2;
	public static $TYPE_DURATION_SEC  = 3;
	public static $TYPE_DURATION_MIN  = 4;
	public static $TYPE_DURATION_HOUR = 5;

	public static $OPERATION_UNKNOWN   = 0;
	public static $OPERATION_LT        = 1;
	public static $OPERATION_GT        = 2;
	public static $OPERATION_EQ        = 3;

e022e5b5   Benjamin Renard   Filter parameter ...
17
	protected $cacheObject = NULL;
2a24088c   Benjamin Renard   Split some source...
18
19
20
21
	protected $type;
	protected $op;
	protected $value;

e022e5b5   Benjamin Renard   Filter parameter ...
22
	function __construct($cacheObject) {
2a24088c   Benjamin Renard   Split some source...
23
24
		$this->type  = self::$TYPE_UNKNOWN;
		$this->op    = self::$OPERATION_UNKNOWN;
e022e5b5   Benjamin Renard   Filter parameter ...
25
26
		$this->value = 0;
		$this->cacheObject = $cacheObject;
2a24088c   Benjamin Renard   Split some source...
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
	}

	public function getType() {
		return $this->type;
	}

	public function getOp() {
		return $this->op;
	}

	public function getValue() {
		return $this->value;
	}

	public function isSame($part) {
		return (($this->type == $part->getType()) && ($this->op == $part->getOp()) && ($this->value == $part->getValue()));
	}

	public function toFiltered($interval) {
		switch ($this->type) {
			case self::$TYPE_START :
				{
					switch ($this->op) {
						case self::$OPERATION_LT :
							return ($interval->getStartToStamp() < $this->value);
						case self::$OPERATION_GT :
							return ($interval->getStartToStamp() > $this->value);
						case self::$OPERATION_EQ :
							return (!(($interval->getStartToStamp() >= $this->value) && ($interval->getStartToStamp() <= $this->value+86400)));
						default :
							return false;
					}
				}
				break;
			case self::$TYPE_STOP :
				{
					switch ($this->op) {
						case self::$OPERATION_LT :
							return ($interval->getStopToStamp() < $this->value);
						case self::$OPERATION_GT :
							return ($interval->getStopToStamp() > $this->value);
						case self::$OPERATION_EQ :
							return (!(($interval->getStopToStamp() >= $this->value) && ($interval->getStopToStamp() <= $this->value+86400)));
						default :
							return false;
					}
				}
				break;
			case self::$TYPE_DURATION_SEC :
			case self::$TYPE_DURATION_MIN :
			case self::$TYPE_DURATION_HOUR :
				{
					$value = $this->value;
					if ($this->type == self::$TYPE_DURATION_MIN)
						$value *= 60;
					else if ($this->type == self::$TYPE_DURATION_HOUR)
						$value *= 3600;
					switch ($this->op) {
						case self::$OPERATION_LT :
							return ($interval->getDuration() < $value);
						case self::$OPERATION_GT :
							return ($interval->getDuration() > $value);
						case self::$OPERATION_EQ :
							return ($interval->getDuration() != $value);
						default :
							return false;
					}
				}
				break;
			default:
				return false;
		}
	}

	public function loadFromObject($part_obj) {
		$this->value = 0.;
		switch ($part_obj->field)
		{
			case 'start'        :
				$this->value = TimeUtils::iso2stamp($part_obj->value);
				$this->type = self::$TYPE_START;
				break;
			case 'stop'         :
				$this->value = TimeUtils::iso2stamp($part_obj->value);
				$this->type = self::$TYPE_STOP;
				break;
			case 'durationMin'  :
				$this->value = $part_obj->value;
				$this->type = self::$TYPE_DURATION_MIN;
				break;
			case 'durationHour' :
				$this->value = $part_obj->value;
				$this->type = self::$TYPE_DURATION_HOUR;
				break;
			case 'durationSec'  :
				$this->value = $part_obj->value;
				$this->type = self::$TYPE_DURATION_SEC;
				break;
			default:
				$this->value = 0.;
				$this->type = self::$TYPE_UNKNOWN;
		}

		switch ($part_obj->comparison)
		{
			case 'lt' :
				$this->op = self::$OPERATION_LT;
				break;
			case 'gt' :
				$this->op = self::$OPERATION_GT;
				break;
			case 'eq' :
				$this->op = self::$OPERATION_EQ;
				break;
			default:
				$this->op = self::$OPERATION_UNKNOWN;
		}
	}

	public function writeBin($handle) {
		fwrite($handle,pack('L2',$this->type,$this->op));
		fwrite($handle,pack('f',$this->value));
	}

	public function loadBin($handle) {
		$res = unpack('L2data',fread($handle,4*2));
		$this->type = $res['data1'];
		$this->op   = $res['data2'];

		$res = unpack('fvalue',fread($handle,4));
		$this->value = $res['value'];
	}

	public function dump() {
e022e5b5   Benjamin Renard   Filter parameter ...
161
		echo "   => ".get_class($this)." : type = ";
2a24088c   Benjamin Renard   Split some source...
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
		switch ($this->type)
		{
			case self::$TYPE_START :
				echo "start";
				break;
			case self::$TYPE_STOP :
				echo "stop";
				break;
			case self::$TYPE_DURATION_SEC :
				echo "duration seconde";
				break;
			case self::$TYPE_DURATION_MIN :
				echo "duration minute";
				break;
			case self::$TYPE_DURATION_HOUR :
				echo "duration hour";
				break;
			default:
				echo "unknown";
		}
		echo ", operation = ";
		switch ($this->op)
		{
			case self::$OPERATION_LT :
				echo "lt";
				break;
			case self::$OPERATION_GT :
				echo "gt";
				break;
			case self::$OPERATION_EQ :
				echo "eq";
				break;
			default:
				echo "unknown";
		}
		echo ", value = ".$this->value.PHP_EOL;
	}
}

2f1cd444   Benjamin Renard   Apply convention ...
201
class TimeTableCacheFilterObject
2a24088c   Benjamin Renard   Split some source...
202
{
e022e5b5   Benjamin Renard   Filter parameter ...
203
	protected $cacheObject = NULL;
2a24088c   Benjamin Renard   Split some source...
204
205
	protected $parts = array();

e022e5b5   Benjamin Renard   Filter parameter ...
206
207
208
	protected function createNewPart() {
		return new TimeTableCacheFilterPartObject($this->cacheObject);
	}
2a24088c   Benjamin Renard   Split some source...
209

e022e5b5   Benjamin Renard   Filter parameter ...
210
211
	function __construct($cacheObject) {
		$this->cacheObject = $cacheObject;
2a24088c   Benjamin Renard   Split some source...
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
	}

	public function getParts() {
		return $this->parts;
	}

	public function reset() {
		$this->parts = array();
	}

	public function isEmpty() {
		return (count($this->parts) == 0);
	}

	public function loadFromJSON($filter_json) {
		$this->reset();
		$filter_obj = json_decode($filter_json);

		foreach ($filter_obj as $filter_part)
		{
e022e5b5   Benjamin Renard   Filter parameter ...
232
			$part = $this->createNewPart();
2a24088c   Benjamin Renard   Split some source...
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
			$part->loadFromObject($filter_part);
			array_push($this->parts, $part);
		}
	}

	public function isSame($filter) {
		if (count($this->parts) != count($filter->getParts()))
			return false;

		$identique = true;
		for ($i = 0; $i < count($this->parts); ++$i)
		{
			if (!$this->parts[$i]->isSame($filter->getParts()[$i]))
			{
				return false;
			}
		}

		return true;
	}

	public function isSameFromJSON($filter_json) {
e022e5b5   Benjamin Renard   Filter parameter ...
255
256
		$this_class = get_class();
		$filter = new $this_class($this->cacheObject);
2a24088c   Benjamin Renard   Split some source...
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
		$filter->loadFromJSON($filter_json);
		return $this->isSame($filter);
	}

	public function toFiltered($interval) {
		foreach ($this->parts as $part)
		{
			if ($part->toFiltered($interval))
				return true;
		}
		return false;
	}

	public function writeBin($handle) {
		fwrite($handle,pack('L',count($this->parts)));
		foreach ($this->parts as $part)
			$part->writeBin($handle);
	}

	public function loadBin($handle) {
		$this->reset();
		$res = unpack('Lcount',fread($handle,4));
		for ($i = 0; $i < $res['count']; ++$i)
		{
e022e5b5   Benjamin Renard   Filter parameter ...
281
			$part = $this->createNewPart();
2a24088c   Benjamin Renard   Split some source...
282
283
284
285
286
287
			$part->loadBin($handle);
			array_push($this->parts, $part);
		}
	}

	public function dump() {
e022e5b5   Benjamin Renard   Filter parameter ...
288
		echo " => ".get_class($this)." : number of parts = ".count($this->parts).PHP_EOL;
2a24088c   Benjamin Renard   Split some source...
289
290
291
292
293
294
		foreach ($this->parts as $part)
			$part->dump();
	}
}

 ?>