TimeTableCacheFilterObject.php 7.07 KB
<?php

class TimeTableCacheFilterPartObject
{
	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 $TYPE_DURATION_DAY  = 6;

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

	protected $cacheObject = NULL;
	protected $type;
	protected $op;
	protected $value;

	function __construct($cacheObject) {
		$this->type  = self::$TYPE_UNKNOWN;
		$this->op    = self::$OPERATION_UNKNOWN;
		$this->value = 0;
		$this->cacheObject = $cacheObject;
	}

	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 :
			case self::$TYPE_DURATION_DAY :
				{
					$value = $this->value;
					if ($this->type == self::$TYPE_DURATION_MIN)
						$value *= 60;
					else if ($this->type == self::$TYPE_DURATION_HOUR)
						$value *= 3600;
					else if ($this->type == self::$TYPE_DURATION_DAY)
						$value *= 86400;
					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;
			case 'durationDay'  :
				$this->value = $part_obj->value;
				$this->type = self::$TYPE_DURATION_DAY;
                                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() {
		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 ", 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;
	}
}

class TimeTableCacheFilterObject
{
	protected $cacheObject = NULL;
	protected $parts = array();

	protected function createNewPart() {
		return new TimeTableCacheFilterPartObject($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 loadFromJSON($filter_json) {
		$this->reset();
		$filter_obj = json_decode($filter_json);

		foreach ($filter_obj as $filter_part)
		{
			$part = $this->createNewPart();
			$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) {
		$this_class = get_class();
		$filter = new $this_class($this->cacheObject);
		$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)
		{
			$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();
	}
}

 ?>