| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
namespace Webklex\PHPIMAP\Query; |
| 14 |
|
| 15 |
use Closure; |
| 16 |
use Illuminate\Support\Str; |
| 17 |
use Webklex\PHPIMAP\Exceptions\InvalidWhereQueryCriteriaException; |
| 18 |
use Webklex\PHPIMAP\Exceptions\MethodNotFoundException; |
| 19 |
use Webklex\PHPIMAP\Exceptions\MessageSearchValidationException; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
class WhereQuery extends Query { |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
protected array $available_criteria = [ |
| 62 |
'OR', 'AND', |
| 63 |
'ALL', 'ANSWERED', 'BCC', 'BEFORE', 'BODY', 'CC', 'DELETED', 'FLAGGED', 'FROM', 'KEYWORD', |
| 64 |
'NEW', 'NOT', 'OLD', 'ON', 'RECENT', 'SEEN', 'SINCE', 'SUBJECT', 'TEXT', 'TO', |
| 65 |
'UNANSWERED', 'UNDELETED', 'UNFLAGGED', 'UNKEYWORD', 'UNSEEN', 'UID' |
| 66 |
]; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function __call(string $name, ?array $arguments) { |
| 78 |
$that = $this; |
| 79 |
|
| 80 |
$name = Str::camel($name); |
| 81 |
|
| 82 |
if (strtolower(substr($name, 0, 3)) === 'not') { |
| 83 |
$that = $that->whereNot(); |
| 84 |
$name = substr($name, 3); |
| 85 |
} |
| 86 |
|
| 87 |
if (!str_contains(strtolower($name), "where")) { |
| 88 |
$method = 'where' . ucfirst($name); |
| 89 |
} else { |
| 90 |
$method = lcfirst($name); |
| 91 |
} |
| 92 |
|
| 93 |
if (method_exists($this, $method) === true) { |
| 94 |
return call_user_func_array([$that, $method], $arguments); |
| 95 |
} |
| 96 |
|
| 97 |
throw new MethodNotFoundException("Method " . self::class . '::' . $method . '() is not supported'); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
protected function validate_criteria($criteria): string { |
| 108 |
$command = strtoupper($criteria); |
| 109 |
if (str_starts_with($command, "CUSTOM ")) { |
| 110 |
return substr($criteria, 7); |
| 111 |
} |
| 112 |
if (in_array($command, $this->available_criteria) === false) { |
| 113 |
throw new InvalidWhereQueryCriteriaException("Invalid imap search criteria: $command"); |
| 114 |
} |
| 115 |
|
| 116 |
return $criteria; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public function where(mixed $criteria, mixed $value = null): static { |
| 136 |
if (is_array($criteria)) { |
| 137 |
foreach ($criteria as $key => $value) { |
| 138 |
if (is_numeric($key)) { |
| 139 |
$this->where($value); |
| 140 |
}else{ |
| 141 |
$this->where($key, $value); |
| 142 |
} |
| 143 |
} |
| 144 |
} else { |
| 145 |
$this->push_search_criteria($criteria, $value); |
| 146 |
} |
| 147 |
|
| 148 |
return $this; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
protected function push_search_criteria(string $criteria, mixed $value): void { |
| 159 |
$criteria = $this->validate_criteria($criteria); |
| 160 |
$value = $this->parse_value($value); |
| 161 |
|
| 162 |
if ($value === '') { |
| 163 |
$this->query->push([$criteria]); |
| 164 |
} else { |
| 165 |
$this->query->push([$criteria, $value]); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public function orWhere(?Closure $closure = null): static { |
| 175 |
$this->query->push(['OR']); |
| 176 |
if ($closure !== null) $closure($this); |
| 177 |
|
| 178 |
return $this; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public function andWhere(?Closure $closure = null): static { |
| 187 |
$this->query->push(['AND']); |
| 188 |
if ($closure !== null) $closure($this); |
| 189 |
|
| 190 |
return $this; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public function whereAll(): static { |
| 198 |
return $this->where('ALL'); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public function whereAnswered(): static { |
| 206 |
return $this->where('ANSWERED'); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public function whereBcc(string $value): static { |
| 216 |
return $this->where('BCC', $value); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public function whereBefore(mixed $value): static { |
| 226 |
$date = $this->parse_date($value); |
| 227 |
return $this->where('BEFORE', $date); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public function whereBody(string $value): static { |
| 237 |
return $this->where('BODY', $value); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public function whereCc(string $value): static { |
| 247 |
return $this->where('CC', $value); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function whereDeleted(): static { |
| 255 |
return $this->where('DELETED'); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public function whereFlagged(string $value): static { |
| 265 |
return $this->where('FLAGGED', $value); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public function whereFrom(string $value): static { |
| 275 |
return $this->where('FROM', $value); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public function whereKeyword(string $value): static { |
| 285 |
return $this->where('KEYWORD', $value); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
public function whereNew(): static { |
| 293 |
return $this->where('NEW'); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public function whereNot(): static { |
| 301 |
return $this->where('NOT'); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
public function whereOld(): static { |
| 309 |
return $this->where('OLD'); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public function whereOn(mixed $value): static { |
| 320 |
$date = $this->parse_date($value); |
| 321 |
return $this->where('ON', $date); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public function whereRecent(): static { |
| 329 |
return $this->where('RECENT'); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public function whereSeen(): static { |
| 337 |
return $this->where('SEEN'); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public function whereSince(mixed $value): static { |
| 348 |
$date = $this->parse_date($value); |
| 349 |
return $this->where('SINCE', $date); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
public function whereSubject(string $value): static { |
| 359 |
return $this->where('SUBJECT', $value); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
public function whereText(string $value): static { |
| 369 |
return $this->where('TEXT', $value); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
public function whereTo(string $value): static { |
| 379 |
return $this->where('TO', $value); |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
public function whereUnkeyword(string $value): static { |
| 389 |
return $this->where('UNKEYWORD', $value); |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
public function whereUnanswered(): static { |
| 397 |
return $this->where('UNANSWERED'); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
public function whereUndeleted(): static { |
| 405 |
return $this->where('UNDELETED'); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
public function whereUnflagged(): static { |
| 413 |
return $this->where('UNFLAGGED'); |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
public function whereUnseen(): static { |
| 421 |
return $this->where('UNSEEN'); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
public function whereNoXSpam(): static { |
| 429 |
return $this->where("CUSTOM X-Spam-Flag NO"); |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
public function whereIsXSpam(): static { |
| 437 |
return $this->where("CUSTOM X-Spam-Flag YES"); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
public function whereHeader($header, $value): static { |
| 449 |
return $this->where("CUSTOM HEADER $header $value"); |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
public function whereMessageId($messageId): static { |
| 460 |
return $this->whereHeader("Message-ID", $messageId); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
public function whereInReplyTo($messageId): static { |
| 471 |
return $this->whereHeader("In-Reply-To", $messageId); |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
public function whereLanguage($country_code): static { |
| 481 |
return $this->where("Content-Language $country_code"); |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public function whereUid(int|string $uid): static { |
| 493 |
return $this->where('UID', $uid); |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
public function whereUidIn(array $uids): static { |
| 505 |
$uids = implode(',', $uids); |
| 506 |
return $this->where('UID', $uids); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
public function when(mixed $value, callable $callback, ?callable $default = null): mixed { |
| 519 |
if ($value) { |
| 520 |
return $callback($this, $value) ?: $this; |
| 521 |
} elseif ($default) { |
| 522 |
return $default($this, $value) ?: $this; |
| 523 |
} |
| 524 |
|
| 525 |
return $this; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
public function unless(mixed $value, callable $callback, ?callable $default = null): mixed { |
| 538 |
if (!$value) { |
| 539 |
return $callback($this, $value) ?: $this; |
| 540 |
} elseif ($default) { |
| 541 |
return $default($this, $value) ?: $this; |
| 542 |
} |
| 543 |
|
| 544 |
return $this; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
public function getAvailableCriteria(): array { |
| 553 |
return $this->available_criteria; |
| 554 |
} |
| 555 |
} |