constraint.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. class Constraint {
  3. const TYPE_LESS_THAN = 0;
  4. const TYPE_LESS_THAN_OR_EQUAL = 1;
  5. const TYPE_EQUALS = 2;
  6. const TYPE_MORE_THAN_OR_EQUAL = 3;
  7. const TYPE_MORE_THAN = 4;
  8. const TYPE_BETWEEN = 5;
  9. const TYPE_BEGINS_WITH = 6;
  10. const TYPE_CONTAINS = 7;
  11. const TYPE_ENDS_WITH = 8;
  12. const TYPE_NOT_EQUALS = 9;
  13. const TYPE_IS_NULL = 10;
  14. const TYPE_IS_NOT_NULL = 11;
  15. const TYPE_NOT_IN = 12;
  16. private $type;
  17. private $values = [];
  18. public static function contains(string $string): Constraint {
  19. $constraint = new Constraint("CONTAINS");
  20. $constraint->setValues($string);
  21. return $constraint;
  22. }
  23. public static function between(string $start, string $end): Constraint {
  24. $constraint = new Constraint("BETWEEN");
  25. $constraint->setValues($start, $end);
  26. return $constraint;
  27. }
  28. public static function notIn(array $values): Constraint {
  29. $constraint = new Constraint("NOT_IN");
  30. $constraint->setValues(...$values);
  31. return $constraint;
  32. }
  33. /**
  34. * Constraint constructor.
  35. * @param string $type
  36. * @throws Exception
  37. */
  38. public function __construct(string $type) {
  39. $type = "TYPE_" . strtoupper($type);
  40. if(!defined("Constraint::$type")) throw new Exception("Constraint::$type is not defined.");
  41. $this->type = constant("Constraint::$type");
  42. }
  43. public function setValues(...$values) {
  44. $this->values = array_map("db::escape", $values);
  45. return $this;
  46. }
  47. public function getConstraintString() {
  48. $value_1 = $this->values[0] ?? 0;
  49. $value_2 = $this->values[1] ?? 0;
  50. switch($this->type) {
  51. case self::TYPE_LESS_THAN:
  52. return " < '$value_1'";
  53. case self::TYPE_LESS_THAN_OR_EQUAL:
  54. return " <= '$value_1'";
  55. case self::TYPE_EQUALS:
  56. return " = '$value_1'";
  57. case self::TYPE_MORE_THAN_OR_EQUAL:
  58. return " >= '$value_1'";
  59. case self::TYPE_MORE_THAN:
  60. return " > '$value_1'";
  61. case self::TYPE_BETWEEN:
  62. return " BETWEEN '$value_1' AND '$value_2'";
  63. case self::TYPE_BEGINS_WITH:
  64. return " LIKE '$value_1%'";
  65. case self::TYPE_CONTAINS:
  66. return " LIKE '%$value_1%'";
  67. case self::TYPE_ENDS_WITH:
  68. return " LIKE '%$value_1'";
  69. case self::TYPE_NOT_EQUALS:
  70. return " != '$value_1'";
  71. case self::TYPE_IS_NULL:
  72. return " IS NULL";
  73. case self::TYPE_IS_NOT_NULL:
  74. return " IS NOT NULL";
  75. case self::TYPE_NOT_IN:
  76. if(empty($this->values)) return false;
  77. $values = array_map(function($value) {
  78. return sprintf("'%s'", $value);
  79. }, $this->values);
  80. return sprintf(" NOT IN (%s)", implode(", ", $values));
  81. }
  82. return false;
  83. }
  84. }