| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | trait TraitQuery | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     /** Adds conditions, orderBy and Limits to query. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |      * @todo Separate query in dedicated class or trait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |      * @param string    $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |      * @param mixed     $cond | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @param array     $order | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @param mixed     $limit -> 5 or [$start, $count] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @param bool      $useHaving sflag - to use HAVING instead of WHERE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @return string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected static function query( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         $query, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $cond = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         array $order = [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $limit = 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $useHaving = false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         //Condition: '' | ' (HAVING|WHERE|AND) ' . $cond | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         self::queryConditions($query, $cond, $useHaving); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         //Order: '' | 'ORDER BY ' . array_keys($order)[0] . ' ' . $order[0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         self::queryOrder($query, $order); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         //Limit: '' | ' LIMIT ' . '(20, 40|20)' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         self::queryLimit($query, $limit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         return $query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private static function queryConditions(&$query, $cond, $useHaving) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         if (!empty($cond)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             $clause = !$useHaving ? 'WHERE' : 'HAVING'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $clue = !strpos($query, $clause) ? $clause : 'AND'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $query .= (' ' . $clue . ' ' . $cond); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     private static function queryOrder(&$query, $order) | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         if (!empty($order)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             $arr = array_map(function (&$value, $key) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |                 return $key . ' ' . strtoupper($value); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             }, $order, array_keys($order)); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             $query .= (' ORDER BY ' . implode(', ', $arr)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     private static function queryLimit(&$query, $limit) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if (!empty($limit)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $value = (is_array($limit) ? implode(', ', $limit) : $limit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $query .= (' LIMIT ' . $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |  |