| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Database; |
| 4 |
|
| 5 |
final class Helper |
| 6 |
{ |
| 7 |
// public static function getTableName($className) |
| 8 |
// { |
| 9 |
// $className = str_replace('\\', '', $className); |
| 10 |
// $className = str_replace('Model', '', $className); |
| 11 |
// return strtolower($className); |
| 12 |
// } |
| 13 |
public function arrValueModifyByLogic($operator, $value) |
| 14 |
{ |
| 15 |
$oprators = [ |
| 16 |
'equal' => '["' . implode('","', $value) . '"]', |
| 17 |
'not_equal' => '["' . implode('","', $value) . '"]', |
| 18 |
'start_with' => '["' . implode('","', $value) . '"%', |
| 19 |
'not_start_with' => '["' . implode('","', $value) . '"%', |
| 20 |
'end_with' => '%"' . implode('","', $value) . '"]', |
| 21 |
'not_end_with' => '%"' . implode('","', $value) . '"]', |
| 22 |
'contain' => '%"' . implode('","', $value) . '"%', |
| 23 |
'not_contain' => '%"' . implode('","', $value) . '"%', |
| 24 |
'empty' => '', |
| 25 |
'not_empty' => '', |
| 26 |
]; |
| 27 |
|
| 28 |
if (isset($oprators[$operator])) { |
| 29 |
return $oprators[$operator]; |
| 30 |
} |
| 31 |
|
| 32 |
return $value; |
| 33 |
} |
| 34 |
|
| 35 |
public function dateQueryList() |
| 36 |
{ |
| 37 |
return [ |
| 38 |
'today' => 'Y-m-d_+0 days_=', |
| 39 |
'till_today' => 'Y-m-d_+0 days_<=', |
| 40 |
'tomorrow' => 'Y-m-d_+1 days_=', |
| 41 |
'till_tomorrow' => 'Y-m-d_+1 days_<=', |
| 42 |
'yesterday' => 'Y-m-d_-1 days_=', |
| 43 |
'till_yesterday' => 'Y-m-d_-1 days_<=', |
| 44 |
'last_7_days' => 'Y-m-d_-7 days_>=', |
| 45 |
'last_30_days' => 'Y-m-d_-30 days_>=', |
| 46 |
'last_60_days' => 'Y-m-d_-60 days_>=', |
| 47 |
'last_90_days' => 'Y-m-d_-90 days_>=', |
| 48 |
'last_120_days' => 'Y-m-d_-120 days_>=', |
| 49 |
'next_7_days' => 'Y-m-d_+7 days_>=', |
| 50 |
'next_30_days' => 'Y-m-d_+30 days_>=', |
| 51 |
'next_60_days' => 'Y-m-d_+60 days_>=', |
| 52 |
'next_90_days' => 'Y-m-d_+90 days_>=', |
| 53 |
'next_120_days' => 'Y-m-d_+120 days_>=', |
| 54 |
'current_month' => 'm_+0 month_=', |
| 55 |
'next_month' => 'm_first day of next month_=', |
| 56 |
'last_month' => 'm_first day of last month_=', |
| 57 |
'current_week' => 'W_0 week_=', |
| 58 |
'next_week' => 'W_+1 week_=', |
| 59 |
'last_week' => 'W_-1 week_=', |
| 60 |
'current_year' => 'Y_+0 year_=', |
| 61 |
'next_year' => 'Y_+1 year_=', |
| 62 |
'last_year' => 'Y_-1 year_=', |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
public function strValueModifyByLogic($operator, $value) |
| 67 |
{ |
| 68 |
$timeInterval = $this->dateQueryList(); |
| 69 |
|
| 70 |
if (isset($timeInterval[$operator])) { |
| 71 |
$data = explode('_', $timeInterval[$operator]); |
| 72 |
if (is_array($data) && count($data) > 0) { |
| 73 |
$value = date($data[0], strtotime($data[1])); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
$oprators = [ |
| 78 |
'greater_than' => (int) $value, |
| 79 |
'less_than' => (int) $value, |
| 80 |
'greater_than_equal' => (int) $value, |
| 81 |
'less_than_equal' => (int) $value, |
| 82 |
'start_with' => $value . '%', |
| 83 |
'end_with' => '%' . $value, |
| 84 |
'contain' => '%' . $value . '%', |
| 85 |
'empty' => '', |
| 86 |
'not_empty' => '', |
| 87 |
'not_start_with' => $value . '%', |
| 88 |
'not_end_with' => '%' . $value, |
| 89 |
'not_contain' => '%' . $value . '%', |
| 90 |
]; |
| 91 |
|
| 92 |
if (isset($oprators[$operator])) { |
| 93 |
$value = $oprators[$operator]; |
| 94 |
} |
| 95 |
|
| 96 |
if (is_array($value)) { |
| 97 |
$value = implode(',', $value); |
| 98 |
} |
| 99 |
|
| 100 |
return $value; |
| 101 |
} |
| 102 |
|
| 103 |
public function convertToSqlOperator($operator) |
| 104 |
{ |
| 105 |
$timeInterval = $this->dateQueryList(); |
| 106 |
|
| 107 |
if (isset($timeInterval[$operator])) { |
| 108 |
$data = explode('_', $timeInterval[$operator]); |
| 109 |
return $data[2]; |
| 110 |
} |
| 111 |
|
| 112 |
$oprators = [ |
| 113 |
'greater_than' => '>', |
| 114 |
'less_than' => '<', |
| 115 |
'greater_than_equal' => '>=', |
| 116 |
'less_than_equal' => '<=', |
| 117 |
'equal' => '=', |
| 118 |
'not_equal' => '!=', |
| 119 |
'empty' => '=', |
| 120 |
'not_empty' => '!=', |
| 121 |
'start_with' => 'LIKE', |
| 122 |
'not_start_with' => 'NOT LIKE', |
| 123 |
'end_with' => 'LIKE', |
| 124 |
'not_end_with' => 'NOT LIKE', |
| 125 |
'contain' => 'LIKE', |
| 126 |
'not_contain' => 'NOT LIKE', |
| 127 |
]; |
| 128 |
|
| 129 |
if (isset($oprators[$operator])) { |
| 130 |
return $oprators[$operator]; |
| 131 |
} |
| 132 |
|
| 133 |
return $operator; |
| 134 |
} |
| 135 |
|
| 136 |
public function fieldQueryByDate($fld, $operator, $value, $logic) |
| 137 |
{ |
| 138 |
$currentYear = date('Y'); |
| 139 |
|
| 140 |
$sql = ''; |
| 141 |
|
| 142 |
$daysInterval = [ |
| 143 |
'today', |
| 144 |
'till_today', |
| 145 |
'tomorrow', |
| 146 |
'till_tomorrow', |
| 147 |
'yesterday', |
| 148 |
'till_yesterday', |
| 149 |
'last_7_days', |
| 150 |
'last_30_days', |
| 151 |
'last_60_days', |
| 152 |
'last_90_days', |
| 153 |
'last_120_days', |
| 154 |
'next_7_days', |
| 155 |
'next_30_days', |
| 156 |
'next_60_days', |
| 157 |
'next_90_days', |
| 158 |
'next_120_days', |
| 159 |
]; |
| 160 |
if (in_array($logic, $daysInterval)) { |
| 161 |
$sql = "`$fld` $operator '$value'"; |
| 162 |
} |
| 163 |
|
| 164 |
if (in_array($logic, ['current_month', 'next_month', 'last_month'])) { |
| 165 |
$sql = "MONTH(`$fld`) $operator '$value' AND YEAR(`$fld`) = '$currentYear'"; |
| 166 |
} |
| 167 |
|
| 168 |
if (in_array($logic, ['current_week', 'next_week', 'last_week'])) { |
| 169 |
$sql = "WEEK(`$fld`) $operator '$value' AND YEAR(`$fld`) = $currentYear"; |
| 170 |
} |
| 171 |
return $sql; |
| 172 |
} |
| 173 |
} |
| 174 |
|