| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace WebComplete\core\condition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | class ConditionMicroDbParser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |      * @param Condition|null $condition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @param int|null $limit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @param int|null $offset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @return \Closure|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public function filter(Condition $condition = null, int &$limit = null, int &$offset = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         $offset = (int)$offset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         if ($condition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             $limit = $condition->getLimit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             $offset = (int)$condition->getOffset(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             $conditions = $condition->getConditions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             return function (array $item) use ($conditions) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |                 foreach ($conditions as $cond) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |                     if (!\is_array($cond)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                         continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                     $cond = \array_values($cond); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                     $type = \array_shift($cond); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                     $value = $item[$cond[0]] ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                     switch ($type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                         case Condition::EQUALS: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                             if (\is_array($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                                 if (!\in_array($cond[1], $value, false)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                                     return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                                 /** @noinspection TypeUnsafeComparisonInspection */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                                 if ($value != $cond[1]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                                     return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                         case Condition::NOT_EQUALS: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                             /** @noinspection TypeUnsafeComparisonInspection */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                             if ($value == $cond[1]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                         case Condition::LESS_THAN: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                             if ($value >= $cond[1]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                         case Condition::GREATER_THAN: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                             if ($value <= $cond[1]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                         case Condition::LESS_OR_EQUALS: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                             if ($value > $cond[1]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                         case Condition::GREATER_OR_EQUALS: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                             if ($value < $cond[1]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                         case Condition::BETWEEN: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                             if ($value < $cond[1] || $value > $cond[2]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                         case Condition::LIKE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                             if (!\mb_strstr($value, $cond[1])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                         case Condition::IN: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                             if (!\in_array($value, $cond[1], false)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param Condition|null $condition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @return \Closure|null | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     public function sort(Condition $condition = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         if ($condition) { | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |             $sort = $condition->getSort(); | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             return function (array $item1, array $item2) use ($sort) { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |                 foreach ($sort as $field => $dir) { | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |                     $value1 = $item1[$field] ?? null; | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |                     $value2 = $item2[$field] ?? null; | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |                     if ($value1 === $value2) { | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |                         continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |                     return ($value1 <=> $value2) * ($dir === \SORT_ASC ? 1 : -1); | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |                 return 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |             }; | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  |