Blame view

php/classes/TimeTableCacheFilterObject.php 7.07 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
{
	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;
3eab488e   Hacene SI HADJ MOHAND   filters ok
11
	public static $TYPE_DURATION_DAY  = 6;
2a24088c   Benjamin Renard   Split some source...
12
13
14
15
16
17

	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 ...
18
	protected $cacheObject = NULL;
2a24088c   Benjamin Renard   Split some source...
19
20
21
22
	protected $type;
	protected $op;
	protected $value;

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

	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 :
3eab488e   Hacene SI HADJ MOHAND   filters ok
79
			case self::$TYPE_DURATION_DAY :
2a24088c   Benjamin Renard   Split some source...
80
81
82
83
84
85
				{
					$value = $this->value;
					if ($this->type == self::$TYPE_DURATION_MIN)
						$value *= 60;
					else if ($this->type == self::$TYPE_DURATION_HOUR)
						$value *= 3600;
3eab488e   Hacene SI HADJ MOHAND   filters ok
86
87
					else if ($this->type == self::$TYPE_DURATION_DAY)
						$value *= 86400;
2a24088c   Benjamin Renard   Split some source...
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
					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;
3eab488e   Hacene SI HADJ MOHAND   filters ok
129
130
131
132
			case 'durationDay'  :
				$this->value = $part_obj->value;
				$this->type = self::$TYPE_DURATION_DAY;
                                break;
2a24088c   Benjamin Renard   Split some source...
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
			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 ...
169
		echo "   => ".get_class($this)." : type = ";
2a24088c   Benjamin Renard   Split some source...
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
		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;
3eab488e   Hacene SI HADJ MOHAND   filters ok
187
188
189
190

			case self::$TYPE_DURATION_DAY :
				echo "duration day";
				break;
2a24088c   Benjamin Renard   Split some source...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
			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 ...
213
class TimeTableCacheFilterObject
2a24088c   Benjamin Renard   Split some source...
214
{
e022e5b5   Benjamin Renard   Filter parameter ...
215
	protected $cacheObject = NULL;
2a24088c   Benjamin Renard   Split some source...
216
217
	protected $parts = array();

e022e5b5   Benjamin Renard   Filter parameter ...
218
219
220
	protected function createNewPart() {
		return new TimeTableCacheFilterPartObject($this->cacheObject);
	}
2a24088c   Benjamin Renard   Split some source...
221

e022e5b5   Benjamin Renard   Filter parameter ...
222
223
	function __construct($cacheObject) {
		$this->cacheObject = $cacheObject;
2a24088c   Benjamin Renard   Split some source...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
	}

	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 ...
244
			$part = $this->createNewPart();
2a24088c   Benjamin Renard   Split some source...
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
			$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 ...
267
268
		$this_class = get_class();
		$filter = new $this_class($this->cacheObject);
2a24088c   Benjamin Renard   Split some source...
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
		$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 ...
293
			$part = $this->createNewPart();
2a24088c   Benjamin Renard   Split some source...
294
295
296
297
298
299
			$part->loadBin($handle);
			array_push($this->parts, $part);
		}
	}

	public function dump() {
e022e5b5   Benjamin Renard   Filter parameter ...
300
		echo " => ".get_class($this)." : number of parts = ".count($this->parts).PHP_EOL;
2a24088c   Benjamin Renard   Split some source...
301
302
303
304
305
306
		foreach ($this->parts as $part)
			$part->dump();
	}
}

 ?>