FilterRes.php 1.67 KB
<?php
  abstract class FilterRes
  {
  	abstract function getCondResult($cond);
  	
  	abstract function getAll();
  	
  	function andOp($cond_res1,$cond_res2)
  	{
  		$result = array();
  		for ($i = 0; $i < count($cond_res1); $i++)
  		  for ($j = 0; $j < count($cond_res2); $j++)
  		    if ($cond_res1[$i] == $cond_res2[$j])
  		    {
  		    	array_push($result,$cond_res1[$i]);
  		    	break;
  		    }
  		return $result;
  	}
  	
  	function orOp($cond_res1,$cond_res2)
  	{
  		$result = $cond_res1;		
  		for ($i = 0; $i < count($cond_res2); $i++)
  		{
  		  $found = false;
  		  for ($j = 0; $j < count($result); $j++)
  		    if ($cond_res2[$i] == $result[$j])
  		    {
  		       	$found = true;
  		    	break;
  		    }
  		  if (!$found)
  		  	array_push($result,$cond_res2[$i]);
  		}
  		return $result;
  	}
  	
  	function getResult($filter,$isEmptyFilter)
  	{  		
  		if ($isEmptyFilter)
  			return $this->getAll();
  		
  		$ops = array();
		$cond_res = array();
  		for ($i = 0 ; $i < count($filter); $i++)
  		{
  			$cond = $filter[$i];
  			$cond_res[$i] = $this->getCondResult($cond);
  			$ops[$i] = $cond['logical'];
  		}
  		
  		for ($i = 1; $i < count($cond_res); $i++)
  		{
  			if ($ops[$i] != 'and')
  				continue;
  			$cond_res[$i-1] = $this->andOp($cond_res[$i-1],$cond_res[$i]);
  			array_splice($cond_res, $i, 1);
  			array_splice($ops, $i, 1);
			$i--;		
  		}
  		
  		for ($i = 1; $i < count($cond_res); $i++)
  		{
  			if ($ops[$i] != 'or')
  				continue;
  			$cond_res[$i-1] = $this->orOp($cond_res[$i-1],$cond_res[$i]);
  			array_splice($cond_res, $i, 1);
  			array_splice($ops, $i, 1);
			$i--;			
  		}
  		
  		return $cond_res[0];
  	}
  }
?>