1
0

constraint.php 3.4 KB

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