Skip to content

Instantly share code, notes, and snippets.

@0x1881
Created June 3, 2024 06:07
Show Gist options
  • Save 0x1881/021c66026c863052b55519fe7f56b87f to your computer and use it in GitHub Desktop.
Save 0x1881/021c66026c863052b55519fe7f56b87f to your computer and use it in GitHub Desktop.
<?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