constraint.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. public static function moreThanOrEquals(string $value): Constraint {
  45. $constraint = new Constraint("MORE_THAN_OR_EQUAL");
  46. $constraint->setValues($value);
  47. return $constraint;
  48. }
  49. public static function isNotNull(): Constraint {
  50. return new Constraint("IS_NOT_NULL");
  51. }
  52. /**
  53. * Constraint constructor.
  54. * @param string $type
  55. * @throws Exception
  56. */
  57. public function __construct(string $type) {
  58. $type = "TYPE_" . strtoupper($type);
  59. if(!defined("Constraint::$type")) {
  60. throw new LogicException("Constraint::$type is not defined.");
  61. }
  62. $this->type = constant("Constraint::$type");
  63. }
  64. public function setValues(...$values): Constraint {
  65. $this->values = array_map("db::escape", $values);
  66. return $this;
  67. }
  68. public function getConstraintString() {
  69. $value_1 = $this->values[0] ?? 0;
  70. $value_2 = $this->values[1] ?? 0;
  71. switch($this->type) {
  72. case self::TYPE_LESS_THAN:
  73. return " < '$value_1'";
  74. case self::TYPE_LESS_THAN_OR_EQUAL:
  75. return " <= '$value_1'";
  76. case self::TYPE_EQUALS:
  77. return " = '$value_1'";
  78. case self::TYPE_MORE_THAN_OR_EQUAL:
  79. return " >= '$value_1'";
  80. case self::TYPE_MORE_THAN:
  81. return " > '$value_1'";
  82. case self::TYPE_BETWEEN:
  83. return " BETWEEN '$value_1' AND '$value_2'";
  84. case self::TYPE_BEGINS_WITH:
  85. return " LIKE '$value_1%'";
  86. case self::TYPE_CONTAINS:
  87. return " LIKE '%$value_1%'";
  88. case self::TYPE_ENDS_WITH:
  89. return " LIKE '%$value_1'";
  90. case self::TYPE_NOT_EQUALS:
  91. return " != '$value_1'";
  92. case self::TYPE_IS_NULL:
  93. return " IS NULL";
  94. case self::TYPE_IS_NOT_NULL:
  95. return " IS NOT NULL";
  96. case self::TYPE_NOT_IN:
  97. if(empty($this->values)) return false;
  98. $values = array_map(function($value) {
  99. return sprintf("'%s'", $value);
  100. }, $this->values);
  101. return sprintf(" NOT IN (%s)", implode(", ", $values));
  102. case self::TYPE_LIKE:
  103. return sprintf(" LIKE '%s'", $value_1);
  104. default:
  105. throw new LogicException("getConstraintString not implemented for type: $this->type");
  106. }
  107. }
  108. }