Blame view

php/classes/TimeTableCacheSortObject.php 6.12 KB
2a24088c   Benjamin Renard   Split some source...
1
2
<?php

2f1cd444   Benjamin Renard   Apply convention ...
3
class TimeTableCacheSortPartObject
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

	public static $DIRECTION_UNKNOWN = 0;
	public static $DIRECTION_ASC     = 1;
	public static $DIRECTION_DES     = 2;

ab7c3c63   Benjamin Renard   Sort parameter co...
17
  protected $cacheObject = NULL;
2a24088c   Benjamin Renard   Split some source...
18
19
20
	protected $type;
	protected $dir;

ab7c3c63   Benjamin Renard   Sort parameter co...
21
	function __construct($cacheObject) {
2a24088c   Benjamin Renard   Split some source...
22
23
		$this->type = self::$TYPE_UNKNOWN;
		$this->dir  = self::$DIRECTION_UNKNOWN;
ab7c3c63   Benjamin Renard   Sort parameter co...
24
		$this->cacheObject = $cacheObject;
2a24088c   Benjamin Renard   Split some source...
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
	}

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

	public function getDir() {
		return $this->dir;
	}

	public function isSame($part) {
		return (($this->type == $part->getType()) && ($this->dir == $part->getDir()));
	}

	public function compare($interval_a, $interval_b) {
		switch ($this->type) {
			case self::$TYPE_START :
				{
					switch ($this->dir) {
						case self::$DIRECTION_ASC :
							return ($interval_b->getStartToStamp() - $interval_a->getStartToStamp());
						default :
							return ($interval_a->getStartToStamp() - $interval_b->getStartToStamp());
					}
				}
				break;
			case self::$TYPE_STOP :
				{
					switch ($this->dir) {
						case self::$DIRECTION_ASC :
							return ($interval_b->getStopToStamp() - $interval_a->getStopToStamp());
						default :
							return ($interval_a->getStopToStamp() - $interval_b->getStopToStamp());
					}
				}
				break;
			case self::$TYPE_DURATION_SEC :
			case self::$TYPE_DURATION_MIN :
			case self::$TYPE_DURATION_HOUR :
3eab488e   Hacene SI HADJ MOHAND   filters ok
64
			case self::$TYPE_DURATION_DAY :
2a24088c   Benjamin Renard   Split some source...
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
				{
					switch ($this->dir) {
						case self::$DIRECTION_ASC :
							return ($interval_b->getDuration() - $interval_a->getDuration());
						default :
							return ($interval_a->getDuration() - $interval_b->getDuration());
					}
				}
				break;
			default :
				return 0;
		}
		return 0;
	}

	public function loadFromObject($part_obj) {
		switch ($part_obj->property)
		{
			case 'start'        :
				$this->type = self::$TYPE_START;
				break;
			case 'stop'         :
				$this->type = self::$TYPE_STOP;
				break;
			case 'durationMin'  :
				$this->type = self::$TYPE_DURATION_MIN;
				break;
			case 'durationHour' :
				$this->type = self::$TYPE_DURATION_HOUR;
				break;
			case 'durationSec'  :
				$this->type = self::$TYPE_DURATION_SEC;
				break;
3eab488e   Hacene SI HADJ MOHAND   filters ok
98
99
100
			case 'durationDay'  :
				$this->type = self::$TYPE_DURATION_DAY;
				break;
2a24088c   Benjamin Renard   Split some source...
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
			default:
				$this->type = self::$TYPE_UNKNOWN;
		}

		switch ($part_obj->direction)
		{
			case 'ASC' :
				$this->dir = self::$DIRECTION_ASC;
				break;
			case 'DESC' :
				$this->dir = self::$DIRECTION_DES;
				break;
			default:
				$this->dir = self::$DIRECTION_UNKNOWN;
		}
	}

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

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

	public function dump() {
ab7c3c63   Benjamin Renard   Sort parameter co...
129
		echo "   => ".get_class($this)." : type = ";
2a24088c   Benjamin Renard   Split some source...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
		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
147
148
149
			case self::$TYPE_DURATION_DAY :
				echo "duration day";
				break;
2a24088c   Benjamin Renard   Split some source...
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
			default:
				echo "unknown";
		}
		echo ", direction = ";
		switch ($this->dir)
		{
			case self::$DIRECTION_ASC :
				echo "ASC";
				break;
			case self::$DIRECTION_DES :
				echo "DESC";
				break;
			default:
				echo "unknown";
		}
		echo PHP_EOL;
	}
}

2f1cd444   Benjamin Renard   Apply convention ...
169
class TimeTableCacheSortObject
2a24088c   Benjamin Renard   Split some source...
170
171
{
	protected $parts = array();
ab7c3c63   Benjamin Renard   Sort parameter co...
172
	protected $cacheObject = NULL;
2a24088c   Benjamin Renard   Split some source...
173

ab7c3c63   Benjamin Renard   Sort parameter co...
174
175
176
177
178
179
	protected function createNewPart() {
		return new TimeTableCacheSortPartObject($this->cacheObject);
	}

	function __construct($cacheObject) {
		$this->cacheObject = $cacheObject;
2a24088c   Benjamin Renard   Split some source...
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
	}

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

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

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

	public function loadFromObject($sort_obj) {
		$this->reset();
		foreach ($sort_obj as $sort_part)
		{
ab7c3c63   Benjamin Renard   Sort parameter co...
198
			$part = $this->createNewPart();
2a24088c   Benjamin Renard   Split some source...
199
200
201
202
203
204
			$part->loadFromObject($sort_part);
			array_push($this->parts, $part);
		}
	}

	public function isSameFromObject($sort_obj) {
ab7c3c63   Benjamin Renard   Sort parameter co...
205
206
		$this_class = get_class();
		$sort = new $this_class($this->cacheObject);
2a24088c   Benjamin Renard   Split some source...
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
		$sort->loadFromObject($sort_obj);
		return $this->isSame($sort);
	}

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

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

		return true;
	}

	public function apply($intervals) {
		$sorted_indexes = array();

		global $global_parts, $global_intervals;
		$global_parts = $this->parts;
		$global_intervals = $intervals;

		foreach ($intervals as $interval)
			array_push($sorted_indexes, $interval->getIndex());

		if (count($global_parts) == 0)
			return $sorted_indexes;

		usort($sorted_indexes, function ($index_a, $index_b) {
			global $global_parts, $global_intervals;
			foreach ($global_parts as $part)
			{
				$res = $part->compare($global_intervals[$index_a], $global_intervals[$index_b]);
				if ($res != 0)
e0e05fa5   Benjamin Renard   Fix sort (#6897)
246
					return ($res > 0) ? 1 : -1;
2a24088c   Benjamin Renard   Split some source...
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
			}
			return $index_a-$index_b;
		});

		return $sorted_indexes;
	}

	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)
		{
ab7c3c63   Benjamin Renard   Sort parameter co...
265
			$part = $this->createNewPart();
2a24088c   Benjamin Renard   Split some source...
266
267
268
269
270
271
			$part->loadBin($handle);
			array_push($this->parts, $part);
		}
	}

	public function dump() {
ab7c3c63   Benjamin Renard   Sort parameter co...
272
		echo " => ".get_class($this)." : number of parts = ".count($this->parts).PHP_EOL;
2a24088c   Benjamin Renard   Split some source...
273
274
275
276
277
278
		foreach ($this->parts as $part)
			$part->dump();
	}
}

 ?>