Blame view

php/classes/TimeTableCacheSortObject.php 5.88 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
11
12
13
14
15
{
	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 $DIRECTION_UNKNOWN = 0;
	public static $DIRECTION_ASC     = 1;
	public static $DIRECTION_DES     = 2;

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

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

	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 :
				{
					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;
			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...
124
		echo "   => ".get_class($this)." : type = ";
2a24088c   Benjamin Renard   Split some source...
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
		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 ", 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 ...
161
class TimeTableCacheSortObject
2a24088c   Benjamin Renard   Split some source...
162
163
{
	protected $parts = array();
ab7c3c63   Benjamin Renard   Sort parameter co...
164
	protected $cacheObject = NULL;
2a24088c   Benjamin Renard   Split some source...
165

ab7c3c63   Benjamin Renard   Sort parameter co...
166
167
168
169
170
171
	protected function createNewPart() {
		return new TimeTableCacheSortPartObject($this->cacheObject);
	}

	function __construct($cacheObject) {
		$this->cacheObject = $cacheObject;
2a24088c   Benjamin Renard   Split some source...
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
	}

	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...
190
			$part = $this->createNewPart();
2a24088c   Benjamin Renard   Split some source...
191
192
193
194
195
196
			$part->loadFromObject($sort_part);
			array_push($this->parts, $part);
		}
	}

	public function isSameFromObject($sort_obj) {
ab7c3c63   Benjamin Renard   Sort parameter co...
197
198
		$this_class = get_class();
		$sort = new $this_class($this->cacheObject);
2a24088c   Benjamin Renard   Split some source...
199
200
201
202
203
204
205
206
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
246
247
248
249
250
251
252
253
254
255
256
		$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)
					return $res;
			}
			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...
257
			$part = $this->createNewPart();
2a24088c   Benjamin Renard   Split some source...
258
259
260
261
262
263
			$part->loadBin($handle);
			array_push($this->parts, $part);
		}
	}

	public function dump() {
ab7c3c63   Benjamin Renard   Sort parameter co...
264
		echo " => ".get_class($this)." : number of parts = ".count($this->parts).PHP_EOL;
2a24088c   Benjamin Renard   Split some source...
265
266
267
268
269
270
		foreach ($this->parts as $part)
			$part->dump();
	}
}

 ?>