Created
June 3, 2024 06:07
-
-
Save 0x1881/021c66026c863052b55519fe7f56b87f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CaseBuilder { | |
private $conditions = []; | |
public function plain($plain) | |
{ | |
if(is_string($plain)) $this->conditions[] = $plain; | |
if(is_array($plain)) $this->conditions = array_merge($this->conditions, $plain); | |
return $this; | |
} | |
public function case($case = null) { | |
$array = ["CASE"]; | |
if ($case) $array[] = "`$case`"; | |
return $this->plain($array); | |
} | |
public function when($column, $operator = null, $value = null) | |
{ | |
$array = ["WHEN `$column`"]; | |
if ($operator) { | |
$array[] = $operator; | |
$array[] = "`$value`"; | |
} | |
return $this->plain($array); | |
} | |
public function then($value) | |
{ | |
return $this->plain("THEN `$value`"); | |
} | |
public function else($value) | |
{ | |
return $this->plain("ELSE `$value`"); | |
} | |
public function end() | |
{ | |
return $this->plain("END"); | |
} | |
public function as($as) | |
{ | |
return $this->plain("AS `$as`"); | |
} | |
public function toRaw() | |
{ | |
return implode(' ', $this->conditions); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment