| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Parses conditions for where and having clauses | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * PHP Version 5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @category  Core | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @package   SQL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @author    Hans-Joachim Piepereit <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @copyright 2013 cSphere Team | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @license   http://opensource.org/licenses/bsd-license Simplified BSD License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @link      http://www.csphere.eu | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | namespace csphere\core\sql; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Parses conditions for where and having clauses | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @category  Core | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @package   SQL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @author    Hans-Joachim Piepereit <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @copyright 2013 cSphere Team | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @license   http://opensource.org/licenses/bsd-license Simplified BSD License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @link      http://www.csphere.eu | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  **/ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | abstract class Conditions | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * Supported operators for conditions | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      **/ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     private static $_operators = ['=', '!=', '>', '<', '<>', '>=', '<=']; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * Generate conditional parts of queries | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * @param array   $conditions Conditions with column, operation and value | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * @param boolean $having     This defines if having or where parts are parsed | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      **/ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public static function parse(array $conditions, $having = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $assoc = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $short = ($having === true) ? 'hav' : 'con'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $query = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         // Check if array dimensions are fine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if (isset($conditions[0]) && !is_array($conditions[0])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $conditions = [$conditions]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         foreach ($conditions AS $num => $con) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             // Process condition parts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $sub = self::_parts($short, $num, (array)$con); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             // Append operator and add part to result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $query .= self::_append($sub['query'], $num, (array)$con); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $assoc = array_merge($assoc, $sub['assoc']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $result = ['query' => $query, 'assoc' => $assoc]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return $result; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      * Generate part of a single condition | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      * @param string  $short Shorthandle for assoc array keys, e.g. con | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * @param integer $num   Number of condition as an integer | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * @param array   $con   Condition details as an array | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      **/ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     private static function _parts($short, $num, array $con) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $key   = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $assoc = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $query = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         // Bind must be unique per query condition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $bind = $short . $num; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         // Append simple oerator checks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         if (in_array($con[1], self::$_operators)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $query .= $con[0] . ' ' . $con[1] . ' :' . $bind; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $assoc[$bind] = $con[2]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } elseif ($con[1] == 'like') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             // Append like comparisons | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $query .= $con[0] . ' LIKE :' . $bind; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $assoc[$bind] = $con[2]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } elseif ($con[1] == 'between') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             // Append between checks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $query .= $con[0] . ' BETWEEN :' . $bind | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                     . 'b1 AND :' . $bind . 'b2'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $assoc[$bind . 'b1'] = $con[2][0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             $assoc[$bind . 'b2'] = $con[2][1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } elseif ($con[1] == 'in') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             // Append in array checks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $query .= $con[0] . ' IN ('; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             foreach ($con[2] AS $val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 $query .= ':' . $bind . 'i' . $key . ', '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 $assoc[$bind . 'i' . $key] = $val; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 $key++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             $query = substr($query, 0, -2) . ')'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         // Create result array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $result = ['query' => $query, 'assoc' => $assoc]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         return $result; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |      * Append operators to conditions | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |      * @param string  $query Query string | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |      * @param integer $num   Number of condition as an integer | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |      * @param array   $con   Condition details as an array | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |      **/ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     private static function _append($query, $num, array $con) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         // Prepend with NOT if set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         if (isset($con[3]) && $con[3] == true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             $query = 'NOT ' . $query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         // Prepend with AND or OR if not first | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         if ($num != 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             $pre = ' AND '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             if (isset($con[4]) && $con[4] == true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                 $pre = ' OR '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             $query = $pre . $query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         return $query; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 170 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 171 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 172 |  |  |  |