constraint.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. public static function notEquals(string $value): Constraint {
  34. $constraint = new Constraint("NOT_EQUALS");
  35. $constraint->setValues($value);
  36. return $constraint;
  37. }
  38. /**
  39. * Constraint constructor.
  40. * @param string $type
  41. * @throws Exception
  42. */
  43. public function __construct(string $type) {
  44. $type = "TYPE_" . strtoupper($type);
  45. if(!defined("Constraint::$type")) throw new Exception("Constraint::$type is not defined.");
  46. $this->type = constant("Constraint::$type");
  47. }
  48. public function setValues(...$values) {
  49. $this->values = array_map("db::escape", $values);
  50. return $this;
  51. }
  52. public function getConstraintString() {
  53. $value_1 = $this->values[0] ?? 0;
  54. $value_2 = $this->values[1] ?? 0;
  55. switch($this->type) {
  56. case self::TYPE_LESS_THAN:
  57. return " < '$value_1'";
  58. case self::TYPE_LESS_THAN_OR_EQUAL:
  59. return " <= '$value_1'";
  60. case self::TYPE_EQUALS:
  61. return " = '$value_1'";
  62. case self::TYPE_MORE_THAN_OR_EQUAL:
  63. return " >= '$value_1'";
  64. case self::TYPE_MORE_THAN:
  65. return " > '$value_1'";
  66. case self::TYPE_BETWEEN:
  67. return " BETWEEN '$value_1' AND '$value_2'";
  68. case self::TYPE_BEGINS_WITH:
  69. return " LIKE '$value_1%'";
  70. case self::TYPE_CONTAINS:
  71. return " LIKE '%$value_1%'";
  72. case self::TYPE_ENDS_WITH:
  73. return " LIKE '%$value_1'";
  74. case self::TYPE_NOT_EQUALS:
  75. return " != '$value_1'";
  76. case self::TYPE_IS_NULL:
  77. return " IS NULL";
  78. case self::TYPE_IS_NOT_NULL:
  79. return " IS NOT NULL";
  80. case self::TYPE_NOT_IN:
  81. if(empty($this->values)) return false;
  82. $values = array_map(function($value) {
  83. return sprintf("'%s'", $value);
  84. }, $this->values);
  85. return sprintf(" NOT IN (%s)", implode(", ", $values));
  86. }
  87. return false;
  88. }
  89. }