DBClause.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Model;
  3. use Closure;
  4. class DBClause
  5. {
  6. private $identify;
  7. public function __construct()
  8. {
  9. $this->identify = "";
  10. }
  11. public function on($first, $operator = null, $second = null, $boolean = 'and')
  12. {
  13. $identify = $this->addEncode($first);
  14. $identify.= $this->addEncode($operator);
  15. $identify.= $this->addEncode($second);
  16. $identify.= $this->addEncode($boolean);
  17. $this->identify.= $identify;
  18. }
  19. public function orOn($first, $operator = null, $second = null)
  20. {
  21. $this->on($first, $operator, $second, 'or');
  22. }
  23. public function toString()
  24. {
  25. return $this->identify;
  26. }
  27. private function addEncode($value)
  28. {
  29. $encodeName = '';
  30. if (is_array($value)) {
  31. ksort($value);
  32. foreach ($value AS $key => $val) {
  33. $encodeName .= ':' . $key . '=' . $this->addEncode($val) . ';';
  34. }
  35. } elseif ($value instanceof Closure) {
  36. $join = new DBClause();
  37. call_user_func($value, $join);
  38. $encodeName .= ':' . $join->toString() . ';';
  39. } elseif (is_string($value) || is_numeric($value)) {
  40. $encodeName .= ':' . $value . ';';
  41. } else {
  42. $encodeName .= ':' . var_export($value, true) . ';';
  43. }
  44. return $encodeName;
  45. }
  46. }