| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- class Constraint {
- const TYPE_LESS_THAN = 0;
- const TYPE_LESS_THAN_OR_EQUAL = 1;
- const TYPE_EQUALS = 2;
- const TYPE_MORE_THAN_OR_EQUAL = 3;
- const TYPE_MORE_THAN = 4;
- const TYPE_BETWEEN = 5;
- const TYPE_BEGINS_WITH = 6;
- const TYPE_CONTAINS = 7;
- const TYPE_ENDS_WITH = 8;
- const TYPE_NOT_EQUALS = 9;
- const TYPE_IS_NULL = 10;
- const TYPE_IS_NOT_NULL = 11;
- const TYPE_NOT_IN = 12;
- const TYPE_LIKE = 13;
- private $type;
- private $values = [];
- public static function contains(string $string): Constraint {
- $constraint = new Constraint("CONTAINS");
- $constraint->setValues($string);
- return $constraint;
- }
- public static function between(string $start, string $end): Constraint {
- $constraint = new Constraint("BETWEEN");
- $constraint->setValues($start, $end);
- return $constraint;
- }
- public static function notIn(array $values): Constraint {
- $constraint = new Constraint("NOT_IN");
- $constraint->setValues(...$values);
- return $constraint;
- }
- public static function notEquals(string $value): Constraint {
- $constraint = new Constraint("NOT_EQUALS");
- $constraint->setValues($value);
- return $constraint;
- }
- public static function like(string $value): Constraint {
- $constraint = new Constraint("LIKE");
- $constraint->setValues($value);
- return $constraint;
- }
- public static function moreThanOrEquals(string $value): Constraint {
- $constraint = new Constraint("MORE_THAN_OR_EQUAL");
- $constraint->setValues($value);
- return $constraint;
- }
- public static function isNotNull(): Constraint {
- return new Constraint("IS_NOT_NULL");
- }
- /**
- * Constraint constructor.
- * @param string $type
- * @throws Exception
- */
- public function __construct(string $type) {
- $type = "TYPE_" . strtoupper($type);
- if(!defined("Constraint::$type")) {
- throw new LogicException("Constraint::$type is not defined.");
- }
- $this->type = constant("Constraint::$type");
- }
- public function setValues(...$values): Constraint {
- $this->values = array_map("db::escape", $values);
- return $this;
- }
- public function getConstraintString() {
- $value_1 = $this->values[0] ?? 0;
- $value_2 = $this->values[1] ?? 0;
- switch($this->type) {
- case self::TYPE_LESS_THAN:
- return " < '$value_1'";
- case self::TYPE_LESS_THAN_OR_EQUAL:
- return " <= '$value_1'";
- case self::TYPE_EQUALS:
- return " = '$value_1'";
- case self::TYPE_MORE_THAN_OR_EQUAL:
- return " >= '$value_1'";
- case self::TYPE_MORE_THAN:
- return " > '$value_1'";
- case self::TYPE_BETWEEN:
- return " BETWEEN '$value_1' AND '$value_2'";
- case self::TYPE_BEGINS_WITH:
- return " LIKE '$value_1%'";
- case self::TYPE_CONTAINS:
- return " LIKE '%$value_1%'";
- case self::TYPE_ENDS_WITH:
- return " LIKE '%$value_1'";
- case self::TYPE_NOT_EQUALS:
- return " != '$value_1'";
- case self::TYPE_IS_NULL:
- return " IS NULL";
- case self::TYPE_IS_NOT_NULL:
- return " IS NOT NULL";
- case self::TYPE_NOT_IN:
- if(empty($this->values)) return false;
- $values = array_map(function($value) {
- return sprintf("'%s'", $value);
- }, $this->values);
- return sprintf(" NOT IN (%s)", implode(", ", $values));
- case self::TYPE_LIKE:
- return sprintf(" LIKE '%s'", $value_1);
- default:
- throw new LogicException("getConstraintString not implemented for type: $this->type");
- }
- }
- }
|