<?php

abstract class DataSource implements IDataSource, IteratorAggregate {



	protected $id;

	public $defaultOrder;
	public $defaultDescending = FALSE;
	public $defaultOffset = 0;
	public $defaultLimit = NULL;



	public function __construct($id) {
		$this->id = $id;
		if (!array_key_exists($this->id, $_SESSION)) {
			$_SESSION[$this->id] = array();
		}
	}



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



	public function fetch() {
		throw new Exception('overwrite this method');
	}



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



	public function setOrder($column, $descending = false) {
		$_SESSION[$this->id]['order'] = $column;
		$_SESSION[$this->id]['descending'] = $descending;
	}



	public function getOrder() {
		return array_key_exists('order', $_SESSION[$this->id]) ? $_SESSION[$this->id]['order'] : $this->defaultOrder;
	}



	public function isDescending() {
		return array_key_exists('descending', $_SESSION[$this->id]) ? $_SESSION[$this->id]['descending'] : $this->defaultDescending;
	}



	public function setOffset($offset) {
		$_SESSION[$this->id]['offset'] = $offset;
	}



	public function getOffset() {
		return array_key_exists('offset', $_SESSION[$this->id]) ? $_SESSION[$this->id]['offset'] : $this->defaultOffset;
	}



	public function setLimit($limit) {
		$_SESSION[$this->id]['limit'] = $limit;
	}



	public function getLimit() {
		return array_key_exists('limit', $_SESSION[$this->id]) ? $_SESSION[$this->id]['limit'] : $this->defaultLimit;
	}



} // class DataSource

?>