fluent-community
/
vendor
/
wpfluent
/
framework
/
src
/
WPFluent
/
Database
/
Concerns
/
BuildsWhereDateClauses.php
BuildsWhereDateClauses.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.6.0, at vendor/wpfluent/framework/src/WPFluent/Database/Concerns/BuildsWhereDateClauses.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentCommunity\Framework\Database\Concerns; |
| 4 | |
| 5 | use FluentCommunity\Framework\Support\Arr; |
| 6 | use FluentCommunity\Framework\Support\DateTime; |
| 7 | |
| 8 | trait BuildsWhereDateClauses |
| 9 | { |
| 10 | /** |
| 11 | * Add a where clause to determine if a "date" column is in the past to the query. |
| 12 | * |
| 13 | * @param array|string $columns |
| 14 | * @return $this |
| 15 | */ |
| 16 | public function wherePast($columns) |
| 17 | { |
| 18 | return $this->wherePastOrFuture($columns, '<', 'and'); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Add a where clause to determine if a "date" column is in the past or now to the query. |
| 23 | * |
| 24 | * @param array|string $columns |
| 25 | * @return $this |
| 26 | */ |
| 27 | public function whereNowOrPast($columns) |
| 28 | { |
| 29 | return $this->wherePastOrFuture($columns, '<=', 'and'); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Add an "or where" clause to determine if a "date" column is in the past to the query. |
| 34 | * |
| 35 | * @param array|string $columns |
| 36 | * @return $this |
| 37 | */ |
| 38 | public function orWherePast($columns) |
| 39 | { |
| 40 | return $this->wherePastOrFuture($columns, '<', 'or'); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Add a where clause to determine if a "date" column is in the past or now to the query. |
| 45 | * |
| 46 | * @param array|string $columns |
| 47 | * @return $this |
| 48 | */ |
| 49 | public function orWhereNowOrPast($columns) |
| 50 | { |
| 51 | return $this->wherePastOrFuture($columns, '<=', 'or'); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Add a where clause to determine if a "date" column is in the future to the query. |
| 56 | * |
| 57 | * @param array|string $columns |
| 58 | * @return $this |
| 59 | */ |
| 60 | public function whereFuture($columns) |
| 61 | { |
| 62 | return $this->wherePastOrFuture($columns, '>', 'and'); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Add a where clause to determine if a "date" column is in the future or now to the query. |
| 67 | * |
| 68 | * @param array|string $columns |
| 69 | * @return $this |
| 70 | */ |
| 71 | public function whereNowOrFuture($columns) |
| 72 | { |
| 73 | return $this->wherePastOrFuture($columns, '>=', 'and'); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Add an "or where" clause to determine if a "date" column is in the future to the query. |
| 78 | * |
| 79 | * @param array|string $columns |
| 80 | * @return $this |
| 81 | */ |
| 82 | public function orWhereFuture($columns) |
| 83 | { |
| 84 | return $this->wherePastOrFuture($columns, '>', 'or'); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Add an "or where" clause to determine if a "date" column is in the future or now to the query. |
| 89 | * |
| 90 | * @param array|string $columns |
| 91 | * @return $this |
| 92 | */ |
| 93 | public function orWhereNowOrFuture($columns) |
| 94 | { |
| 95 | return $this->wherePastOrFuture($columns, '>=', 'or'); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Add an "where" clause to determine if a "date" column is in the past or future. |
| 100 | * |
| 101 | * @param array|string $columns |
| 102 | * @return $this |
| 103 | */ |
| 104 | protected function wherePastOrFuture($columns, $operator, $boolean) |
| 105 | { |
| 106 | $type = 'Basic'; |
| 107 | $value = DateTime::now(); |
| 108 | |
| 109 | foreach (Arr::wrap($columns) as $column) { |
| 110 | $this->wheres[] = compact('type', 'column', 'boolean', 'operator', 'value'); |
| 111 | |
| 112 | $this->addBinding($value); |
| 113 | } |
| 114 | |
| 115 | return $this; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Add a "where date" clause to determine if a "date" column is today to the query. |
| 120 | * |
| 121 | * @param array|string $columns |
| 122 | * @param string $boolean |
| 123 | * @return $this |
| 124 | */ |
| 125 | public function whereToday($columns, $boolean = 'and') |
| 126 | { |
| 127 | return $this->whereTodayBeforeOrAfter($columns, '=', $boolean); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Add a "where date" clause to determine if a "date" column is before today. |
| 132 | * |
| 133 | * @param array|string $columns |
| 134 | * @return $this |
| 135 | */ |
| 136 | public function whereBeforeToday($columns) |
| 137 | { |
| 138 | return $this->whereTodayBeforeOrAfter($columns, '<', 'and'); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Add a "where date" clause to determine if a "date" column is today or before to the query. |
| 143 | * |
| 144 | * @param array|string $columns |
| 145 | * @return $this |
| 146 | */ |
| 147 | public function whereTodayOrBefore($columns) |
| 148 | { |
| 149 | return $this->whereTodayBeforeOrAfter($columns, '<=', 'and'); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Add a "where date" clause to determine if a "date" column is after today. |
| 154 | * |
| 155 | * @param array|string $columns |
| 156 | * @return $this |
| 157 | */ |
| 158 | public function whereAfterToday($columns) |
| 159 | { |
| 160 | return $this->whereTodayBeforeOrAfter($columns, '>', 'and'); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Add a "where date" clause to determine if a "date" column is today or after to the query. |
| 165 | * |
| 166 | * @param array|string $columns |
| 167 | * @return $this |
| 168 | */ |
| 169 | public function whereTodayOrAfter($columns) |
| 170 | { |
| 171 | return $this->whereTodayBeforeOrAfter($columns, '>=', 'and'); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Add an "or where date" clause to determine if a "date" column is today to the query. |
| 176 | * |
| 177 | * @param array|string $columns |
| 178 | * @return $this |
| 179 | */ |
| 180 | public function orWhereToday($columns) |
| 181 | { |
| 182 | return $this->whereToday($columns, 'or'); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Add an "or where date" clause to determine if a "date" column is before today. |
| 187 | * |
| 188 | * @param array|string $columns |
| 189 | * @return $this |
| 190 | */ |
| 191 | public function orWhereBeforeToday($columns) |
| 192 | { |
| 193 | return $this->whereTodayBeforeOrAfter($columns, '<', 'or'); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Add an "or where date" clause to determine if a "date" column is today or before to the query. |
| 198 | * |
| 199 | * @param array|string $columns |
| 200 | * @return $this |
| 201 | */ |
| 202 | public function orWhereTodayOrBefore($columns) |
| 203 | { |
| 204 | return $this->whereTodayBeforeOrAfter($columns, '<=', 'or'); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Add an "or where date" clause to determine if a "date" column is after today. |
| 209 | * |
| 210 | * @param array|string $columns |
| 211 | * @return $this |
| 212 | */ |
| 213 | public function orWhereAfterToday($columns) |
| 214 | { |
| 215 | return $this->whereTodayBeforeOrAfter($columns, '>', 'or'); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Add an "or where date" clause to determine if a "date" column is today or after to the query. |
| 220 | * |
| 221 | * @param array|string $columns |
| 222 | * @return $this |
| 223 | */ |
| 224 | public function orWhereTodayOrAfter($columns) |
| 225 | { |
| 226 | return $this->whereTodayBeforeOrAfter($columns, '>=', 'or'); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Add a "where date" clause to determine if a "date" column is today or after to the query. |
| 231 | * |
| 232 | * @param array|string $columns |
| 233 | * @param string $operator |
| 234 | * @param string $boolean |
| 235 | * @return $this |
| 236 | */ |
| 237 | protected function whereTodayBeforeOrAfter($columns, $operator, $boolean) |
| 238 | { |
| 239 | $value = DateTime::today()->format('Y-m-d'); |
| 240 | |
| 241 | foreach (Arr::wrap($columns) as $column) { |
| 242 | $this->addDateBasedWhere( |
| 243 | 'Date', $column, $operator, $value, $boolean |
| 244 | ); |
| 245 | } |
| 246 | |
| 247 | return $this; |
| 248 | } |
| 249 | } |
| 250 |