TimeTableCacheIntervalObject.php 3.23 KB
<?php
class TimeTableCacheIntervalObject
{
	protected $cacheObject = NULL;
	protected $id         = -1;
	protected $index      = -1;
	protected $start      = 0;
	protected $stop       = 0;
	protected $isNew      = false;
	protected $isModified = false;

	function __construct($cacheObject, $id, $index = -1) {
		$this->cacheObject = $cacheObject;
		$this->id = $id;
		$this->index = $index;
	}

	public function getId() {
		return $this->id;
	}

	public function getIndex() {
		return $this->index;
	}

	public function setIndex($index) {
		$this->index = $index;
	}

	public function getStartToStamp() {
		return $this->start;
	}

	public function getStartToISO() {
		return TimeUtils::stamp2iso($this->start);
	}

	public function setStartFromStamp($stamp) {
		$this->start = $stamp;
	}

	public function setStartFromISO($iso) {
		$this->start = TimeUtils::iso2stamp($iso);
	}

	public function getStopToStamp() {
		return $this->stop;
	}

	public function getStopToISO() {
		return TimeUtils::stamp2iso($this->stop);
	}

	public function setStopFromStamp($stamp) {
		$this->stop = $stamp;
	}

	public function setStopFromISO($iso) {
		$this->stop = TimeUtils::iso2stamp($iso);
	}

	public function getDuration() {
		return ($this->stop-$this->start);
	}

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

	public function setIsModified($isModified) {
		$this->isModified = $isModified;
	}

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

	public function setIsNew($isNew) {
		$this->isNew = $isNew;
	}

	public function toArray() {
		$result = array(
			"cacheId" => $this->id,
			"start"   => $this->getStartToISO(),
			"stop"    => $this->getStopToISO()
		);
		if ($this->isNew)
			$result["isNew"] = true;
		if ($this->isModified)
			$result["isModified"] = true;

		return $result;
	}

	public function modifyInterval($data) {
		if (array_key_exists('start', $data)) {
			$this->setStartFromISO($data->{'start'});
			$this->setIsModified(true);
		}
		if (array_key_exists('stop', $data)) {
			$this->setStopFromISO($data->{'stop'});
			$this->setIsModified(true);
		}

		return $this->isModified();
	}

	public function writeBin($handle) {

                                        fwrite($handle,pack('L2',$this->id,$this->index));
                                        fwrite($handle,pack('d2', $this->start,$this->stop));
                                        fwrite($handle, pack('L2', $this->isNew,$this->isModified));
	}

	public function loadBin($handle) {
            
                                        $res =  unpack('L2int',fread($handle,4*2));
		$this->id         = $res['int1'];
		$this->index   = $res['int2'];
                
                                        $res =  unpack('d2d',fread($handle,8*2));
		$this->start         = $res['d1'];
		$this->stop         = $res['d2'];

                
                                        $res =  unpack('L2int',fread($handle,4*2));
		$this->isNew         = $res['int1'];
		$this->isModified = $res['int2'];
                            
	}

	public function dump() {
		echo " => Interval : id = ".$this->id.", index = ".$this->index.", start = ".TimeUtils::stamp2iso($this->start).", stop = ".TimeUtils::stamp2iso($this->stop).", isNew = ".$this->isNew.", isModified = ".$this->isModified.PHP_EOL;
	}
}
?>