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"); } } }