Condition.php 1.43 KB
Newer Older
doctrine's avatar
doctrine committed
1
<?php
2
Doctrine::autoload("Doctrine_Query_Part");
doctrine's avatar
doctrine committed
3 4 5 6 7 8 9 10 11 12 13 14 15

abstract class Doctrine_Query_Condition extends Doctrine_Query_Part {
    /**
     * DQL CONDITION PARSER
     * parses the where/having part of the query string
     *
     *
     * @param string $str
     * @return string
     */
    final public function parse($str) {
        $tmp = trim($str);

16
        $parts = Doctrine_Query::bracketExplode($str, array(' \&\& ', ' AND '), "(", ")");
doctrine's avatar
doctrine committed
17 18 19
        if(count($parts) > 1) {
            $ret = array();
            foreach($parts as $part) {
20 21
                $part = Doctrine_Query::bracketTrim($part, "(", ")");
                $ret[] = $this->parse($part);
doctrine's avatar
doctrine committed
22 23 24
            }
            $r = implode(" AND ",$ret);
        } else {
25

26
            $parts = Doctrine_Query::bracketExplode($str, array(' \|\| ', ' OR '), "(", ")");
doctrine's avatar
doctrine committed
27 28 29
            if(count($parts) > 1) {
                $ret = array();
                foreach($parts as $part) {
30
                    $part = Doctrine_Query::bracketTrim($part, "(", ")");
doctrine's avatar
doctrine committed
31 32 33 34
                    $ret[] = $this->parse($part);
                }
                $r = implode(" OR ",$ret);
            } else {
35 36 37 38
                if(substr($parts[0],0,1) == "(" && substr($parts[0],-1) == ")") 
                    return $this->parse(substr($parts[0],1,-1));
                else
                    return $this->load($parts[0]);
doctrine's avatar
doctrine committed
39 40
            }
        }
41 42

        return "(".$r.")";
doctrine's avatar
doctrine committed
43 44
    }
}
45