type = self::$TYPE_UNKNOWN; $this->dir = self::$DIRECTION_UNKNOWN; $this->cacheObject = $cacheObject; } 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 : case self::$TYPE_DURATION_DAY : { 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; case 'durationDay' : $this->type = self::$TYPE_DURATION_DAY; 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() { echo " => ".get_class($this)." : type = "; 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; case self::$TYPE_DURATION_DAY : echo "duration day"; 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; } } class TimeTableCacheSortObject { protected $parts = array(); protected $cacheObject = NULL; protected function createNewPart() { return new TimeTableCacheSortPartObject($this->cacheObject); } function __construct($cacheObject) { $this->cacheObject = $cacheObject; } 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) { $part = $this->createNewPart(); $part->loadFromObject($sort_part); array_push($this->parts, $part); } } public function isSameFromObject($sort_obj) { $this_class = get_class(); $sort = new $this_class($this->cacheObject); $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 > 0) ? 1 : -1; } 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) { $part = $this->createNewPart(); $part->loadBin($handle); array_push($this->parts, $part); } } public function dump() { echo " => ".get_class($this)." : number of parts = ".count($this->parts).PHP_EOL; foreach ($this->parts as $part) $part->dump(); } } ?>