Coverage for Doctrine_Query_Condition

Back to coverage report

1 <?php
2 /*
3  *  $Id: Condition.php 3884 2008-02-22 18:26:35Z jwage $
4  *
5  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16  *
17  * This software consists of voluntary contributions made by many individuals
18  * and is licensed under the LGPL. For more information, see
19  * <http://www.phpdoctrine.org>.
20  */
21 Doctrine::autoload('Doctrine_Query_Part');
22 /**
23  * Doctrine_Query_Condition
24  *
25  * @package     Doctrine
26  * @subpackage  Query
27  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28  * @link        www.phpdoctrine.org
29  * @since       1.0
30  * @version     $Revision: 3884 $
31  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
32  */
33 abstract class Doctrine_Query_Condition extends Doctrine_Query_Part
34 {
35     /**
36      * DQL CONDITION PARSER
37      * parses the join condition/where/having part of the query string
38      *
39      * @param string $str
40      * @return string
41      */
42     public function parse($str)
43     {
44         $tmp = trim($str);
45
46         $parts = $this->_tokenizer->bracketExplode($str, array(' \&\& ', ' AND '), '(', ')');
47
48         if (count($parts) > 1) {
49             $ret = array();
50             foreach ($parts as $part) {
51                 $part = $this->_tokenizer->bracketTrim($part, '(', ')');
52                 $ret[] = $this->parse($part);
53             }
54             $r = implode(' AND ', $ret);
55         } else {
56
57             $parts = $this->_tokenizer->bracketExplode($str, array(' \|\| ', ' OR '), '(', ')');
58             if (count($parts) > 1) {
59                 $ret = array();
60                 foreach ($parts as $part) {
61                     $part = $this->_tokenizer->bracketTrim($part, '(', ')');
62                     $ret[] = $this->parse($part);
63                 }
64                 $r = implode(' OR ', $ret);
65             } else {
66                 // Fix for #710
67                 if (substr($parts[0],0,1) == '(' && substr($parts[0], -1) == ')') {
68                     return $this->parse(substr($parts[0], 1, -1));
69                 } else {
70                     // Processing NOT here
71                     if (strtoupper(substr($parts[0], 0, 4)) === 'NOT ') {
72                         $r = 'NOT ('.$this->parse(substr($parts[0], 4)).')';
73                     } else {
74                         return $this->load($parts[0]);
75                     }
76                 }
77             }
78         }
79
80         return '(' . $r . ')';
81     }
82
83     /**
84      * parses a literal value and returns the parsed value
85      *
86      * boolean literals are parsed to integers
87      * components are parsed to associated table aliases
88      *
89      * @param string $value         literal value to be parsed
90      * @return string
91      */
92     public function parseLiteralValue($value)
93     {
94         // check that value isn't a string
95         if (strpos($value, '\'') === false) {
96             // parse booleans
97             $value = $this->query->getConnection()
98                      ->dataDict->parseBoolean($value);
99
100             $a = explode('.', $value);
101
102             if (count($a) > 1) {
103             // either a float or a component..
104
105                 if ( ! is_numeric($a[0])) {
106                     // a component found
107                     $field     = array_pop($a);
108                    $reference = implode('.', $a);
109                     $value = $this->query->getTableAlias($reference). '.' . $field;
110                 }
111             }
112         } else {
113             // string literal found
114         }
115
116         return $value;
117     }
118 }