constraint.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. const TYPE_LIKE = 13;
  17. private $type;
  18. private $values = [];
  19. public static function contains(string $string): Constraint {
  20. $constraint = new Constraint("CONTAINS");
  21. $constraint->setValues($string);
  22. return $constraint;
  23. }
  24. public static function between(string $start, string $end): Constraint {
  25. $constraint = new Constraint("BETWEEN");
  26. $constraint->setValues($start, $end);
  27. return $constraint;
  28. }
  29. public static function notIn(array $values): Constraint {
  30. $constraint = new Constraint("NOT_IN");
  31. $constraint->setValues(...$values);
  32. return $constraint;
  33. }
  34. public static function notEquals(string $value): Constraint {
  35. $constraint = new Constraint("NOT_EQUALS");
  36. $constraint->setValues($value);
  37. return $constraint;
  38. }
  39. public static function like(string $value): Constraint {
  40. $constraint = new Constraint("LIKE");
  41. $constraint->setValues($value);
  42. return $constraint;
  43. }
  44. /**
  45. * Constraint constructor.
  46. * @param string $type
  47. * @throws Exception
  48. */
  49. public function __construct(string $type) {
  50. $type = "TYPE_" . strtoupper($type);
  51. if(!defined("Constraint::$type")) {
  52. throw new LogicException("Constraint::$type is not defined.");
  53. }
  54. $this->type = constant("Constraint::$type");
  55. }
  56. public function setValues(...$values): Constraint {
  57. $this->values = array_map("db::escape", $values);
  58. return $this;
  59. }
  60. public function getConstraintString() {
  61. $value_1 = $this->values[0] ?? 0;
  62. $value_2 = $this->values[1] ?? 0;
  63. switch($this->type) {
  64. case self::TYPE_LESS_THAN:
  65. return " < '$value_1'";
  66. case self::TYPE_LESS_THAN_OR_EQUAL:
  67. return " <= '$value_1'";
  68. case self::TYPE_EQUALS:
  69. return " = '$value_1'";
  70. case self::TYPE_MORE_THAN_OR_EQUAL:
  71. return " >= '$value_1'";
  72. case self::TYPE_MORE_THAN:
  73. return " > '$value_1'";
  74. case self::TYPE_BETWEEN:
  75. return " BETWEEN '$value_1' AND '$value_2'";
  76. case self::TYPE_BEGINS_WITH:
  77. return " LIKE '$value_1%'";
  78. case self::TYPE_CONTAINS:
  79. return " LIKE '%$value_1%'";
  80. case self::TYPE_ENDS_WITH:
  81. return " LIKE '%$value_1'";
  82. case self::TYPE_NOT_EQUALS:
  83. return " != '$value_1'";
  84. case self::TYPE_IS_NULL:
  85. return " IS NULL";
  86. case self::TYPE_IS_NOT_NULL:
  87. return " IS NOT NULL";
  88. case self::TYPE_NOT_IN:
  89. if(empty($this->values)) return false;
  90. $values = array_map(function($value) {
  91. return sprintf("'%s'", $value);
  92. }, $this->values);
  93. return sprintf(" NOT IN (%s)", implode(", ", $values));
  94. case self::TYPE_LIKE:
  95. return sprintf(" LIKE '%s'", $value_1);
  96. default:
  97. throw new LogicException("getConstraintString not implemented for type: $this->type");
  98. }
  99. }
  100. }