Filter式の作成(LDAP検索式)

class Composer2{
	var $text;
	var $values;
	
	public function __construct(){
		$this->text = "";
		$this->values = array();
	}
	public function composeCompare($compare){
		$this->text = "(" . $compare->field . "=" . $compare->value . ")";
		
	}
	public function composeRange($range){
		$this->text = "(" . $compare->field . ">=" . $compare->valueFrom . " and " .$compare->field . " <= " . $compare->valueTo . ")";
		
	}
	public function composeOperator($op){
		$this->text = ""; 
	}
	public function composeComplex($complex){
		$it = new ArrayIterator($complex->expressions);
	    $text = $this->term($it); 
		
		$this->text = $text; 
	}
	private function term(ArrayIterator $it){
	    $count = 1;
	    $text = $this->fact($it);
	    
	    while($it->valid()){
	        $text .= $this->fact($it);
	        $count++;
	    }
	    if($count>1){
	        $text = "(|" . $text .")";
	    }
	    
	    return $text;
	    
	}
	private function fact(ArrayIterator $it){
	    $count = 0;
	    $text = "";

	    while($it->valid()){
	        $expr = $it->current();
	        if($expr instanceof Operator){
	            if($this->isOr($expr)){
    	            $it->next();
    	            break;
    	        }
    	        $it->next();
	        }
	        if(!$it->valid()){
	            break;
	        }
	        $expr = $it->current();
	        $expr->compose($this);
	        $text .= $this->text;
	        $it->next(); 
	        $count++;
	    }
	    if($count>1){
	        $text = "(&" . $text .")";
	    }
	    return $text;
	}
	private function isOr($expr){
	    if($expr instanceof Operator){
	        if($expr->op == "or") return true;
	    }
	    return false;
	}
	private function isAnd($expr){
	    if($expr instanceof Operator){
	        if($expr->op == "and") return true;
	    }
	    return false;
	}
		
	
}