Location: PHPKode > scripts > mysql2object > mysql2object/column.class.php
<?
/**
 * Column Class - part of mysql2object set of classes
 *
 * @author Arkadiusz Mali&#324;ski <hide@address.com>
 * @package mysql2object
 */
class Column {
	
	/**
	 * Column name
	 *
	 * @var string
	 */
	private $_name;
	
	/**
	 * Column decription based on SQL's 'DESCRIBE tableName';
	 *
	 * @var unknown_type
	 */
	private $_description;
	
	/**
	 * Column's values
	 *
	 * @var unknown_type
	 */
	private $_values;
	
	/**
	 * Array of valid comparision operands
	 *
	 * @var array
	 */
	private $_validOperators = array('==', '>=', '<=', '<', '>', '<>', '!=');
	

	/**
	 * Constructor
	 *
	 * @param array $arrColumn
	 */
	public function __construct($arrColumn) {
		$this->_name = $arrColumn['name'];
		$this->_description = $arrColumn['description'];
		$this->_values = $arrColumn['values'];
	}


	/**
	 * Returns column's name
	 *
	 * @return string
	 */
	public function __toString() {
		return $this->_name;
	}
	

	/**
	 * Handling calls
	 *
	 * @param string $func
	 * @return mixed
	 */
	public function __get($func) {
		if(array_key_exists($func, $this->_description)) {
			return $this->_description[$func];
		} else switch (strtolower($func)) {
			case 'all': 
				return $this->_values; 
				break;
			case 'name': 
				return $this->_name;
				break;
			default: 
				return null;
				break;
		}
	}


	/**
	 * Handling functions calls
	 *
	 * @param string $func
	 * @param mixed $args
	 */
	public function __call($func, $args) {
		switch (strtolower($func)) {
			default:
				throw new ColumnException('Unknown call "'.$func.'"');
		}
	}


	/**
	 * Gets value on selected index
	 *
	 * @param integer $index
	 * @return mixed
	 */
	public function getValue($index) {
		if (array_key_exists($index, $this->_values))
			return $this->_values[$index];
		else 
			throw new TableException('Index out of bounds in "getValue('.$index.')"');
	}


	/**
	 * Select values on defined conditions.
	 * As a parameters uses array(operator, value_for_comparision) for each condition.
	 *
	 * @param array $condition1,...
	 * @return mixed
	 */
	public function select() {
		if(func_num_args()) {
			foreach (func_get_args() as $param) {
				if(!in_array($param[0], $this->_validOperators))
					throw new TableException('Invalid comparision operator "'.$param[0].'"');
			}
			$ret = $this->_values;
			foreach (func_get_args() as $param) {
				$ret = $this->_test($param, $ret);
			}
			return $ret;	
		} else {
			throw new ColumnException('No condition definied in "select()" function');
		}
	}


	/**
	 * Test selected condition and return results
	 *
	 * @param array $param
	 * @return mixed
	 */
	private function _test($param, $vals) {
		foreach ($vals as $index=>$value) {
			eval("\$returnedValue = ($value $param[0] $param[1]);");
			if($returnedValue) $indexOK[] = $index;
		}
		
		foreach ($vals as $k=>$v) {
			if(in_array($k, $indexOK))
				$return[] = $v;
		}
		return $return;
	}


	/**
	 * Returns number od values stored in column
	 *
	 * @return integer
	 */
	public function count() {
		return count($this->_values);
	}

}

class ColumnException extends Exception {}

?>
Return current item: mysql2object