<?php
/*
 * Copyright (c) 2013, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

abstract class _WV_Filter_BaseColumn implements WV_Filter_Interface_ChangeTable {
	protected $column;
	protected $value;
	protected $operation;

	public function __construct($column, $value, $operation = '=') {
		if (!in_array($operation, array('=', '<>', '!=', '<=', '>=', '<', '>', 'LIKE', 'NOT LIKE', 'REGEXP', 'IN'))) {
			throw new WV_Exception_Filter(t('devutils_filters_base_unknown', $operation));
		}

		if (is_array($value)) {
			$operation = 'IN';
		}

		$this->column    = $column;
		$this->value     = $value;
		$this->operation = $operation;
	}

	public function isValid() {
		return true;
	}

	protected function prefix() {
		return sly_Core::getTablePrefix();
	}

	public function changeTable($oldTable, $newTable) {
		$parts = explode('.', $this->column);
		if (count($parts) === 2 && $parts[0] == (sly_Core::getTablePrefix() . $oldTable)) {
			$this->column = $newTable . '.' . $parts[1];
		}
	}
}
