CatalogCacheIntervalObject.php 3.72 KB
<?php

class CatalogCacheIntervalObject extends TimeTableCacheIntervalObject
{
	// for catalog
	private $params      = array();

	public function toArray()
	{
		$result = parent::toArray();

		foreach ($this->cacheObject->getParametersInfo() as $parameter)
		{
			$result[$parameter['id']] = $this->params[$parameter['id']];
		}
		return $result;
	}

	// for catalog
	public function setParamValue($param_id, $param_value)
	{
		$this->params[$param_id] = $param_value;
	}

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

	public function modifyInterval($data) {
		parent::modifyInterval($data);
		foreach ($this->cacheObject->getParametersInfo() as $parameter) {
			if (array_key_exists($parameter['id'], $data)) {
				$this->setParamValue($parameter['id'], $data->{$parameter['id']});
				$this->setIsModified(true);
			}
		}
		return $this->isModified();
	}

	public function writeBin($handle)
	{
		parent::writeBin($handle);

		foreach ($this->cacheObject->getParametersInfo() as $parameter)
		{
			$size = $parameter['size'];
			$values = $this->params[$parameter['id']];
			if ($size > 1) {
				$values_array = explode(',',$values);
			}
			else {
				$values_array = array($values);
			}
			for ($i = 0; $i < $size; ++$i) {
				$value = ($i >= count($values_array)) ? "" : $values_array[$i];
				switch ($parameter['type']) {
					case 0: //double
						if($value == "nan")
							$floatValue  = NAN;
						else
							$floatValue = floatval($value);
						fwrite($handle,pack('d', $floatValue));
					  break;
					case 1: //date (timestamp)
						fwrite($handle,pack('d', TimeUtils::iso2stamp($value)));
						break;
					case 2: //string
						fwrite($handle,pack('L', strlen($value)));
						for ($j = 0; $j < strlen($value); ++$j) {
							fwrite($handle,pack('C', ord($value[$j])));
						}
						break;
					case 3: //int
						fwrite($handle,pack('l', intval($value)));
					  break;
					default: // not defined => string
					fwrite($handle,pack('L', strlen($value)));
					for ($j = 0; $j < strlen($value); ++$j) {
						fwrite($handle,pack('C', ord($value[$j])));
					}
				}
			}
		}
	}

	public function loadBin($handle)
	{
		parent::loadBin($handle);

		foreach ($this->cacheObject->getParametersInfo() as $parameter) {
		  $size = $parameter['size'];
			$id = $parameter['id'];


			$values = array();
		  for ($i = 0; $i < $size; $i++)
		  {
				$val = "";
				switch ($parameter['type']) {
					case 0: //double
						if (!$res = unpack('dval',fread($handle,8))) {
							break;
						}
						$val = is_nan($res['val']) ? "nan" : $res['val'];
					  break;
					case 1: //date (timestamp)
						if (!$res = unpack('dval',fread($handle,8))) {
							break;
						}
						$val = TimeUtils::stamp2iso($res['val']);
						break;
					case 2: //string
						if (!$res = unpack('Llength',fread($handle,4))) {
							break;
						}
						$length = $res['length'];
						$val = "";
						for ($j = 0; $j < $length; ++$j) {
							if (!$res = unpack('Cval',fread($handle,1))) {
								break;
							}
							$val .= chr($res['val']);
						}

						break;
					case 3: //int
						if (!$res = unpack('lval',fread($handle,4))) {
							break;
						}
						$val = $res['val'];
					  break;
					default: // not defined => string
						if (!$res = unpack('Llength',fread($handle,4))) {
							break;
						}
						$length = $res['length'];
						$val = "";
						for ($j = 0; $j < $length; ++$j) {
							if (!$res = unpack('Cval',fread($handle,1))) {
								break;
							}
							$val .= chr($res['val']);
						}
				}

				$values[] = $val;
			}

			$this->params[$id] = implode(',',$values);
		}
	}

	public function dump()
	{
		parent::dump();
		echo "               parameters = ";
		foreach ($this->params as $param) {
			echo $param." - ";
		}
		echo PHP_EOL;
	}
}

 ?>